]> git.sesse.net Git - vlc/blob - modules/demux/nsv.c
* Stringreview !!!
[vlc] / modules / demux / nsv.c
1 /*****************************************************************************
2  * nsv.c: NullSoft Video demuxer.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: nsv.c,v 1.5 2004/01/25 20:05:28 hartman Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 /* TODO:
33  *  - implement NSVf parsing (to get meta data)
34  *  - implement missing Control (and in the right way)
35  *  - ...
36  */
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open    ( vlc_object_t * );
42 static void Close  ( vlc_object_t * );
43
44 vlc_module_begin();
45     set_description( _("NullSoft demuxer" ) );
46     set_capability( "demux2", 10 );
47     set_callbacks( Open, Close );
48     add_shortcut( "nsv" );
49 vlc_module_end();
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54
55 struct demux_sys_t
56 {
57     es_format_t  fmt_audio;
58     es_out_id_t *p_audio;
59
60     es_format_t  fmt_video;
61     es_out_id_t *p_video;
62
63     int64_t     i_pcr;
64     int64_t     i_time;
65     int64_t     i_pcr_inc;
66 };
67
68 static int Demux  ( demux_t *p_demux );
69 static int Control( demux_t *p_demux, int i_query, va_list args );
70
71 static int ReSynch( demux_t *p_demux );
72
73 static int ReadNSVf( demux_t *p_demux );
74 static int ReadNSVs( demux_t *p_demux );
75
76 /*****************************************************************************
77  * Open
78  *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
80 {
81     demux_t     *p_demux = (demux_t*)p_this;
82     demux_sys_t *p_sys;
83
84     uint8_t     *p_peek;
85     int         i;
86
87     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
88     {
89         msg_Err( p_demux, "cannot peek" );
90         return VLC_EGENERIC;
91     }
92     if( strncmp( p_peek, "NSVf", 4 ) && strncmp( p_peek, "NSVs", 4 ))
93     {
94        /* In case we had force this demuxer we try to resynch */
95         if( strcmp( p_demux->psz_demux, "nsv" ) || ReSynch( p_demux ) )
96         {
97             msg_Warn( p_demux, "NSV module discarded" );
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     p_sys->i_pcr   = 1;
114     p_sys->i_time  = 0;
115     p_sys->i_pcr_inc = 0;
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Close
122  *****************************************************************************/
123 static void Close( vlc_object_t *p_this )
124 {
125     demux_t     *p_demux = (demux_t*)p_this;
126     demux_sys_t *p_sys = p_demux->p_sys;
127
128     free( p_sys );
129 }
130
131
132 /*****************************************************************************
133  * Demux:
134  *****************************************************************************/
135 static int Demux( demux_t *p_demux )
136 {
137     demux_sys_t *p_sys = p_demux->p_sys;
138
139     uint8_t     header[5];
140     uint8_t     *p_peek;
141
142     int         i_size;
143     block_t     *p_frame;
144
145     for( ;; )
146     {
147         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
148         {
149             msg_Warn( p_demux, "cannot peek" );
150             return 0;
151         }
152
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             /* Next frame of the current NSVs chunk */
171             if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
172             {
173                 msg_Warn( p_demux, "cannot read" );
174                 return 0;
175             }
176             break;
177         }
178         else
179         {
180             msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", *(uint32_t*)p_peek, (char*)p_peek );
181             if( ReSynch( p_demux ) )
182             {
183                 return -1;
184             }
185         }
186     }
187
188     if( stream_Read( p_demux->s, header, 5 ) < 5 )
189     {
190         msg_Warn( p_demux, "cannot read" );
191         return 0;
192     }
193
194     /* Set PCR */
195     es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
196
197     /* Read video */
198     i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
199     if( i_size > 0 )
200     {
201         /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
202         if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
203         {
204             p_frame->i_dts = p_sys->i_pcr;
205             es_out_Send( p_demux->out, p_sys->p_video, p_frame );
206         }
207     }
208
209     /* Read audio */
210     i_size = header[3] | ( header[4] << 8 );
211     if( i_size > 0 )
212     {
213         /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
214         if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
215         {
216             uint8_t h[4];
217             stream_Read( p_demux->s, h, 4 );
218
219             p_sys->fmt_audio.audio.i_channels = h[1];
220             p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
221
222             i_size -= 4;
223         }
224         if( p_sys->p_audio == NULL )
225         {
226             p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
227         }
228
229         if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
230         {
231             p_frame->i_dts =
232             p_frame->i_pts = p_sys->i_pcr;
233             es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
234         }
235     }
236
237     p_sys->i_pcr += p_sys->i_pcr_inc;
238     if( p_sys->i_time >= 0 )
239     {
240         p_sys->i_time += p_sys->i_pcr_inc;
241     }
242
243     return 1;
244 }
245
246 /*****************************************************************************
247  * Control:
248  *****************************************************************************/
249 static int Control( demux_t *p_demux, int i_query, va_list args )
250 {
251     demux_sys_t *p_sys = p_demux->p_sys;
252     double f, *pf;
253     int64_t i64, *pi64;
254
255     switch( i_query )
256     {
257         case DEMUX_GET_POSITION:
258             pf = (double*) va_arg( args, double* );
259             i64 = stream_Size( p_demux->s );
260             if( i64 > 0 )
261             {
262                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
263             }
264             else
265             {
266                 *pf = 0.0;
267             }
268             return VLC_SUCCESS;
269
270         case DEMUX_SET_POSITION:
271             f = (double) va_arg( args, double );
272             i64 = stream_Size( p_demux->s );
273
274             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
275             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
276             {
277                 return VLC_EGENERIC;
278             }
279             p_sys->i_time = -1; /* Invalidate time display */
280             return VLC_SUCCESS;
281
282         case DEMUX_GET_TIME:
283             pi64 = (int64_t*)va_arg( args, int64_t * );
284             if( p_sys->i_time < 0 )
285             {
286                 *pi64 = 0;
287                 return VLC_EGENERIC;
288             }
289             *pi64 = p_sys->i_time;
290             return VLC_SUCCESS;
291
292 #if 0
293         case DEMUX_GET_LENGTH:
294             pi64 = (int64_t*)va_arg( args, int64_t * );
295             if( p_sys->i_mux_rate > 0 )
296             {
297                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
298                 return VLC_SUCCESS;
299             }
300             *pi64 = 0;
301             return VLC_EGENERIC;
302
303         case DEMUX_SET_TIME:
304 #endif
305         case DEMUX_GET_FPS:
306             pf = (double*)va_arg( args, double * );
307             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
308             return VLC_SUCCESS;
309
310         default:
311             return VLC_EGENERIC;
312     }
313 }
314
315 /*****************************************************************************
316  * ReSynch:
317  *****************************************************************************/
318 static int ReSynch( demux_t *p_demux )
319 {
320     uint8_t *p_peek;
321     int      i_skip;
322     int      i_peek;
323
324     while( !p_demux->b_die )
325     {
326         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
327         {
328             return VLC_EGENERIC;
329         }
330         i_skip = 0;
331
332         while( i_skip < i_peek - 4 )
333         {
334             if( !strncmp( p_peek, "NSVf", 4 ) || !strncmp( p_peek, "NSVs", 4 ) )
335             {
336                 if( i_skip > 0 )
337                 {
338                     stream_Read( p_demux->s, NULL, i_skip );
339                 }
340                 return VLC_SUCCESS;
341             }
342             p_peek++;
343             i_skip++;
344         }
345
346         stream_Read( p_demux->s, NULL, i_skip );
347     }
348     return VLC_EGENERIC;
349 }
350
351 /*****************************************************************************
352  * ReadNSVf:
353  *****************************************************************************/
354 static int ReadNSVf( demux_t *p_demux )
355 {
356     demux_sys_t *p_sys = p_demux->p_sys;
357     uint8_t     *p;
358     int         i_size;
359
360     msg_Dbg( p_demux, "new NSVf chunk" );
361     if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
362     {
363         return VLC_EGENERIC;
364     }
365
366     i_size = GetDWLE( &p[4] );
367     msg_Dbg( p_demux, "    - size=%d", i_size );
368
369     return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
370 }
371 /*****************************************************************************
372  * ReadNSVf:
373  *****************************************************************************/
374 static int ReadNSVs( demux_t *p_demux )
375 {
376     demux_sys_t *p_sys = p_demux->p_sys;
377     uint8_t      header[19];
378     vlc_fourcc_t fcc;
379
380     if( stream_Read( p_demux->s, header, 19 ) < 19 )
381     {
382         msg_Warn( p_demux, "cannot read" );
383         return VLC_EGENERIC;
384     }
385
386     msg_Dbg( p_demux, "new NSVs chunk" );
387     /* Video */
388     switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
389     {
390         case VLC_FOURCC( 'V', 'P', '3', ' ' ):
391         case VLC_FOURCC( 'V', 'P', '3', '1' ):
392             fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
393             break;
394         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
395             break;
396         default:
397             msg_Warn( p_demux, "unknown codec" );
398             break;
399     }
400     if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
401     {
402         es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
403         p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
404         p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
405         if( p_sys->p_video )
406         {
407             es_out_Del( p_demux->out, p_sys->p_video );
408         }
409         p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
410
411         msg_Dbg( p_demux, "    - video `%4.4s' %dx%d",
412                  (char*)&fcc,
413                  p_sys->fmt_video.video.i_width,
414                  p_sys->fmt_video.video.i_height );
415     }
416
417     /* Audio */
418     switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
419     {
420         case VLC_FOURCC( 'M', 'P', '3', ' ' ):
421             fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
422             break;
423         case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
424             fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
425             break;
426         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
427             break;
428         default:
429             msg_Warn( p_demux, "unknown codec" );
430             break;
431     }
432
433     if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
434     {
435         msg_Dbg( p_demux, "    - audio `%4.4s'", (char*)&fcc );
436
437         if( p_sys->p_audio )
438         {
439             es_out_Del( p_demux->out, p_sys->p_audio );
440             p_sys->p_audio = NULL;
441         }
442         es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
443     }
444
445     if( header[16]&0x80 )
446     {
447         switch( header[16]&0x7f )
448         {
449             case 1: /* 29.97 fps */
450                 p_sys->i_pcr_inc = 33367;
451                 break;
452             case 3: /* 23.976 fps */
453                 p_sys->i_pcr_inc = 41708;
454                 break;
455             case 5: /* 14.98 fps */
456                 p_sys->i_pcr_inc = 66755;
457                 break;
458             default:
459                 msg_Dbg( p_demux, "unknown fps (0x%x)", header[16] );
460                 p_sys->i_pcr_inc = 40000;
461                 break;
462         }
463     }
464     else if( header[16] != 0 )
465     {
466         p_sys->i_pcr_inc = 1000000 / header[16];
467     }
468     else
469     {
470         msg_Dbg( p_demux, "invalid fps (0x00)" );
471         p_sys->i_pcr_inc = 40000;
472     }
473     msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
474
475     return VLC_SUCCESS;
476 }
477