1 /*****************************************************************************
2 * nsv.c: NullSoft Video demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004-2007 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
38 * - implement NSVf parsing (to get meta data)
39 * - implement missing Control (and in the right way)
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t
* );
47 static void Close ( vlc_object_t
* );
50 set_description( N_("NullSoft demuxer" ) )
51 set_capability( "demux", 10 )
52 set_category( CAT_INPUT
)
53 set_subcategory( SUBCAT_INPUT_DEMUX
)
54 set_callbacks( Open
, Close
)
56 add_file_extension("nsv")
59 /*****************************************************************************
61 *****************************************************************************/
65 es_format_t fmt_audio
;
68 es_format_t fmt_video
;
81 static int Demux ( demux_t
*p_demux
);
82 static int Control( demux_t
*p_demux
, int i_query
, va_list args
);
84 static int ReSynch( demux_t
*p_demux
);
86 static int ReadNSVf( demux_t
*p_demux
);
87 static int ReadNSVs( demux_t
*p_demux
);
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Open( vlc_object_t
*p_this
)
94 demux_t
*p_demux
= (demux_t
*)p_this
;
97 const uint8_t *p_peek
;
99 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 )
102 if( memcmp( p_peek
, "NSVf", 4 ) && memcmp( p_peek
, "NSVs", 4 ) )
104 /* In case we had force this demuxer we try to resynch */
105 if( !p_demux
->obj
.force
|| ReSynch( p_demux
) )
109 p_sys
= malloc( sizeof( demux_sys_t
) );
110 if( unlikely(p_sys
== NULL
) )
113 /* Fill p_demux field */
114 p_demux
->p_sys
= p_sys
;
115 p_demux
->pf_demux
= Demux
;
116 p_demux
->pf_control
= Control
;
118 es_format_Init( &p_sys
->fmt_audio
, AUDIO_ES
, 0 );
119 p_sys
->p_audio
= NULL
;
121 es_format_Init( &p_sys
->fmt_video
, VIDEO_ES
, 0 );
122 p_sys
->p_video
= NULL
;
124 es_format_Init( &p_sys
->fmt_sub
, SPU_ES
, 0 );
129 p_sys
->i_pcr_inc
= 0;
131 p_sys
->b_start_record
= false;
136 /*****************************************************************************
138 *****************************************************************************/
139 static void Close( vlc_object_t
*p_this
)
141 demux_t
*p_demux
= (demux_t
*)p_this
;
142 demux_sys_t
*p_sys
= p_demux
->p_sys
;
148 /*****************************************************************************
150 *****************************************************************************/
151 static int Demux( demux_t
*p_demux
)
153 demux_sys_t
*p_sys
= p_demux
->p_sys
;
156 const uint8_t *p_peek
;
163 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 )
164 return VLC_DEMUXER_EOF
;
166 if( !memcmp( p_peek
, "NSVf", 4 ) )
168 if( ReadNSVf( p_demux
) )
169 return VLC_DEMUXER_EGENERIC
;
171 else if( !memcmp( p_peek
, "NSVs", 4 ) )
173 if( p_sys
->b_start_record
)
175 /* Enable recording once synchronized */
176 vlc_stream_Control( p_demux
->s
, STREAM_SET_RECORD_STATE
, true, "nsv" );
177 p_sys
->b_start_record
= false;
180 if( ReadNSVs( p_demux
) )
181 return VLC_DEMUXER_EGENERIC
;
184 else if( GetWLE( p_peek
) == 0xbeef )
186 /* Next frame of the current NSVs chunk */
187 if( vlc_stream_Read( p_demux
->s
, NULL
, 2 ) < 2 )
189 msg_Warn( p_demux
, "cannot read" );
190 return VLC_DEMUXER_EOF
;
196 msg_Err( p_demux
, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek
), (const char*)p_peek
);
197 if( ReSynch( p_demux
) )
198 return VLC_DEMUXER_EGENERIC
;
202 if( vlc_stream_Read( p_demux
->s
, header
, 5 ) < 5 )
204 msg_Warn( p_demux
, "cannot read" );
205 return VLC_DEMUXER_EOF
;
209 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+ p_sys
->i_pcr
);
212 i_size
= ( header
[0] >> 4 ) | ( header
[1] << 4 ) | ( header
[2] << 12 );
216 if( (header
[0]&0x0f) != 0x0 )
221 if( vlc_stream_Read( p_demux
->s
, aux
, 6 ) < 6 )
223 msg_Warn( p_demux
, "cannot read" );
224 return VLC_DEMUXER_EOF
;
226 i_aux
= GetWLE( aux
);
227 fcc
= VLC_FOURCC( aux
[2], aux
[3], aux
[4], aux
[5] );
229 msg_Dbg( p_demux
, "Belekas: %d - size=%d fcc=%4.4s",
230 header
[0]&0xf, i_aux
, (char*)&fcc
);
232 if( fcc
== VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux
> 2 )
234 if( p_sys
->p_sub
== NULL
)
236 p_sys
->fmt_sub
.i_codec
= VLC_FOURCC( 's', 'u', 'b', 't' );
237 p_sys
->p_sub
= es_out_Add( p_demux
->out
, &p_sys
->fmt_sub
);
239 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_sys
->p_sub
);
241 if( vlc_stream_Read( p_demux
->s
, NULL
, 2 ) < 2 )
242 return VLC_DEMUXER_EOF
;
244 if( ( p_frame
= vlc_stream_Block( p_demux
->s
, i_aux
- 2 ) ) )
246 uint8_t *p
= p_frame
->p_buffer
;
248 while( p
< &p_frame
->p_buffer
[p_frame
->i_buffer
] && *p
!= 0 )
252 if( *p
== 0 && p
+ 1 < &p_frame
->p_buffer
[p_frame
->i_buffer
] )
254 p_frame
->i_buffer
-= p
+ 1 - p_frame
->p_buffer
;
255 p_frame
->p_buffer
= p
+ 1;
258 /* Skip the first part (it is the language name) */
259 p_frame
->i_pts
= VLC_TICK_0
+ p_sys
->i_pcr
;
260 p_frame
->i_dts
= VLC_TICK_0
+ p_sys
->i_pcr
+ VLC_TICK_FROM_SEC(4);
263 es_out_Send( p_demux
->out
, p_sys
->p_sub
, p_frame
);
265 block_Release( p_frame
);
270 /* We skip this extra data */
271 if( vlc_stream_Read( p_demux
->s
, NULL
, i_aux
) < i_aux
)
273 msg_Warn( p_demux
, "cannot read" );
274 return VLC_DEMUXER_EOF
;
280 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
281 if( i_size
> 0 && ( p_frame
= vlc_stream_Block( p_demux
->s
, i_size
) ) )
283 p_frame
->i_dts
= VLC_TICK_0
+ p_sys
->i_pcr
;
286 es_out_Send( p_demux
->out
, p_sys
->p_video
, p_frame
);
289 block_Release( p_frame
);
290 msg_Dbg( p_demux
, "ignoring unsupported video frame (size=%d)",
297 i_size
= header
[3] | ( header
[4] << 8 );
300 /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
301 if( p_sys
->fmt_audio
.i_codec
== VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
304 if( vlc_stream_Read( p_demux
->s
, h
, 4 ) < 4 )
305 return VLC_DEMUXER_EOF
;
307 p_sys
->fmt_audio
.audio
.i_channels
= h
[1];
308 p_sys
->fmt_audio
.audio
.i_rate
= GetWLE( &h
[2] );
312 if( p_sys
->p_audio
== NULL
)
314 p_sys
->p_audio
= es_out_Add( p_demux
->out
, &p_sys
->fmt_audio
);
317 if( ( p_frame
= vlc_stream_Block( p_demux
->s
, i_size
) ) )
320 p_frame
->i_pts
= VLC_TICK_0
+ p_sys
->i_pcr
;
323 es_out_Send( p_demux
->out
, p_sys
->p_audio
, p_frame
);
326 block_Release( p_frame
);
327 msg_Dbg( p_demux
, "ignoring unsupported audio frame (size=%d)",
333 p_sys
->i_pcr
+= p_sys
->i_pcr_inc
;
334 if( p_sys
->i_time
>= 0 )
336 p_sys
->i_time
+= p_sys
->i_pcr_inc
;
339 return VLC_DEMUXER_SUCCESS
;
342 /*****************************************************************************
344 *****************************************************************************/
345 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
347 demux_sys_t
*p_sys
= p_demux
->p_sys
;
349 bool b_bool
, *pb_bool
;
355 return vlc_stream_vaControl( p_demux
->s
, i_query
, args
);
357 case DEMUX_GET_POSITION
:
358 pf
= va_arg( args
, double * );
359 i64
= stream_Size( p_demux
->s
);
362 double current
= vlc_stream_Tell( p_demux
->s
);
363 *pf
= current
/ (double)i64
;
371 case DEMUX_SET_POSITION
:
372 f
= va_arg( args
, double );
373 i64
= stream_Size( p_demux
->s
);
375 if( vlc_stream_Seek( p_demux
->s
, (int64_t)(i64
* f
) ) || ReSynch( p_demux
) )
378 p_sys
->i_time
= -1; /* Invalidate time display */
382 if( p_sys
->i_time
< 0 )
384 *va_arg( args
, vlc_tick_t
* ) = 0;
387 *va_arg( args
, vlc_tick_t
* ) = p_sys
->i_time
;
391 case DEMUX_GET_LENGTH
:
392 if( p_sys
->i_mux_rate
> 0 )
394 *va_arg( args
, vlc_tick_t
* ) = vlc_tick_from_samples( stream_Size( p_demux
->s
) / 50, p_sys
->i_mux_rate
);
397 *va_arg( args
, vlc_tick_t
* ) = 0;
402 pf
= va_arg( args
, double * );
403 *pf
= (double)CLOCK_FREQ
/ (double)p_sys
->i_pcr_inc
;
406 case DEMUX_CAN_RECORD
:
407 pb_bool
= va_arg( args
, bool * );
411 case DEMUX_SET_RECORD_STATE
:
412 b_bool
= (bool)va_arg( args
, int );
415 vlc_stream_Control( p_demux
->s
, STREAM_SET_RECORD_STATE
, false );
416 p_sys
->b_start_record
= b_bool
;
423 case DEMUX_CAN_PAUSE
:
424 case DEMUX_SET_PAUSE_STATE
:
425 case DEMUX_CAN_CONTROL_PACE
:
426 case DEMUX_GET_PTS_DELAY
:
427 return demux_vaControlHelper( p_demux
->s
, 0, -1, 0, 1, i_query
, args
);
434 /*****************************************************************************
436 *****************************************************************************/
437 static int ReSynch( demux_t
*p_demux
)
441 const uint8_t *p_peek
;
442 int i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, 1024 );
448 while( i_skip
< i_peek
- 4 )
450 if( !memcmp( p_peek
, "NSVf", 4 )
451 || !memcmp( p_peek
, "NSVs", 4 ) )
454 && vlc_stream_Read( p_demux
->s
, NULL
, i_skip
) < i_skip
)
462 if( vlc_stream_Read( p_demux
->s
, NULL
, i_skip
) < i_skip
)
468 /*****************************************************************************
470 *****************************************************************************/
471 static int ReadNSVf( demux_t
*p_demux
)
473 /* demux_sys_t *p_sys = p_demux->p_sys; */
476 msg_Dbg( p_demux
, "new NSVf chunk" );
477 if( vlc_stream_Peek( p_demux
->s
, &p
, 8 ) < 8 )
482 uint32_t i_header_size
= GetDWLE( &p
[4] );
483 msg_Dbg( p_demux
, " - size=%" PRIu32
, i_header_size
);
485 if( i_header_size
== 0 || i_header_size
== UINT32_MAX
)
487 #if (UINT32_MAX > SSIZE_MAX)
488 if( i_header_size
> SSIZE_MAX
)
491 if ( vlc_stream_Read( p_demux
->s
, NULL
, i_header_size
)
492 < (ssize_t
)i_header_size
)
496 /*****************************************************************************
498 *****************************************************************************/
499 static int ReadNSVs( demux_t
*p_demux
)
501 demux_sys_t
*p_sys
= p_demux
->p_sys
;
505 if( vlc_stream_Read( p_demux
->s
, header
, 19 ) < 19 )
507 msg_Warn( p_demux
, "cannot read" );
512 switch( ( fcc
= VLC_FOURCC( header
[4], header
[5], header
[6], header
[7] ) ) )
514 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
515 case VLC_FOURCC( 'V', 'P', '3', '0' ):
516 fcc
= VLC_FOURCC( 'V', 'P', '3', '0' );
519 case VLC_FOURCC( 'V', 'P', '3', '1' ):
520 fcc
= VLC_FOURCC( 'V', 'P', '3', '1' );
523 case VLC_FOURCC( 'V', 'P', '5', ' ' ):
524 case VLC_FOURCC( 'V', 'P', '5', '0' ):
525 fcc
= VLC_FOURCC( 'V', 'P', '5', '0' );
527 case VLC_FOURCC( 'V', 'P', '6', '0' ):
528 case VLC_FOURCC( 'V', 'P', '6', '1' ):
529 case VLC_FOURCC( 'V', 'P', '6', '2' ):
530 case VLC_FOURCC( 'V', 'P', '8', '0' ):
531 case VLC_FOURCC( 'H', '2', '6', '4' ):
532 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
535 msg_Err( p_demux
, "unsupported video codec %4.4s", (char *)&fcc
);
538 if( fcc
!= VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc
!= p_sys
->fmt_video
.i_codec
)
540 es_format_Init( &p_sys
->fmt_video
, VIDEO_ES
, fcc
);
541 p_sys
->fmt_video
.video
.i_width
= GetWLE( &header
[12] );
542 p_sys
->fmt_video
.video
.i_height
= GetWLE( &header
[14] );
543 p_sys
->fmt_video
.video
.i_visible_width
= p_sys
->fmt_video
.video
.i_width
;
544 p_sys
->fmt_video
.video
.i_visible_height
= p_sys
->fmt_video
.video
.i_height
;
547 es_out_Del( p_demux
->out
, p_sys
->p_video
);
549 p_sys
->p_video
= es_out_Add( p_demux
->out
, &p_sys
->fmt_video
);
551 msg_Dbg( p_demux
, " - video `%4.4s' %dx%d",
553 p_sys
->fmt_video
.video
.i_width
,
554 p_sys
->fmt_video
.video
.i_height
);
558 switch( ( fcc
= VLC_FOURCC( header
[8], header
[9], header
[10], header
[11] ) ) )
560 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
561 fcc
= VLC_FOURCC( 'm', 'p', 'g', 'a' );
563 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
564 fcc
= VLC_FOURCC( 'a', 'r', 'a', 'w' );
566 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
567 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
568 fcc
= VLC_FOURCC( 'm', 'p', '4', 'a' );
570 case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
571 fcc
= VLC_FOURCC( 's', 'p', 'x', ' ' );
573 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
576 msg_Err( p_demux
, "unsupported audio codec %4.4s", (char *)&fcc
);
580 if( fcc
!= VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc
!= p_sys
->fmt_audio
.i_codec
)
582 msg_Dbg( p_demux
, " - audio `%4.4s'", (char*)&fcc
);
586 es_out_Del( p_demux
->out
, p_sys
->p_audio
);
587 p_sys
->p_audio
= NULL
;
589 es_format_Init( &p_sys
->fmt_audio
, AUDIO_ES
, fcc
);
592 if( header
[16]&0x80 )
594 /* Fractional frame rate */
595 switch( header
[16]&0x03 )
598 p_sys
->i_pcr_inc
= 33333; /* 300000/9 */
600 case 1: /* 29.97 fps */
601 p_sys
->i_pcr_inc
= 33367; /* 300300/9 */
604 p_sys
->i_pcr_inc
= 40000; /* 360000/9 */
606 case 3: /* 23.98 fps */
607 p_sys
->i_pcr_inc
= 41700; /* 375300/9 */
611 if( header
[16] < 0xc0 )
612 p_sys
->i_pcr_inc
= p_sys
->i_pcr_inc
* (((header
[16] ^ 0x80) >> 2 ) +1 );
614 p_sys
->i_pcr_inc
= p_sys
->i_pcr_inc
/ (((header
[16] ^ 0xc0) >> 2 ) +1 );
616 else if( header
[16] != 0 )
618 /* Integer frame rate */
619 p_sys
->i_pcr_inc
= vlc_tick_from_samples(1, header
[16]);
623 msg_Dbg( p_demux
, "invalid fps (0x00)" );
624 p_sys
->i_pcr_inc
= VLC_TICK_FROM_MS(40);
626 //msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
628 if( p_sys
->p_audio
== NULL
&& p_sys
->p_video
== NULL
)
630 msg_Err( p_demux
, "unable to play neither audio nor video, aborting." );