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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc_demux.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_category( CAT_INPUT );
48 set_subcategory( SUBCAT_INPUT_DEMUX );
49 set_callbacks( Open, Close );
50 add_shortcut( "nsv" );
53 /*****************************************************************************
55 *****************************************************************************/
59 es_format_t fmt_audio;
62 es_format_t fmt_video;
73 static int Demux ( demux_t *p_demux );
74 static int Control( demux_t *p_demux, int i_query, va_list args );
76 static int ReSynch( demux_t *p_demux );
78 static int ReadNSVf( demux_t *p_demux );
79 static int ReadNSVs( demux_t *p_demux );
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
86 demux_t *p_demux = (demux_t*)p_this;
89 const uint8_t *p_peek;
91 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
94 if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
96 /* In case we had force this demuxer we try to resynch */
97 if( strcmp( p_demux->psz_demux, "nsv" ) || ReSynch( p_demux ) )
103 /* Fill p_demux field */
104 p_demux->pf_demux = Demux;
105 p_demux->pf_control = Control;
106 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
108 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
109 p_sys->p_audio = NULL;
111 es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
112 p_sys->p_video = NULL;
114 es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
119 p_sys->i_pcr_inc = 0;
124 /*****************************************************************************
126 *****************************************************************************/
127 static void Close( vlc_object_t *p_this )
129 demux_t *p_demux = (demux_t*)p_this;
130 demux_sys_t *p_sys = p_demux->p_sys;
136 /*****************************************************************************
138 *****************************************************************************/
139 static int Demux( demux_t *p_demux )
141 demux_sys_t *p_sys = p_demux->p_sys;
144 const uint8_t *p_peek;
151 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
153 msg_Warn( p_demux, "cannot peek" );
157 if( !memcmp( p_peek, "NSVf", 4 ) )
159 if( ReadNSVf( p_demux ) )
164 else if( !memcmp( p_peek, "NSVs", 4 ) )
166 if( ReadNSVs( p_demux ) )
172 else if( GetWLE( p_peek ) == 0xbeef )
174 /* Next frame of the current NSVs chunk */
175 if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
177 msg_Warn( p_demux, "cannot read" );
184 msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
185 if( ReSynch( p_demux ) )
192 if( stream_Read( p_demux->s, header, 5 ) < 5 )
194 msg_Warn( p_demux, "cannot read" );
199 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
202 i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
206 if( (header[0]&0x0f) != 0x0 )
211 if( stream_Read( p_demux->s, aux, 6 ) < 6 )
213 msg_Warn( p_demux, "cannot read" );
216 i_aux = GetWLE( aux );
217 fcc = VLC_FOURCC( aux[2], aux[3], aux[4], aux[5] );
219 msg_Dbg( p_demux, "Belekas: %d - size=%d fcc=%4.4s",
220 header[0]&0xf, i_aux, (char*)&fcc );
222 if( fcc == VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux > 2 )
224 if( p_sys->p_sub == NULL )
226 p_sys->fmt_sub.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
227 p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
228 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
230 stream_Read( p_demux->s, NULL, 2 );
232 if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
234 uint8_t *p = p_frame->p_buffer;
236 while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
240 if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
242 p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
243 p_frame->p_buffer = p + 1;
246 /* Skip the first part (it is the language name) */
247 p_frame->i_pts = p_sys->i_pcr;
248 p_frame->i_dts = p_sys->i_pcr + 4000000; /* 4s */
250 es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
255 /* We skip this extra data */
256 if( stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
258 msg_Warn( p_demux, "cannot read" );
265 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
266 if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
268 p_frame->i_dts = p_sys->i_pcr;
269 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
274 i_size = header[3] | ( header[4] << 8 );
277 /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
278 if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
281 stream_Read( p_demux->s, h, 4 );
283 p_sys->fmt_audio.audio.i_channels = h[1];
284 p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
288 if( p_sys->p_audio == NULL )
290 p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
293 if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
296 p_frame->i_pts = p_sys->i_pcr;
297 es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
301 p_sys->i_pcr += p_sys->i_pcr_inc;
302 if( p_sys->i_time >= 0 )
304 p_sys->i_time += p_sys->i_pcr_inc;
310 /*****************************************************************************
312 *****************************************************************************/
313 static int Control( demux_t *p_demux, int i_query, va_list args )
315 demux_sys_t *p_sys = p_demux->p_sys;
321 case DEMUX_GET_POSITION:
322 pf = (double*) va_arg( args, double* );
323 i64 = stream_Size( p_demux->s );
326 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
334 case DEMUX_SET_POSITION:
335 f = (double) va_arg( args, double );
336 i64 = stream_Size( p_demux->s );
338 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
339 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
343 p_sys->i_time = -1; /* Invalidate time display */
347 pi64 = (int64_t*)va_arg( args, int64_t * );
348 if( p_sys->i_time < 0 )
353 *pi64 = p_sys->i_time;
357 case DEMUX_GET_LENGTH:
358 pi64 = (int64_t*)va_arg( args, int64_t * );
359 if( p_sys->i_mux_rate > 0 )
361 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
369 pf = (double*)va_arg( args, double * );
370 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
379 /*****************************************************************************
381 *****************************************************************************/
382 static int ReSynch( demux_t *p_demux )
384 const uint8_t *p_peek;
388 while( !p_demux->b_die )
390 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
396 while( i_skip < i_peek - 4 )
398 if( !memcmp( p_peek, "NSVf", 4 )
399 || !memcmp( p_peek, "NSVs", 4 ) )
403 stream_Read( p_demux->s, NULL, i_skip );
411 stream_Read( p_demux->s, NULL, i_skip );
416 /*****************************************************************************
418 *****************************************************************************/
419 static int ReadNSVf( demux_t *p_demux )
421 /* demux_sys_t *p_sys = p_demux->p_sys; */
425 msg_Dbg( p_demux, "new NSVf chunk" );
426 if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
431 i_size = GetDWLE( &p[4] );
432 msg_Dbg( p_demux, " - size=%d", i_size );
434 return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
436 /*****************************************************************************
438 *****************************************************************************/
439 static int ReadNSVs( demux_t *p_demux )
441 demux_sys_t *p_sys = p_demux->p_sys;
445 if( stream_Read( p_demux->s, header, 19 ) < 19 )
447 msg_Warn( p_demux, "cannot read" );
452 switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
454 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
455 case VLC_FOURCC( 'V', 'P', '3', '1' ):
456 fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
458 case VLC_FOURCC( 'V', 'P', '6', '0' ):
459 case VLC_FOURCC( 'V', 'P', '6', '1' ):
460 case VLC_FOURCC( 'V', 'P', '6', '2' ):
461 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
464 msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
467 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec )
469 es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
470 p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
471 p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
474 es_out_Del( p_demux->out, p_sys->p_video );
476 p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
478 msg_Dbg( p_demux, " - video `%4.4s' %dx%d",
480 p_sys->fmt_video.video.i_width,
481 p_sys->fmt_video.video.i_height );
485 switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
487 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
488 fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
490 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
491 fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
493 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
494 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
495 fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
497 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
500 msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
504 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
506 msg_Dbg( p_demux, " - audio `%4.4s'", (char*)&fcc );
510 es_out_Del( p_demux->out, p_sys->p_audio );
511 p_sys->p_audio = NULL;
513 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
516 if( header[16]&0x80 )
518 /* Fractional frame rate */
519 switch( header[16]&0x03 )
522 p_sys->i_pcr_inc = 33333; /* 300000/9 */
524 case 1: /* 29.97 fps */
525 p_sys->i_pcr_inc = 33367; /* 300300/9 */
528 p_sys->i_pcr_inc = 40000; /* 360000/9 */
530 case 3: /* 23.98 fps */
531 p_sys->i_pcr_inc = 41700; /* 375300/9 */
535 if( header[16] < 0xc0 )
536 p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
538 p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
540 else if( header[16] != 0 )
542 /* Integer frame rate */
543 p_sys->i_pcr_inc = 1000000 / header[16];
547 msg_Dbg( p_demux, "invalid fps (0x00)" );
548 p_sys->i_pcr_inc = 40000;
550 //msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );