1 /*****************************************************************************
2 * nsv.c: NullSoft Video demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_demux.h>
32 * - implement NSVf parsing (to get meta data)
33 * - implement missing Control (and in the right way)
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
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" );
52 /*****************************************************************************
54 *****************************************************************************/
58 es_format_t fmt_audio;
61 es_format_t fmt_video;
72 static int Demux ( demux_t *p_demux );
73 static int Control( demux_t *p_demux, int i_query, va_list args );
75 static int ReSynch( demux_t *p_demux );
77 static int ReadNSVf( demux_t *p_demux );
78 static int ReadNSVs( demux_t *p_demux );
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
85 demux_t *p_demux = (demux_t*)p_this;
88 const uint8_t *p_peek;
90 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
93 if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
95 /* In case we had force this demuxer we try to resynch */
96 if( !p_demux->b_force || ReSynch( p_demux ) )
100 /* Fill p_demux field */
101 p_demux->pf_demux = Demux;
102 p_demux->pf_control = Control;
103 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
105 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
106 p_sys->p_audio = NULL;
108 es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
109 p_sys->p_video = NULL;
111 es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
116 p_sys->i_pcr_inc = 0;
121 /*****************************************************************************
123 *****************************************************************************/
124 static void Close( vlc_object_t *p_this )
126 demux_t *p_demux = (demux_t*)p_this;
127 demux_sys_t *p_sys = p_demux->p_sys;
133 /*****************************************************************************
135 *****************************************************************************/
136 static int Demux( demux_t *p_demux )
138 demux_sys_t *p_sys = p_demux->p_sys;
141 const uint8_t *p_peek;
148 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
150 msg_Warn( p_demux, "cannot peek" );
154 if( !memcmp( p_peek, "NSVf", 4 ) )
156 if( ReadNSVf( p_demux ) )
161 else if( !memcmp( p_peek, "NSVs", 4 ) )
163 if( ReadNSVs( p_demux ) )
169 else if( GetWLE( p_peek ) == 0xbeef )
171 /* Next frame of the current NSVs chunk */
172 if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
174 msg_Warn( p_demux, "cannot read" );
181 msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
182 if( ReSynch( p_demux ) )
189 if( stream_Read( p_demux->s, header, 5 ) < 5 )
191 msg_Warn( p_demux, "cannot read" );
196 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
199 i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
203 if( (header[0]&0x0f) != 0x0 )
208 if( stream_Read( p_demux->s, aux, 6 ) < 6 )
210 msg_Warn( p_demux, "cannot read" );
213 i_aux = GetWLE( aux );
214 fcc = VLC_FOURCC( aux[2], aux[3], aux[4], aux[5] );
216 msg_Dbg( p_demux, "Belekas: %d - size=%d fcc=%4.4s",
217 header[0]&0xf, i_aux, (char*)&fcc );
219 if( fcc == VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux > 2 )
221 if( p_sys->p_sub == NULL )
223 p_sys->fmt_sub.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
224 p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
225 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
227 stream_Read( p_demux->s, NULL, 2 );
229 if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
231 uint8_t *p = p_frame->p_buffer;
233 while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
237 if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
239 p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
240 p_frame->p_buffer = p + 1;
243 /* Skip the first part (it is the language name) */
244 p_frame->i_pts = p_sys->i_pcr;
245 p_frame->i_dts = p_sys->i_pcr + 4000000; /* 4s */
247 es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
252 /* We skip this extra data */
253 if( stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
255 msg_Warn( p_demux, "cannot read" );
262 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
263 if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
265 p_frame->i_dts = p_sys->i_pcr;
266 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
271 i_size = header[3] | ( header[4] << 8 );
274 /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
275 if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
278 stream_Read( p_demux->s, h, 4 );
280 p_sys->fmt_audio.audio.i_channels = h[1];
281 p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
285 if( p_sys->p_audio == NULL )
287 p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
290 if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
293 p_frame->i_pts = p_sys->i_pcr;
294 es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
298 p_sys->i_pcr += p_sys->i_pcr_inc;
299 if( p_sys->i_time >= 0 )
301 p_sys->i_time += p_sys->i_pcr_inc;
307 /*****************************************************************************
309 *****************************************************************************/
310 static int Control( demux_t *p_demux, int i_query, va_list args )
312 demux_sys_t *p_sys = p_demux->p_sys;
318 case DEMUX_GET_POSITION:
319 pf = (double*) va_arg( args, double* );
320 i64 = stream_Size( p_demux->s );
323 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
331 case DEMUX_SET_POSITION:
332 f = (double) va_arg( args, double );
333 i64 = stream_Size( p_demux->s );
335 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
336 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
340 p_sys->i_time = -1; /* Invalidate time display */
344 pi64 = (int64_t*)va_arg( args, int64_t * );
345 if( p_sys->i_time < 0 )
350 *pi64 = p_sys->i_time;
354 case DEMUX_GET_LENGTH:
355 pi64 = (int64_t*)va_arg( args, int64_t * );
356 if( p_sys->i_mux_rate > 0 )
358 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
366 pf = (double*)va_arg( args, double * );
367 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
376 /*****************************************************************************
378 *****************************************************************************/
379 static int ReSynch( demux_t *p_demux )
381 const uint8_t *p_peek;
385 while( !p_demux->b_die )
387 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
393 while( i_skip < i_peek - 4 )
395 if( !memcmp( p_peek, "NSVf", 4 )
396 || !memcmp( p_peek, "NSVs", 4 ) )
400 stream_Read( p_demux->s, NULL, i_skip );
408 stream_Read( p_demux->s, NULL, i_skip );
413 /*****************************************************************************
415 *****************************************************************************/
416 static int ReadNSVf( demux_t *p_demux )
418 /* demux_sys_t *p_sys = p_demux->p_sys; */
422 msg_Dbg( p_demux, "new NSVf chunk" );
423 if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
428 i_size = GetDWLE( &p[4] );
429 msg_Dbg( p_demux, " - size=%d", i_size );
431 return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
433 /*****************************************************************************
435 *****************************************************************************/
436 static int ReadNSVs( demux_t *p_demux )
438 demux_sys_t *p_sys = p_demux->p_sys;
442 if( stream_Read( p_demux->s, header, 19 ) < 19 )
444 msg_Warn( p_demux, "cannot read" );
449 switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
451 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
452 case VLC_FOURCC( 'V', 'P', '3', '1' ):
453 fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
455 case VLC_FOURCC( 'V', 'P', '6', '0' ):
456 case VLC_FOURCC( 'V', 'P', '6', '1' ):
457 case VLC_FOURCC( 'V', 'P', '6', '2' ):
458 case VLC_FOURCC( 'H', '2', '6', '4' ):
459 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
462 msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
465 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec )
467 es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
468 p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
469 p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
472 es_out_Del( p_demux->out, p_sys->p_video );
474 p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
476 msg_Dbg( p_demux, " - video `%4.4s' %dx%d",
478 p_sys->fmt_video.video.i_width,
479 p_sys->fmt_video.video.i_height );
483 switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
485 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
486 fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
488 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
489 fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
491 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
492 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
493 fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
495 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
498 msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
502 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
504 msg_Dbg( p_demux, " - audio `%4.4s'", (char*)&fcc );
508 es_out_Del( p_demux->out, p_sys->p_audio );
509 p_sys->p_audio = NULL;
511 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
514 if( header[16]&0x80 )
516 /* Fractional frame rate */
517 switch( header[16]&0x03 )
520 p_sys->i_pcr_inc = 33333; /* 300000/9 */
522 case 1: /* 29.97 fps */
523 p_sys->i_pcr_inc = 33367; /* 300300/9 */
526 p_sys->i_pcr_inc = 40000; /* 360000/9 */
528 case 3: /* 23.98 fps */
529 p_sys->i_pcr_inc = 41700; /* 375300/9 */
533 if( header[16] < 0xc0 )
534 p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
536 p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
538 else if( header[16] != 0 )
540 /* Integer frame rate */
541 p_sys->i_pcr_inc = 1000000 / header[16];
545 msg_Dbg( p_demux, "invalid fps (0x00)" );
546 p_sys->i_pcr_inc = 40000;
548 //msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );