]> git.sesse.net Git - vlc/blob - modules/demux/nsv.c
d864b8306207f13a3ae2da6848308e62e39bda88
[vlc] / modules / demux / nsv.c
1 /*****************************************************************************
2  * nsv.c: NullSoft Video demuxer.
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30
31 /* TODO:
32  *  - implement NSVf parsing (to get meta data)
33  *  - implement missing Control (and in the right way)
34  *  - ...
35  */
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open    ( vlc_object_t * );
41 static void Close  ( vlc_object_t * );
42
43 vlc_module_begin();
44     set_description( _("NullSoft demuxer" ) );
45     set_capability( "demux2", 10 );
46     set_category( CAT_INPUT );
47     set_subcategory( SUBCAT_INPUT_DEMUX );
48     set_callbacks( Open, Close );
49     add_shortcut( "nsv" );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55
56 struct demux_sys_t
57 {
58     es_format_t  fmt_audio;
59     es_out_id_t *p_audio;
60
61     es_format_t  fmt_video;
62     es_out_id_t *p_video;
63
64     es_format_t  fmt_sub;
65     es_out_id_t  *p_sub;
66
67     int64_t     i_pcr;
68     int64_t     i_time;
69     int64_t     i_pcr_inc;
70 };
71
72 static int Demux  ( demux_t *p_demux );
73 static int Control( demux_t *p_demux, int i_query, va_list args );
74
75 static int ReSynch( demux_t *p_demux );
76
77 static int ReadNSVf( demux_t *p_demux );
78 static int ReadNSVs( demux_t *p_demux );
79
80 /*****************************************************************************
81  * Open
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     demux_t     *p_demux = (demux_t*)p_this;
86     demux_sys_t *p_sys;
87
88     const uint8_t *p_peek;
89
90     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
91         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( strcmp( p_demux->psz_demux, "nsv" ) || ReSynch( p_demux ) )
97         {
98             return VLC_EGENERIC;
99         }
100     }
101
102     /* Fill p_demux field */
103     p_demux->pf_demux = Demux;
104     p_demux->pf_control = Control;
105     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
106
107     es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
108     p_sys->p_audio = NULL;
109
110     es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
111     p_sys->p_video = NULL;
112
113     es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
114     p_sys->p_sub = NULL;
115
116     p_sys->i_pcr   = 1;
117     p_sys->i_time  = 0;
118     p_sys->i_pcr_inc = 0;
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Close
125  *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
127 {
128     demux_t     *p_demux = (demux_t*)p_this;
129     demux_sys_t *p_sys = p_demux->p_sys;
130
131     free( p_sys );
132 }
133
134
135 /*****************************************************************************
136  * Demux:
137  *****************************************************************************/
138 static int Demux( demux_t *p_demux )
139 {
140     demux_sys_t *p_sys = p_demux->p_sys;
141
142     uint8_t     header[5];
143     const uint8_t *p_peek;
144
145     int         i_size;
146     block_t     *p_frame;
147
148     for( ;; )
149     {
150         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
151         {
152             msg_Warn( p_demux, "cannot peek" );
153             return 0;
154         }
155
156         if( !memcmp( p_peek, "NSVf", 4 ) )
157         {
158             if( ReadNSVf( p_demux ) )
159             {
160                 return -1;
161             }
162         }
163         else if( !memcmp( p_peek, "NSVs", 4 ) )
164         {
165             if( ReadNSVs( p_demux ) )
166             {
167                 return -1;
168             }
169             break;
170         }
171         else if( GetWLE( p_peek ) == 0xbeef )
172         {
173             /* Next frame of the current NSVs chunk */
174             if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
175             {
176                 msg_Warn( p_demux, "cannot read" );
177                 return 0;
178             }
179             break;
180         }
181         else
182         {
183             msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
184             if( ReSynch( p_demux ) )
185             {
186                 return -1;
187             }
188         }
189     }
190
191     if( stream_Read( p_demux->s, header, 5 ) < 5 )
192     {
193         msg_Warn( p_demux, "cannot read" );
194         return 0;
195     }
196
197     /* Set PCR */
198     es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
199
200     /* Read video */
201     i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
202     if( i_size > 0 )
203     {
204         /* extra data ? */
205         if( (header[0]&0x0f) != 0x0 )
206         {
207             uint8_t      aux[6];
208             int          i_aux;
209             vlc_fourcc_t fcc;
210             if( stream_Read( p_demux->s, aux, 6 ) < 6 )
211             {
212                 msg_Warn( p_demux, "cannot read" );
213                 return 0;
214             }
215             i_aux = GetWLE( aux );
216             fcc   = VLC_FOURCC( aux[2], aux[3], aux[4], aux[5] );
217
218             msg_Dbg( p_demux, "Belekas: %d - size=%d fcc=%4.4s",
219                      header[0]&0xf, i_aux, (char*)&fcc );
220
221             if( fcc == VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux > 2 )
222             {
223                 if( p_sys->p_sub == NULL )
224                 {
225                     p_sys->fmt_sub.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
226                     p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
227                     es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
228                 }
229                 stream_Read( p_demux->s, NULL, 2 );
230
231                 if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
232                 {
233                     uint8_t *p = p_frame->p_buffer;
234
235                     while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
236                     {
237                         p++;
238                     }
239                     if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
240                     {
241                         p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
242                         p_frame->p_buffer = p + 1;
243                     }
244
245                     /* Skip the first part (it is the language name) */
246                     p_frame->i_pts = p_sys->i_pcr;
247                     p_frame->i_dts = p_sys->i_pcr + 4000000;    /* 4s */
248
249                     es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
250                 }
251             }
252             else
253             {
254                 /* We skip this extra data */
255                 if( stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
256                 {
257                     msg_Warn( p_demux, "cannot read" );
258                     return 0;
259                 }
260             }
261             i_size -= 6 + i_aux;
262         }
263
264         /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
265         if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
266         {
267             p_frame->i_dts = p_sys->i_pcr;
268             es_out_Send( p_demux->out, p_sys->p_video, p_frame );
269         }
270     }
271
272     /* Read audio */
273     i_size = header[3] | ( header[4] << 8 );
274     if( i_size > 0 )
275     {
276         /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
277         if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
278         {
279             uint8_t h[4];
280             stream_Read( p_demux->s, h, 4 );
281
282             p_sys->fmt_audio.audio.i_channels = h[1];
283             p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
284
285             i_size -= 4;
286         }
287         if( p_sys->p_audio == NULL )
288         {
289             p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
290         }
291
292         if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
293         {
294             p_frame->i_dts =
295             p_frame->i_pts = p_sys->i_pcr;
296             es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
297         }
298     }
299
300     p_sys->i_pcr += p_sys->i_pcr_inc;
301     if( p_sys->i_time >= 0 )
302     {
303         p_sys->i_time += p_sys->i_pcr_inc;
304     }
305
306     return 1;
307 }
308
309 /*****************************************************************************
310  * Control:
311  *****************************************************************************/
312 static int Control( demux_t *p_demux, int i_query, va_list args )
313 {
314     demux_sys_t *p_sys = p_demux->p_sys;
315     double f, *pf;
316     int64_t i64, *pi64;
317
318     switch( i_query )
319     {
320         case DEMUX_GET_POSITION:
321             pf = (double*) va_arg( args, double* );
322             i64 = stream_Size( p_demux->s );
323             if( i64 > 0 )
324             {
325                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
326             }
327             else
328             {
329                 *pf = 0.0;
330             }
331             return VLC_SUCCESS;
332
333         case DEMUX_SET_POSITION:
334             f = (double) va_arg( args, double );
335             i64 = stream_Size( p_demux->s );
336
337             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
338             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
339             {
340                 return VLC_EGENERIC;
341             }
342             p_sys->i_time = -1; /* Invalidate time display */
343             return VLC_SUCCESS;
344
345         case DEMUX_GET_TIME:
346             pi64 = (int64_t*)va_arg( args, int64_t * );
347             if( p_sys->i_time < 0 )
348             {
349                 *pi64 = 0;
350                 return VLC_EGENERIC;
351             }
352             *pi64 = p_sys->i_time;
353             return VLC_SUCCESS;
354
355 #if 0
356         case DEMUX_GET_LENGTH:
357             pi64 = (int64_t*)va_arg( args, int64_t * );
358             if( p_sys->i_mux_rate > 0 )
359             {
360                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
361                 return VLC_SUCCESS;
362             }
363             *pi64 = 0;
364             return VLC_EGENERIC;
365
366 #endif
367         case DEMUX_GET_FPS:
368             pf = (double*)va_arg( args, double * );
369             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
370             return VLC_SUCCESS;
371
372         case DEMUX_SET_TIME:
373         default:
374             return VLC_EGENERIC;
375     }
376 }
377
378 /*****************************************************************************
379  * ReSynch:
380  *****************************************************************************/
381 static int ReSynch( demux_t *p_demux )
382 {
383     const uint8_t *p_peek;
384     int      i_skip;
385     int      i_peek;
386
387     while( !p_demux->b_die )
388     {
389         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
390         {
391             return VLC_EGENERIC;
392         }
393         i_skip = 0;
394
395         while( i_skip < i_peek - 4 )
396         {
397             if( !memcmp( p_peek, "NSVf", 4 )
398              || !memcmp( p_peek, "NSVs", 4 ) )
399             {
400                 if( i_skip > 0 )
401                 {
402                     stream_Read( p_demux->s, NULL, i_skip );
403                 }
404                 return VLC_SUCCESS;
405             }
406             p_peek++;
407             i_skip++;
408         }
409
410         stream_Read( p_demux->s, NULL, i_skip );
411     }
412     return VLC_EGENERIC;
413 }
414
415 /*****************************************************************************
416  * ReadNSVf:
417  *****************************************************************************/
418 static int ReadNSVf( demux_t *p_demux )
419 {
420     /* demux_sys_t *p_sys = p_demux->p_sys; */
421     const uint8_t     *p;
422     int         i_size;
423
424     msg_Dbg( p_demux, "new NSVf chunk" );
425     if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
426     {
427         return VLC_EGENERIC;
428     }
429
430     i_size = GetDWLE( &p[4] );
431     msg_Dbg( p_demux, "    - size=%d", i_size );
432
433     return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
434 }
435 /*****************************************************************************
436  * ReadNSVf:
437  *****************************************************************************/
438 static int ReadNSVs( demux_t *p_demux )
439 {
440     demux_sys_t *p_sys = p_demux->p_sys;
441     uint8_t      header[19];
442     vlc_fourcc_t fcc;
443
444     if( stream_Read( p_demux->s, header, 19 ) < 19 )
445     {
446         msg_Warn( p_demux, "cannot read" );
447         return VLC_EGENERIC;
448     }
449
450     /* Video */
451     switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
452     {
453         case VLC_FOURCC( 'V', 'P', '3', ' ' ):
454         case VLC_FOURCC( 'V', 'P', '3', '1' ):
455             fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
456             break;
457         case VLC_FOURCC( 'V', 'P', '6', '0' ):
458         case VLC_FOURCC( 'V', 'P', '6', '1' ):
459         case VLC_FOURCC( 'V', 'P', '6', '2' ):
460         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
461             break;
462         default:
463             msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
464             break;
465     }
466     if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
467     {
468         es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
469         p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
470         p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
471         if( p_sys->p_video )
472         {
473             es_out_Del( p_demux->out, p_sys->p_video );
474         }
475         p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
476
477         msg_Dbg( p_demux, "    - video `%4.4s' %dx%d",
478                  (char*)&fcc,
479                  p_sys->fmt_video.video.i_width,
480                  p_sys->fmt_video.video.i_height );
481     }
482
483     /* Audio */
484     switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
485     {
486         case VLC_FOURCC( 'M', 'P', '3', ' ' ):
487             fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
488             break;
489         case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
490             fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
491             break;
492         case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
493         case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
494             fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
495             break;
496         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
497             break;
498         default:
499             msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
500             break;
501     }
502
503     if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
504     {
505         msg_Dbg( p_demux, "    - audio `%4.4s'", (char*)&fcc );
506
507         if( p_sys->p_audio )
508         {
509             es_out_Del( p_demux->out, p_sys->p_audio );
510             p_sys->p_audio = NULL;
511         }
512         es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
513     }
514
515     if( header[16]&0x80 )
516     {
517         /* Fractional frame rate */
518         switch( header[16]&0x03 )
519         {
520             case 0: /* 30 fps */
521                 p_sys->i_pcr_inc = 33333; /* 300000/9 */
522                 break;
523             case 1: /* 29.97 fps */
524                 p_sys->i_pcr_inc = 33367; /* 300300/9 */
525                 break;
526             case 2: /* 25 fps */
527                 p_sys->i_pcr_inc = 40000; /* 360000/9 */
528                 break;
529             case 3: /* 23.98 fps */
530                 p_sys->i_pcr_inc = 41700; /* 375300/9 */
531                 break;
532         }
533
534         if( header[16] < 0xc0 )
535             p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
536         else
537             p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
538     }
539     else if( header[16] != 0 )
540     {
541         /* Integer frame rate */
542         p_sys->i_pcr_inc = 1000000 / header[16];
543     }
544     else
545     {
546         msg_Dbg( p_demux, "invalid fps (0x00)" );
547         p_sys->i_pcr_inc = 40000;
548     }
549     //msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
550
551     return VLC_SUCCESS;
552 }
553