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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
37 * - implement NSVf parsing (to get meta data)
38 * - implement missing Control (and in the right way)
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
49 set_description( N_("NullSoft demuxer" ) );
50 set_capability( "demux", 10 );
51 set_category( CAT_INPUT );
52 set_subcategory( SUBCAT_INPUT_DEMUX );
53 set_callbacks( Open, Close );
54 add_shortcut( "nsv" );
57 /*****************************************************************************
59 *****************************************************************************/
63 es_format_t fmt_audio;
66 es_format_t fmt_video;
77 static int Demux ( demux_t *p_demux );
78 static int Control( demux_t *p_demux, int i_query, va_list args );
80 static int ReSynch( demux_t *p_demux );
82 static int ReadNSVf( demux_t *p_demux );
83 static int ReadNSVs( demux_t *p_demux );
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
90 demux_t *p_demux = (demux_t*)p_this;
93 const uint8_t *p_peek;
95 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
98 if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
100 /* In case we had force this demuxer we try to resynch */
101 if( !p_demux->b_force || ReSynch( p_demux ) )
105 /* Fill p_demux field */
106 p_demux->pf_demux = Demux;
107 p_demux->pf_control = Control;
108 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
110 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
111 p_sys->p_audio = NULL;
113 es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
114 p_sys->p_video = NULL;
116 es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
121 p_sys->i_pcr_inc = 0;
126 /*****************************************************************************
128 *****************************************************************************/
129 static void Close( vlc_object_t *p_this )
131 demux_t *p_demux = (demux_t*)p_this;
132 demux_sys_t *p_sys = p_demux->p_sys;
138 /*****************************************************************************
140 *****************************************************************************/
141 static int Demux( demux_t *p_demux )
143 demux_sys_t *p_sys = p_demux->p_sys;
146 const uint8_t *p_peek;
153 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
155 msg_Warn( p_demux, "cannot peek" );
159 if( !memcmp( p_peek, "NSVf", 4 ) )
161 if( ReadNSVf( p_demux ) )
166 else if( !memcmp( p_peek, "NSVs", 4 ) )
168 if( ReadNSVs( p_demux ) )
174 else if( GetWLE( p_peek ) == 0xbeef )
176 /* Next frame of the current NSVs chunk */
177 if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
179 msg_Warn( p_demux, "cannot read" );
186 msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
187 if( ReSynch( p_demux ) )
194 if( stream_Read( p_demux->s, header, 5 ) < 5 )
196 msg_Warn( p_demux, "cannot read" );
201 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
204 i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
208 if( (header[0]&0x0f) != 0x0 )
213 if( stream_Read( p_demux->s, aux, 6 ) < 6 )
215 msg_Warn( p_demux, "cannot read" );
218 i_aux = GetWLE( aux );
219 fcc = VLC_FOURCC( aux[2], aux[3], aux[4], aux[5] );
221 msg_Dbg( p_demux, "Belekas: %d - size=%d fcc=%4.4s",
222 header[0]&0xf, i_aux, (char*)&fcc );
224 if( fcc == VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux > 2 )
226 if( p_sys->p_sub == NULL )
228 p_sys->fmt_sub.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
229 p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
230 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
232 stream_Read( p_demux->s, NULL, 2 );
234 if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
236 uint8_t *p = p_frame->p_buffer;
238 while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
242 if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
244 p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
245 p_frame->p_buffer = p + 1;
248 /* Skip the first part (it is the language name) */
249 p_frame->i_pts = p_sys->i_pcr;
250 p_frame->i_dts = p_sys->i_pcr + 4000000; /* 4s */
252 es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
257 /* We skip this extra data */
258 if( stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
260 msg_Warn( p_demux, "cannot read" );
267 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
268 if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
270 p_frame->i_dts = p_sys->i_pcr;
271 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
276 i_size = header[3] | ( header[4] << 8 );
279 /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
280 if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
283 stream_Read( p_demux->s, h, 4 );
285 p_sys->fmt_audio.audio.i_channels = h[1];
286 p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
290 if( p_sys->p_audio == NULL )
292 p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
295 if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
298 p_frame->i_pts = p_sys->i_pcr;
299 es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
303 p_sys->i_pcr += p_sys->i_pcr_inc;
304 if( p_sys->i_time >= 0 )
306 p_sys->i_time += p_sys->i_pcr_inc;
312 /*****************************************************************************
314 *****************************************************************************/
315 static int Control( demux_t *p_demux, int i_query, va_list args )
317 demux_sys_t *p_sys = p_demux->p_sys;
323 case DEMUX_GET_POSITION:
324 pf = (double*) va_arg( args, double* );
325 i64 = stream_Size( p_demux->s );
328 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
336 case DEMUX_SET_POSITION:
337 f = (double) va_arg( args, double );
338 i64 = stream_Size( p_demux->s );
340 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
341 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
345 p_sys->i_time = -1; /* Invalidate time display */
349 pi64 = (int64_t*)va_arg( args, int64_t * );
350 if( p_sys->i_time < 0 )
355 *pi64 = p_sys->i_time;
359 case DEMUX_GET_LENGTH:
360 pi64 = (int64_t*)va_arg( args, int64_t * );
361 if( p_sys->i_mux_rate > 0 )
363 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
371 pf = (double*)va_arg( args, double * );
372 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
381 /*****************************************************************************
383 *****************************************************************************/
384 static int ReSynch( demux_t *p_demux )
386 const uint8_t *p_peek;
390 while( vlc_object_alive (p_demux) )
392 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
398 while( i_skip < i_peek - 4 )
400 if( !memcmp( p_peek, "NSVf", 4 )
401 || !memcmp( p_peek, "NSVs", 4 ) )
405 stream_Read( p_demux->s, NULL, i_skip );
413 stream_Read( p_demux->s, NULL, i_skip );
418 /*****************************************************************************
420 *****************************************************************************/
421 static int ReadNSVf( demux_t *p_demux )
423 /* demux_sys_t *p_sys = p_demux->p_sys; */
427 msg_Dbg( p_demux, "new NSVf chunk" );
428 if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
433 i_size = GetDWLE( &p[4] );
434 msg_Dbg( p_demux, " - size=%d", i_size );
436 return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
438 /*****************************************************************************
440 *****************************************************************************/
441 static int ReadNSVs( demux_t *p_demux )
443 demux_sys_t *p_sys = p_demux->p_sys;
447 if( stream_Read( p_demux->s, header, 19 ) < 19 )
449 msg_Warn( p_demux, "cannot read" );
454 switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
456 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
457 case VLC_FOURCC( 'V', 'P', '3', '1' ):
458 fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
460 case VLC_FOURCC( 'V', 'P', '6', '0' ):
461 case VLC_FOURCC( 'V', 'P', '6', '1' ):
462 case VLC_FOURCC( 'V', 'P', '6', '2' ):
463 case VLC_FOURCC( 'H', '2', '6', '4' ):
464 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
467 msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
470 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec )
472 es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
473 p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
474 p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
477 es_out_Del( p_demux->out, p_sys->p_video );
479 p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
481 msg_Dbg( p_demux, " - video `%4.4s' %dx%d",
483 p_sys->fmt_video.video.i_width,
484 p_sys->fmt_video.video.i_height );
488 switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
490 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
491 fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
493 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
494 fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
496 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
497 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
498 fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
500 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
503 msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
507 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
509 msg_Dbg( p_demux, " - audio `%4.4s'", (char*)&fcc );
513 es_out_Del( p_demux->out, p_sys->p_audio );
514 p_sys->p_audio = NULL;
516 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
519 if( header[16]&0x80 )
521 /* Fractional frame rate */
522 switch( header[16]&0x03 )
525 p_sys->i_pcr_inc = 33333; /* 300000/9 */
527 case 1: /* 29.97 fps */
528 p_sys->i_pcr_inc = 33367; /* 300300/9 */
531 p_sys->i_pcr_inc = 40000; /* 360000/9 */
533 case 3: /* 23.98 fps */
534 p_sys->i_pcr_inc = 41700; /* 375300/9 */
538 if( header[16] < 0xc0 )
539 p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
541 p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
543 else if( header[16] != 0 )
545 /* Integer frame rate */
546 p_sys->i_pcr_inc = 1000000 / header[16];
550 msg_Dbg( p_demux, "invalid fps (0x00)" );
551 p_sys->i_pcr_inc = 40000;
553 //msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );