nsv.c 13.6 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * nsv.c: NullSoft Video demuxer.
 *****************************************************************************
 * Copyright (C) 2004 VideoLAN
hartman's avatar
hartman committed
5
 * $Id: nsv.c,v 1.5 2004/01/25 20:05:28 hartman Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>

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

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

vlc_module_begin();
    set_description( _("NullSoft demuxer" ) );
46
    set_capability( "demux2", 10 );
47
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;

    int64_t     i_pcr;
64
    int64_t     i_time;
65
66
67
68
69
70
    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 );

71
72
73
74
75
static int ReSynch( demux_t *p_demux );

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

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*****************************************************************************
 * 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;
    int         i;

    if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
    {
        msg_Err( p_demux, "cannot peek" );
        return VLC_EGENERIC;
    }
    if( strncmp( p_peek, "NSVf", 4 ) && strncmp( p_peek, "NSVs", 4 ))
    {
94
95
96
97
98
99
       /* In case we had force this demuxer we try to resynch */
        if( strcmp( p_demux->psz_demux, "nsv" ) || ReSynch( p_demux ) )
        {
            msg_Warn( p_demux, "NSV module discarded" );
            return VLC_EGENERIC;
        }
100
101
102
103
104
105
    }

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

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

110
    es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
111
112
113
    p_sys->p_video = NULL;

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

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

    int         i_size;
    block_t     *p_frame;

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

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

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

    /* Set PCR */
195
    es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
196
197
198
199
200
201
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
227
228
229
230
231
232
233
234
235
236
237

    /* Read video */
    i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
    if( i_size > 0 )
    {
        /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
        if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
        {
            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];
            stream_Read( p_demux->s, h, 4 );

            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 =
            p_frame->i_pts = p_sys->i_pcr;
            es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
        }
    }

    p_sys->i_pcr += p_sys->i_pcr_inc;
238
239
240
241
    if( p_sys->i_time >= 0 )
    {
        p_sys->i_time += p_sys->i_pcr_inc;
    }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

    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;
269

270
271
272
273
274
        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 );
275
276
277
278
279
280
            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;
281
282
283

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
284
            if( p_sys->i_time < 0 )
285
            {
286
287
                *pi64 = 0;
                return VLC_EGENERIC;
288
            }
289
290
            *pi64 = p_sys->i_time;
            return VLC_SUCCESS;
291

292
#if 0
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        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;

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

        default:
            return VLC_EGENERIC;
    }
}

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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*****************************************************************************
 * ReSynch:
 *****************************************************************************/
static int ReSynch( demux_t *p_demux )
{
    uint8_t *p_peek;
    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;

        while( i_skip < i_peek - 4 )
        {
            if( !strncmp( p_peek, "NSVf", 4 ) || !strncmp( p_peek, "NSVs", 4 ) )
            {
                if( i_skip > 0 )
                {
                    stream_Read( p_demux->s, NULL, i_skip );
                }
                return VLC_SUCCESS;
            }
            p_peek++;
            i_skip++;
        }

        stream_Read( p_demux->s, NULL, i_skip );
    }
    return VLC_EGENERIC;
}

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

hartman's avatar
hartman committed
360
    msg_Dbg( p_demux, "new NSVf chunk" );
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    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;
}
/*****************************************************************************
 * ReadNSVf:
 *****************************************************************************/
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;
    }

hartman's avatar
hartman committed
386
    msg_Dbg( p_demux, "new NSVs chunk" );
387
388
389
390
391
392
393
394
395
396
    /* 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;
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
            break;
        default:
hartman's avatar
hartman committed
397
            msg_Warn( p_demux, "unknown codec" );
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
            break;
    }
    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
    {
        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;
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
            break;
        default:
hartman's avatar
hartman committed
429
            msg_Warn( p_demux, "unknown codec" );
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
            break;
    }

    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
    {
        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 )
    {
        switch( header[16]&0x7f )
        {
            case 1: /* 29.97 fps */
                p_sys->i_pcr_inc = 33367;
                break;
            case 3: /* 23.976 fps */
                p_sys->i_pcr_inc = 41708;
                break;
            case 5: /* 14.98 fps */
                p_sys->i_pcr_inc = 66755;
                break;
            default:
hartman's avatar
hartman committed
459
                msg_Dbg( p_demux, "unknown fps (0x%x)", header[16] );
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
                p_sys->i_pcr_inc = 40000;
                break;
        }
    }
    else if( header[16] != 0 )
    {
        p_sys->i_pcr_inc = 1000000 / header[16];
    }
    else
    {
        msg_Dbg( p_demux, "invalid fps (0x00)" );
        p_sys->i_pcr_inc = 40000;
    }
    msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );

    return VLC_SUCCESS;
}
477