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

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

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

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_demux.h>
35

36 37 38 39 40 41
/* TODO:
 *  - implement NSVf parsing (to get meta data)
 *  - implement missing Control (and in the right way)
 *  - ...
 */

42 43 44 45 46 47
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close  ( vlc_object_t * );

48 49 50 51 52 53 54 55
vlc_module_begin ()
    set_description( N_("NullSoft demuxer" ) )
    set_capability( "demux", 10 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_callbacks( Open, Close )
    add_shortcut( "nsv" )
vlc_module_end ()
56 57 58 59 60 61 62 63 64 65 66 67 68

/*****************************************************************************
 * 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;

69 70 71
    es_format_t  fmt_sub;
    es_out_id_t  *p_sub;

72
    int64_t     i_pcr;
73
    int64_t     i_time;
74
    int64_t     i_pcr_inc;
75 76

    bool b_start_record;
77 78 79 80 81
};

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

82 83 84 85 86
static int ReSynch( demux_t *p_demux );

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

87 88 89 90 91 92 93 94
/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

95
    const uint8_t *p_peek;
96

97
    if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
98
        return VLC_EGENERIC;
99

100
    if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
101 102
    {
       /* In case we had force this demuxer we try to resynch */
103
        if( !p_demux->obj.force || ReSynch( p_demux ) )
104 105 106
            return VLC_EGENERIC;
    }

107 108 109 110
    p_sys = malloc( sizeof( demux_sys_t ) );
    if( unlikely(p_sys == NULL) )
        return VLC_ENOMEM;

111
    /* Fill p_demux field */
112
    p_demux->p_sys = p_sys;
113 114
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
115

116 117 118
    es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
    p_sys->p_audio = NULL;

119
    es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
120 121
    p_sys->p_video = NULL;

122 123 124
    es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
    p_sys->p_sub = NULL;

125
    p_sys->i_pcr   = 0;
126
    p_sys->i_time  = 0;
127 128
    p_sys->i_pcr_inc = 0;

129 130
    p_sys->b_start_record = false;

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    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;

153
    uint8_t     header[5];
154
    const uint8_t *p_peek;
155 156 157 158

    int         i_size;
    block_t     *p_frame;

159
    for( ;; )
160
    {
161
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
162
        {
163
            msg_Warn( p_demux, "cannot peek" );
164 165 166
            return 0;
        }

167
        if( !memcmp( p_peek, "NSVf", 4 ) )
168
        {
169 170
            if( ReadNSVf( p_demux ) )
                return -1;
171
        }
172
        else if( !memcmp( p_peek, "NSVs", 4 ) )
173
        {
174
            if( p_sys->b_start_record )
175
            {
176
                /* Enable recording once synchronized */
177
                vlc_stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, true, "nsv" );
178
                p_sys->b_start_record = false;
179
            }
180 181 182

            if( ReadNSVs( p_demux ) )
                return -1;
183
            break;
184
        }
185
        else if( GetWLE( p_peek ) == 0xbeef )
186
        {
187
            /* Next frame of the current NSVs chunk */
188
            if( vlc_stream_Read( p_demux->s, NULL, 2 ) < 2 )
189
            {
190 191
                msg_Warn( p_demux, "cannot read" );
                return 0;
192
            }
193
            break;
194 195 196
        }
        else
        {
197
            msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
198 199
            if( ReSynch( p_demux ) )
                return -1;
200 201 202
        }
    }

203
    if( vlc_stream_Read( p_demux->s, header, 5 ) < 5 )
204 205 206 207 208 209
    {
        msg_Warn( p_demux, "cannot read" );
        return 0;
    }

    /* Set PCR */
210
    es_out_SetPCR( p_demux->out, VLC_TS_0 + p_sys->i_pcr );
211 212 213 214 215

    /* Read video */
    i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
    if( i_size > 0 )
    {
216 217 218 219 220 221
        /* extra data ? */
        if( (header[0]&0x0f) != 0x0 )
        {
            uint8_t      aux[6];
            int          i_aux;
            vlc_fourcc_t fcc;
222
            if( vlc_stream_Read( p_demux->s, aux, 6 ) < 6 )
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            {
                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 );
                }
241 242
                if( vlc_stream_Read( p_demux->s, NULL, 2 ) < 2 )
                    return 0;
243

244
                if( ( p_frame = vlc_stream_Block( p_demux->s, i_aux - 2 ) ) )
245 246 247 248 249 250 251 252 253 254 255 256 257 258
                {
                    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) */
259 260
                    p_frame->i_pts = VLC_TS_0 + p_sys->i_pcr;
                    p_frame->i_dts = VLC_TS_0 + p_sys->i_pcr + 4000000;    /* 4s */
261 262 263 264 265 266 267

                    es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
                }
            }
            else
            {
                /* We skip this extra data */
268
                if( vlc_stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
269 270 271 272 273 274 275 276
                {
                    msg_Warn( p_demux, "cannot read" );
                    return 0;
                }
            }
            i_size -= 6 + i_aux;
        }

277
        /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
278
        if( i_size > 0 && ( p_frame = vlc_stream_Block( p_demux->s, i_size ) ) )
279
        {
280
            p_frame->i_dts = VLC_TS_0 + p_sys->i_pcr;
281 282 283 284 285 286 287 288 289

            if( p_sys->p_video )
                es_out_Send( p_demux->out, p_sys->p_video, p_frame );
            else
            {
                block_Release( p_frame );
                msg_Dbg( p_demux, "ignoring unsupported video frame (size=%d)",
                         i_size );
            }
290 291 292 293 294 295 296 297 298 299 300
        }
    }

    /* 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];
301 302
            if( vlc_stream_Read( p_demux->s, h, 4 ) < 4 )
                return 0;
303 304 305 306 307 308 309 310 311 312 313

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

314
        if( ( p_frame = vlc_stream_Block( p_demux->s, i_size ) ) )
315 316
        {
            p_frame->i_dts =
317
            p_frame->i_pts = VLC_TS_0 + p_sys->i_pcr;
318 319 320 321 322 323 324 325 326

            if( p_sys->p_audio )
                es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
            else
            {
                block_Release( p_frame );
                msg_Dbg( p_demux, "ignoring unsupported audio frame (size=%d)",
                         i_size );
            }
327 328 329 330
        }
    }

    p_sys->i_pcr += p_sys->i_pcr_inc;
331 332 333 334
    if( p_sys->i_time >= 0 )
    {
        p_sys->i_time += p_sys->i_pcr_inc;
    }
335 336 337 338 339 340 341 342 343 344 345

    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;
346
    bool b_bool, *pb_bool;
347 348 349 350
    int64_t i64, *pi64;

    switch( i_query )
    {
351
        case DEMUX_CAN_SEEK:
352
            return vlc_stream_vaControl( p_demux->s, i_query, args );
353

354
        case DEMUX_GET_POSITION:
355
            pf = va_arg( args, double * );
356 357 358
            i64 = stream_Size( p_demux->s );
            if( i64 > 0 )
            {
359
                double current = vlc_stream_Tell( p_demux->s );
360
                *pf = current / (double)i64;
361 362 363 364 365 366
            }
            else
            {
                *pf = 0.0;
            }
            return VLC_SUCCESS;
367

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

372
            if( vlc_stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
373
                return VLC_EGENERIC;
374

375 376
            p_sys->i_time = -1; /* Invalidate time display */
            return VLC_SUCCESS;
377 378

        case DEMUX_GET_TIME:
379
            pi64 = va_arg( args, int64_t * );
380
            if( p_sys->i_time < 0 )
381
            {
382 383
                *pi64 = 0;
                return VLC_EGENERIC;
384
            }
385 386
            *pi64 = p_sys->i_time;
            return VLC_SUCCESS;
387

388
#if 0
389
        case DEMUX_GET_LENGTH:
390
            pi64 = va_arg( args, int64_t * );
391 392 393 394 395 396 397 398 399 400
            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:
401
            pf = va_arg( args, double * );
402 403 404
            *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
            return VLC_SUCCESS;

405
        case DEMUX_CAN_RECORD:
406
            pb_bool = va_arg( args, bool * );
407 408 409 410 411 412 413
            *pb_bool = true;
            return VLC_SUCCESS;

        case DEMUX_SET_RECORD_STATE:
            b_bool = (bool)va_arg( args, int );

            if( !b_bool )
414
                vlc_stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
415 416 417 418
            p_sys->b_start_record = b_bool;
            return VLC_SUCCESS;


419
        case DEMUX_SET_TIME:
420 421 422 423 424
        default:
            return VLC_EGENERIC;
    }
}

425 426 427 428 429
/*****************************************************************************
 * ReSynch:
 *****************************************************************************/
static int ReSynch( demux_t *p_demux )
{
430
    for( ;; )
431
    {
432
        const uint8_t *p_peek;
433
        int i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 1024 );
434 435 436 437
        if( i_peek < 8 )
            break;

        int i_skip = 0;
Clément Stenac's avatar
Clément Stenac committed
438

439 440
        while( i_skip < i_peek - 4 )
        {
441 442
            if( !memcmp( p_peek, "NSVf", 4 )
             || !memcmp( p_peek, "NSVs", 4 ) )
443
            {
444
                if( i_skip > 0
445
                 && vlc_stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
446
                    return VLC_EGENERIC;
447 448 449 450 451 452
                return VLC_SUCCESS;
            }
            p_peek++;
            i_skip++;
        }

453
        if( vlc_stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
454
            break;
455 456 457 458 459 460 461 462 463
    }
    return VLC_EGENERIC;
}

/*****************************************************************************
 * ReadNSVf:
 *****************************************************************************/
static int ReadNSVf( demux_t *p_demux )
{
464
    /* demux_sys_t *p_sys = p_demux->p_sys; */
465
    const uint8_t     *p;
466

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
467
    msg_Dbg( p_demux, "new NSVf chunk" );
468
    if( vlc_stream_Peek( p_demux->s, &p, 8 ) < 8 )
469 470 471 472
    {
        return VLC_EGENERIC;
    }

473 474 475 476 477 478
    uint32_t i_header_size = GetDWLE( &p[4] );
    msg_Dbg( p_demux, "    - size=%" PRIu32, i_header_size );

    if( i_header_size == 0 || i_header_size == UINT32_MAX )
        return VLC_EGENERIC;

479

480 481
    return vlc_stream_Read( p_demux->s, NULL, i_header_size ) == i_header_size
        ? VLC_SUCCESS : VLC_EGENERIC;
482 483
}
/*****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
484
 * ReadNSVs:
485 486 487 488 489 490 491
 *****************************************************************************/
static int ReadNSVs( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t      header[19];
    vlc_fourcc_t fcc;

492
    if( vlc_stream_Read( p_demux->s, header, 19 ) < 19 )
493 494 495 496 497 498 499 500 501
    {
        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', ' ' ):
502
        case VLC_FOURCC( 'V', 'P', '3', '0' ):
503
            fcc = VLC_FOURCC( 'V', 'P', '3', '0' );
504
            break;
Laurent Aimar's avatar
Laurent Aimar committed
505 506 507 508 509

        case VLC_FOURCC( 'V', 'P', '3', '1' ):
            fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
            break;

510 511 512 513
        case VLC_FOURCC( 'V', 'P', '5', ' ' ):
        case VLC_FOURCC( 'V', 'P', '5', '0' ):
            fcc = VLC_FOURCC( 'V', 'P', '5', '0' );
            break;
514 515
        case VLC_FOURCC( 'V', 'P', '6', '0' ):
        case VLC_FOURCC( 'V', 'P', '6', '1' ):
516
        case VLC_FOURCC( 'V', 'P', '6', '2' ):
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
517
        case VLC_FOURCC( 'V', 'P', '8', '0' ):
518
        case VLC_FOURCC( 'H', '2', '6', '4' ):
519 520 521
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
            break;
        default:
522
            msg_Err( p_demux, "unsupported video codec %4.4s", (char *)&fcc );
523 524
            break;
    }
525
    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
526 527 528 529
    {
        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] );
530 531
        p_sys->fmt_video.video.i_visible_width = p_sys->fmt_video.video.i_width;
        p_sys->fmt_video.video.i_visible_height = p_sys->fmt_video.video.i_height;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        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;
553
        case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
554
        case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
555 556
            fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
            break;
557 558 559
        case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
            fcc = VLC_FOURCC( 's', 'p', 'x', ' ' );
            break;
560 561 562
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
            break;
        default:
563
            msg_Err( p_demux, "unsupported audio codec %4.4s", (char *)&fcc );
564 565 566
            break;
    }

567
    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
568 569 570 571 572 573 574 575 576 577 578 579 580
    {
        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 )
    {
581
        /* Fractional frame rate */
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
        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 );
602 603 604
    }
    else if( header[16] != 0 )
    {
605
        /* Integer frame rate */
606 607 608 609 610 611 612
        p_sys->i_pcr_inc = 1000000 / header[16];
    }
    else
    {
        msg_Dbg( p_demux, "invalid fps (0x00)" );
        p_sys->i_pcr_inc = 40000;
    }
613
    //msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
614

615 616 617 618 619 620
    if( p_sys->p_audio == NULL && p_sys->p_video == NULL )
    {
        msg_Err( p_demux, "unable to play neither audio nor video, aborting." );
        return VLC_EGENERIC;
    }

621 622
    return VLC_SUCCESS;
}
623