nsv.c 16.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * nsv.c: NullSoft Video demuxer.
 *****************************************************************************
4
 * Copyright (C) 2004-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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28
 *****************************************************************************/

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

#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
29
#include <vlc_demux.h>
30

31 32 33 34 35 36
/* TODO:
 *  - implement NSVf parsing (to get meta data)
 *  - implement missing Control (and in the right way)
 *  - ...
 */

37 38 39 40 41 42 43 44
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close  ( vlc_object_t * );

vlc_module_begin();
    set_description( _("NullSoft demuxer" ) );
45
    set_capability( "demux2", 10 );
zorglub's avatar
zorglub committed
46 47
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    set_callbacks( Open, Close );
    add_shortcut( "nsv" );
vlc_module_end();

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

struct demux_sys_t
{
    es_format_t  fmt_audio;
    es_out_id_t *p_audio;

    es_format_t  fmt_video;
    es_out_id_t *p_video;

64 65 66
    es_format_t  fmt_sub;
    es_out_id_t  *p_sub;

67
    int64_t     i_pcr;
68
    int64_t     i_time;
69 70 71 72 73 74
    int64_t     i_pcr_inc;
};

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

75 76 77 78 79
static int ReSynch( demux_t *p_demux );

static int ReadNSVf( demux_t *p_demux );
static int ReadNSVs( demux_t *p_demux );

80 81 82 83 84 85 86 87
/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

88
    const uint8_t *p_peek;
89

90 91
    if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
        return VLC_EGENERIC;
92

93
    if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
94 95
    {
       /* In case we had force this demuxer we try to resynch */
96
        if( !p_demux->b_force || ReSynch( p_demux ) )
97 98 99
            return VLC_EGENERIC;
    }

100 101 102 103
    /* 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 ) );
104

105 106 107
    es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
    p_sys->p_audio = NULL;

108
    es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
109 110
    p_sys->p_video = NULL;

111 112 113
    es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
    p_sys->p_sub = NULL;

114
    p_sys->i_pcr   = 1;
115
    p_sys->i_time  = 0;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    p_sys->i_pcr_inc = 0;

    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;

    free( p_sys );
}


/*****************************************************************************
 * Demux:
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

140
    uint8_t     header[5];
141
    const uint8_t *p_peek;
142 143 144 145

    int         i_size;
    block_t     *p_frame;

146
    for( ;; )
147
    {
148
        if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
149
        {
150
            msg_Warn( p_demux, "cannot peek" );
151 152 153
            return 0;
        }

154
        if( !memcmp( p_peek, "NSVf", 4 ) )
155
        {
156
            if( ReadNSVf( p_demux ) )
157
            {
158
                return -1;
159 160
            }
        }
161
        else if( !memcmp( p_peek, "NSVs", 4 ) )
162
        {
163
            if( ReadNSVs( p_demux ) )
164
            {
165
                return -1;
166
            }
167
            break;
168
        }
169
        else if( GetWLE( p_peek ) == 0xbeef )
170
        {
171 172
            /* Next frame of the current NSVs chunk */
            if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
173
            {
174 175
                msg_Warn( p_demux, "cannot read" );
                return 0;
176
            }
177
            break;
178 179 180
        }
        else
        {
181
            msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
182 183 184 185
            if( ReSynch( p_demux ) )
            {
                return -1;
            }
186 187 188 189 190 191 192 193 194 195
        }
    }

    if( stream_Read( p_demux->s, header, 5 ) < 5 )
    {
        msg_Warn( p_demux, "cannot read" );
        return 0;
    }

    /* Set PCR */
196
    es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
197 198 199 200 201

    /* Read video */
    i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
    if( i_size > 0 )
    {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        /* extra data ? */
        if( (header[0]&0x0f) != 0x0 )
        {
            uint8_t      aux[6];
            int          i_aux;
            vlc_fourcc_t fcc;
            if( stream_Read( p_demux->s, aux, 6 ) < 6 )
            {
                msg_Warn( p_demux, "cannot read" );
                return 0;
            }
            i_aux = GetWLE( aux );
            fcc   = VLC_FOURCC( aux[2], aux[3], aux[4], aux[5] );

            msg_Dbg( p_demux, "Belekas: %d - size=%d fcc=%4.4s",
                     header[0]&0xf, i_aux, (char*)&fcc );

            if( fcc == VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux > 2 )
            {
                if( p_sys->p_sub == NULL )
                {
                    p_sys->fmt_sub.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
                    p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
                    es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
                }
227 228
                stream_Read( p_demux->s, NULL, 2 );

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
                {
                    uint8_t *p = p_frame->p_buffer;

                    while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
                    {
                        p++;
                    }
                    if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
                    {
                        p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
                        p_frame->p_buffer = p + 1;
                    }

                    /* Skip the first part (it is the language name) */
                    p_frame->i_pts = p_sys->i_pcr;
                    p_frame->i_dts = p_sys->i_pcr + 4000000;    /* 4s */

                    es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
                }
            }
            else
            {
                /* We skip this extra data */
                if( stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
                {
                    msg_Warn( p_demux, "cannot read" );
                    return 0;
                }
            }
            i_size -= 6 + i_aux;
        }

262
        /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
263
        if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
264 265 266 267 268 269 270 271 272 273 274 275 276 277
        {
            p_frame->i_dts = p_sys->i_pcr;
            es_out_Send( p_demux->out, p_sys->p_video, p_frame );
        }
    }

    /* Read audio */
    i_size = header[3] | ( header[4] << 8 );
    if( i_size > 0 )
    {
        /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
        if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
        {
            uint8_t h[4];
278
            stream_Read( p_demux->s, h, 4 );
279 280 281 282 283 284 285 286 287 288 289 290 291 292

            p_sys->fmt_audio.audio.i_channels = h[1];
            p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );

            i_size -= 4;
        }
        if( p_sys->p_audio == NULL )
        {
            p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
        }

        if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
        {
            p_frame->i_dts =
293
            p_frame->i_pts = p_sys->i_pcr;
294 295 296 297 298
            es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
        }
    }

    p_sys->i_pcr += p_sys->i_pcr_inc;
299 300 301 302
    if( p_sys->i_time >= 0 )
    {
        p_sys->i_time += p_sys->i_pcr_inc;
    }
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

    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, *pi64;

    switch( i_query )
    {
        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;
330

331 332 333 334 335
        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 );
336 337 338 339 340 341
            if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
            {
                return VLC_EGENERIC;
            }
            p_sys->i_time = -1; /* Invalidate time display */
            return VLC_SUCCESS;
342 343 344

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
345
            if( p_sys->i_time < 0 )
346
            {
347 348
                *pi64 = 0;
                return VLC_EGENERIC;
349
            }
350 351
            *pi64 = p_sys->i_time;
            return VLC_SUCCESS;
352

353
#if 0
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_mux_rate > 0 )
            {
                *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
                return VLC_SUCCESS;
            }
            *pi64 = 0;
            return VLC_EGENERIC;

#endif
        case DEMUX_GET_FPS:
            pf = (double*)va_arg( args, double * );
            *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
            return VLC_SUCCESS;

370
        case DEMUX_SET_TIME:
371 372 373 374 375
        default:
            return VLC_EGENERIC;
    }
}

376 377 378 379 380
/*****************************************************************************
 * ReSynch:
 *****************************************************************************/
static int ReSynch( demux_t *p_demux )
{
381
    const uint8_t *p_peek;
382 383 384 385 386 387 388 389 390 391
    int      i_skip;
    int      i_peek;

    while( !p_demux->b_die )
    {
        if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
        {
            return VLC_EGENERIC;
        }
        i_skip = 0;
zorglub's avatar
zorglub committed
392

393 394
        while( i_skip < i_peek - 4 )
        {
395 396
            if( !memcmp( p_peek, "NSVf", 4 )
             || !memcmp( p_peek, "NSVs", 4 ) )
397 398 399 400 401 402 403 404 405 406 407
            {
                if( i_skip > 0 )
                {
                    stream_Read( p_demux->s, NULL, i_skip );
                }
                return VLC_SUCCESS;
            }
            p_peek++;
            i_skip++;
        }

408
        stream_Read( p_demux->s, NULL, i_skip );
409 410 411 412 413 414 415 416 417
    }
    return VLC_EGENERIC;
}

/*****************************************************************************
 * ReadNSVf:
 *****************************************************************************/
static int ReadNSVf( demux_t *p_demux )
{
418
    /* demux_sys_t *p_sys = p_demux->p_sys; */
419
    const uint8_t     *p;
420 421
    int         i_size;

hartman's avatar
hartman committed
422
    msg_Dbg( p_demux, "new NSVf chunk" );
423 424 425 426 427 428 429 430 431 432 433
    if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
    {
        return VLC_EGENERIC;
    }

    i_size = GetDWLE( &p[4] );
    msg_Dbg( p_demux, "    - size=%d", i_size );

    return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
}
/*****************************************************************************
434
 * ReadNSVf:
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
 *****************************************************************************/
static int ReadNSVs( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t      header[19];
    vlc_fourcc_t fcc;

    if( stream_Read( p_demux->s, header, 19 ) < 19 )
    {
        msg_Warn( p_demux, "cannot read" );
        return VLC_EGENERIC;
    }

    /* Video */
    switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
    {
        case VLC_FOURCC( 'V', 'P', '3', ' ' ):
        case VLC_FOURCC( 'V', 'P', '3', '1' ):
            fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
            break;
455 456
        case VLC_FOURCC( 'V', 'P', '6', '0' ):
        case VLC_FOURCC( 'V', 'P', '6', '1' ):
457
        case VLC_FOURCC( 'V', 'P', '6', '2' ):
458 459 460
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
            break;
        default:
461
            msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
462 463
            break;
    }
464
    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
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
    {
        es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
        p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
        p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
        if( p_sys->p_video )
        {
            es_out_Del( p_demux->out, p_sys->p_video );
        }
        p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );

        msg_Dbg( p_demux, "    - video `%4.4s' %dx%d",
                 (char*)&fcc,
                 p_sys->fmt_video.video.i_width,
                 p_sys->fmt_video.video.i_height );
    }

    /* Audio */
    switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
    {
        case VLC_FOURCC( 'M', 'P', '3', ' ' ):
            fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
            break;
        case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
            fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
            break;
490
        case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
hartman's avatar
hartman committed
491
        case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
492 493
            fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
            break;
494 495 496
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
            break;
        default:
497
            msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
498 499 500
            break;
    }

501
    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
502 503 504 505 506 507 508 509 510 511 512 513 514
    {
        msg_Dbg( p_demux, "    - audio `%4.4s'", (char*)&fcc );

        if( p_sys->p_audio )
        {
            es_out_Del( p_demux->out, p_sys->p_audio );
            p_sys->p_audio = NULL;
        }
        es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
    }

    if( header[16]&0x80 )
    {
515
        /* Fractional frame rate */
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        switch( header[16]&0x03 )
        {
            case 0: /* 30 fps */
                p_sys->i_pcr_inc = 33333; /* 300000/9 */
                break;
            case 1: /* 29.97 fps */
                p_sys->i_pcr_inc = 33367; /* 300300/9 */
                break;
            case 2: /* 25 fps */
                p_sys->i_pcr_inc = 40000; /* 360000/9 */
                break;
            case 3: /* 23.98 fps */
                p_sys->i_pcr_inc = 41700; /* 375300/9 */
                break;
        }

        if( header[16] < 0xc0 )
            p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
        else
            p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
536 537 538
    }
    else if( header[16] != 0 )
    {
539
        /* Integer frame rate */
540 541 542 543 544 545 546
        p_sys->i_pcr_inc = 1000000 / header[16];
    }
    else
    {
        msg_Dbg( p_demux, "invalid fps (0x00)" );
        p_sys->i_pcr_inc = 40000;
    }
547
    //msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
548 549 550

    return VLC_SUCCESS;
}
551