1 /*****************************************************************************
2 * nsv.c: NullSoft Video demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: nsv.c,v 1.4 2004/01/07 15:31:31 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
33 * - implement NSVf parsing (to get meta data)
34 * - implement missing Control (and in the right way)
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
45 set_description( _("NullSoft demuxer" ) );
46 set_capability( "demux2", 10 );
47 set_callbacks( Open, Close );
48 add_shortcut( "nsv" );
51 /*****************************************************************************
53 *****************************************************************************/
57 es_format_t fmt_audio;
60 es_format_t fmt_video;
68 static int Demux ( demux_t *p_demux );
69 static int Control( demux_t *p_demux, int i_query, va_list args );
71 static int ReSynch( demux_t *p_demux );
73 static int ReadNSVf( demux_t *p_demux );
74 static int ReadNSVs( demux_t *p_demux );
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
81 demux_t *p_demux = (demux_t*)p_this;
87 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
89 msg_Err( p_demux, "cannot peek" );
92 if( strncmp( p_peek, "NSVf", 4 ) && strncmp( p_peek, "NSVs", 4 ))
94 /* In case we had force this demuxer we try to resynch */
95 if( strcmp( p_demux->psz_demux, "nsv" ) || ReSynch( p_demux ) )
97 msg_Warn( p_demux, "NSV module discarded" );
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 ) );
107 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
108 p_sys->p_audio = NULL;
110 es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
111 p_sys->p_video = NULL;
115 p_sys->i_pcr_inc = 0;
120 /*****************************************************************************
122 *****************************************************************************/
123 static void Close( vlc_object_t *p_this )
125 demux_t *p_demux = (demux_t*)p_this;
126 demux_sys_t *p_sys = p_demux->p_sys;
132 /*****************************************************************************
134 *****************************************************************************/
135 static int Demux( demux_t *p_demux )
137 demux_sys_t *p_sys = p_demux->p_sys;
147 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
149 msg_Warn( p_demux, "cannot peek" );
153 if( !strncmp( p_peek, "NSVf", 4 ) )
155 if( ReadNSVf( p_demux ) )
160 else if( !strncmp( p_peek, "NSVs", 4 ) )
162 if( ReadNSVs( p_demux ) )
168 else if( GetWLE( p_peek ) == 0xbeef )
170 /* Next frame of the current NSVs chunk */
171 if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
173 msg_Warn( p_demux, "cannot read" );
180 msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", *(uint32_t*)p_peek, (char*)p_peek );
181 if( ReSynch( p_demux ) )
188 if( stream_Read( p_demux->s, header, 5 ) < 5 )
190 msg_Warn( p_demux, "cannot read" );
195 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
198 i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
201 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
202 if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
204 p_frame->i_dts = p_sys->i_pcr;
205 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
210 i_size = header[3] | ( header[4] << 8 );
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' ) )
217 stream_Read( p_demux->s, h, 4 );
219 p_sys->fmt_audio.audio.i_channels = h[1];
220 p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
224 if( p_sys->p_audio == NULL )
226 p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
229 if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
232 p_frame->i_pts = p_sys->i_pcr;
233 es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
237 p_sys->i_pcr += p_sys->i_pcr_inc;
238 if( p_sys->i_time >= 0 )
240 p_sys->i_time += p_sys->i_pcr_inc;
246 /*****************************************************************************
248 *****************************************************************************/
249 static int Control( demux_t *p_demux, int i_query, va_list args )
251 demux_sys_t *p_sys = p_demux->p_sys;
257 case DEMUX_GET_POSITION:
258 pf = (double*) va_arg( args, double* );
259 i64 = stream_Size( p_demux->s );
262 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
270 case DEMUX_SET_POSITION:
271 f = (double) va_arg( args, double );
272 i64 = stream_Size( p_demux->s );
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 ) )
279 p_sys->i_time = -1; /* Invalidate time display */
283 pi64 = (int64_t*)va_arg( args, int64_t * );
284 if( p_sys->i_time < 0 )
289 *pi64 = p_sys->i_time;
293 case DEMUX_GET_LENGTH:
294 pi64 = (int64_t*)va_arg( args, int64_t * );
295 if( p_sys->i_mux_rate > 0 )
297 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
306 pf = (double*)va_arg( args, double * );
307 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
315 /*****************************************************************************
317 *****************************************************************************/
318 static int ReSynch( demux_t *p_demux )
324 while( !p_demux->b_die )
326 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
332 while( i_skip < i_peek - 4 )
334 if( !strncmp( p_peek, "NSVf", 4 ) || !strncmp( p_peek, "NSVs", 4 ) )
338 stream_Read( p_demux->s, NULL, i_skip );
346 stream_Read( p_demux->s, NULL, i_skip );
351 /*****************************************************************************
353 *****************************************************************************/
354 static int ReadNSVf( demux_t *p_demux )
356 demux_sys_t *p_sys = p_demux->p_sys;
360 msg_Dbg( p_demux, "New NSVf chunk" );
361 if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
366 i_size = GetDWLE( &p[4] );
367 msg_Dbg( p_demux, " - size=%d", i_size );
369 return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
371 /*****************************************************************************
373 *****************************************************************************/
374 static int ReadNSVs( demux_t *p_demux )
376 demux_sys_t *p_sys = p_demux->p_sys;
380 if( stream_Read( p_demux->s, header, 19 ) < 19 )
382 msg_Warn( p_demux, "cannot read" );
386 msg_Dbg( p_demux, "New NSVs chunk" );
388 switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
390 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
391 case VLC_FOURCC( 'V', 'P', '3', '1' ):
392 fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
394 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
397 msg_Warn( p_demux, "unknow codec" );
400 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec )
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] );
407 es_out_Del( p_demux->out, p_sys->p_video );
409 p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
411 msg_Dbg( p_demux, " - video `%4.4s' %dx%d",
413 p_sys->fmt_video.video.i_width,
414 p_sys->fmt_video.video.i_height );
418 switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
420 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
421 fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
423 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
424 fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
426 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
429 msg_Warn( p_demux, "unknow codec" );
433 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
435 msg_Dbg( p_demux, " - audio `%4.4s'", (char*)&fcc );
439 es_out_Del( p_demux->out, p_sys->p_audio );
440 p_sys->p_audio = NULL;
442 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
445 if( header[16]&0x80 )
447 switch( header[16]&0x7f )
449 case 1: /* 29.97 fps */
450 p_sys->i_pcr_inc = 33367;
452 case 3: /* 23.976 fps */
453 p_sys->i_pcr_inc = 41708;
455 case 5: /* 14.98 fps */
456 p_sys->i_pcr_inc = 66755;
459 msg_Dbg( p_demux, "unknow fps (0x%x)", header[16] );
460 p_sys->i_pcr_inc = 40000;
464 else if( header[16] != 0 )
466 p_sys->i_pcr_inc = 1000000 / header[16];
470 msg_Dbg( p_demux, "invalid fps (0x00)" );
471 p_sys->i_pcr_inc = 40000;
473 msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );