1 /*****************************************************************************
2 * nsv.c: NullSoft Video demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004-2007 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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
)
57 /*****************************************************************************
59 *****************************************************************************/
63 es_format_t fmt_audio
;
66 es_format_t fmt_video
;
79 static int Demux ( demux_t
*p_demux
);
80 static int Control( demux_t
*p_demux
, int i_query
, va_list args
);
82 static int ReSynch( demux_t
*p_demux
);
84 static int ReadNSVf( demux_t
*p_demux
);
85 static int ReadNSVs( demux_t
*p_demux
);
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open( vlc_object_t
*p_this
)
92 demux_t
*p_demux
= (demux_t
*)p_this
;
95 const uint8_t *p_peek
;
97 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 )
100 if( memcmp( p_peek
, "NSVf", 4 ) && memcmp( p_peek
, "NSVs", 4 ) )
102 /* In case we had force this demuxer we try to resynch */
103 if( !p_demux
->obj
.force
|| ReSynch( p_demux
) )
107 p_sys
= malloc( sizeof( demux_sys_t
) );
108 if( unlikely(p_sys
== NULL
) )
111 /* Fill p_demux field */
112 p_demux
->p_sys
= p_sys
;
113 p_demux
->pf_demux
= Demux
;
114 p_demux
->pf_control
= Control
;
116 es_format_Init( &p_sys
->fmt_audio
, AUDIO_ES
, 0 );
117 p_sys
->p_audio
= NULL
;
119 es_format_Init( &p_sys
->fmt_video
, VIDEO_ES
, 0 );
120 p_sys
->p_video
= NULL
;
122 es_format_Init( &p_sys
->fmt_sub
, SPU_ES
, 0 );
127 p_sys
->i_pcr_inc
= 0;
129 p_sys
->b_start_record
= false;
134 /*****************************************************************************
136 *****************************************************************************/
137 static void Close( vlc_object_t
*p_this
)
139 demux_t
*p_demux
= (demux_t
*)p_this
;
140 demux_sys_t
*p_sys
= p_demux
->p_sys
;
146 /*****************************************************************************
148 *****************************************************************************/
149 static int Demux( demux_t
*p_demux
)
151 demux_sys_t
*p_sys
= p_demux
->p_sys
;
154 const uint8_t *p_peek
;
161 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) < 8 )
163 msg_Warn( p_demux
, "cannot peek" );
167 if( !memcmp( p_peek
, "NSVf", 4 ) )
169 if( ReadNSVf( p_demux
) )
172 else if( !memcmp( p_peek
, "NSVs", 4 ) )
174 if( p_sys
->b_start_record
)
176 /* Enable recording once synchronized */
177 vlc_stream_Control( p_demux
->s
, STREAM_SET_RECORD_STATE
, true, "nsv" );
178 p_sys
->b_start_record
= false;
181 if( ReadNSVs( p_demux
) )
185 else if( GetWLE( p_peek
) == 0xbeef )
187 /* Next frame of the current NSVs chunk */
188 if( vlc_stream_Read( p_demux
->s
, NULL
, 2 ) < 2 )
190 msg_Warn( p_demux
, "cannot read" );
197 msg_Err( p_demux
, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek
), (const char*)p_peek
);
198 if( ReSynch( p_demux
) )
203 if( vlc_stream_Read( p_demux
->s
, header
, 5 ) < 5 )
205 msg_Warn( p_demux
, "cannot read" );
210 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ p_sys
->i_pcr
);
213 i_size
= ( header
[0] >> 4 ) | ( header
[1] << 4 ) | ( header
[2] << 12 );
217 if( (header
[0]&0x0f) != 0x0 )
222 if( vlc_stream_Read( p_demux
->s
, aux
, 6 ) < 6 )
224 msg_Warn( p_demux
, "cannot read" );
227 i_aux
= GetWLE( aux
);
228 fcc
= VLC_FOURCC( aux
[2], aux
[3], aux
[4], aux
[5] );
230 msg_Dbg( p_demux
, "Belekas: %d - size=%d fcc=%4.4s",
231 header
[0]&0xf, i_aux
, (char*)&fcc
);
233 if( fcc
== VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux
> 2 )
235 if( p_sys
->p_sub
== NULL
)
237 p_sys
->fmt_sub
.i_codec
= VLC_FOURCC( 's', 'u', 'b', 't' );
238 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 )
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_TS_0
+ p_sys
->i_pcr
;
260 p_frame
->i_dts
= VLC_TS_0
+ p_sys
->i_pcr
+ 4000000; /* 4s */
262 es_out_Send( p_demux
->out
, p_sys
->p_sub
, p_frame
);
267 /* We skip this extra data */
268 if( vlc_stream_Read( p_demux
->s
, NULL
, i_aux
) < i_aux
)
270 msg_Warn( p_demux
, "cannot read" );
277 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
278 if( i_size
> 0 && ( p_frame
= vlc_stream_Block( p_demux
->s
, i_size
) ) )
280 p_frame
->i_dts
= VLC_TS_0
+ p_sys
->i_pcr
;
283 es_out_Send( p_demux
->out
, p_sys
->p_video
, p_frame
);
286 block_Release( p_frame
);
287 msg_Dbg( p_demux
, "ignoring unsupported video frame (size=%d)",
294 i_size
= header
[3] | ( header
[4] << 8 );
297 /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
298 if( p_sys
->fmt_audio
.i_codec
== VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
301 if( vlc_stream_Read( p_demux
->s
, h
, 4 ) < 4 )
304 p_sys
->fmt_audio
.audio
.i_channels
= h
[1];
305 p_sys
->fmt_audio
.audio
.i_rate
= GetWLE( &h
[2] );
309 if( p_sys
->p_audio
== NULL
)
311 p_sys
->p_audio
= es_out_Add( p_demux
->out
, &p_sys
->fmt_audio
);
314 if( ( p_frame
= vlc_stream_Block( p_demux
->s
, i_size
) ) )
317 p_frame
->i_pts
= VLC_TS_0
+ p_sys
->i_pcr
;
320 es_out_Send( p_demux
->out
, p_sys
->p_audio
, p_frame
);
323 block_Release( p_frame
);
324 msg_Dbg( p_demux
, "ignoring unsupported audio frame (size=%d)",
330 p_sys
->i_pcr
+= p_sys
->i_pcr_inc
;
331 if( p_sys
->i_time
>= 0 )
333 p_sys
->i_time
+= p_sys
->i_pcr_inc
;
339 /*****************************************************************************
341 *****************************************************************************/
342 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
344 demux_sys_t
*p_sys
= p_demux
->p_sys
;
346 bool b_bool
, *pb_bool
;
352 return vlc_stream_vaControl( p_demux
->s
, i_query
, args
);
354 case DEMUX_GET_POSITION
:
355 pf
= va_arg( args
, double * );
356 i64
= stream_Size( p_demux
->s
);
359 double current
= vlc_stream_Tell( p_demux
->s
);
360 *pf
= current
/ (double)i64
;
368 case DEMUX_SET_POSITION
:
369 f
= va_arg( args
, double );
370 i64
= stream_Size( p_demux
->s
);
372 if( vlc_stream_Seek( p_demux
->s
, (int64_t)(i64
* f
) ) || ReSynch( p_demux
) )
375 p_sys
->i_time
= -1; /* Invalidate time display */
379 pi64
= va_arg( args
, int64_t * );
380 if( p_sys
->i_time
< 0 )
385 *pi64
= p_sys
->i_time
;
389 case DEMUX_GET_LENGTH
:
390 pi64
= va_arg( args
, int64_t * );
391 if( p_sys
->i_mux_rate
> 0 )
393 *pi64
= (int64_t)1000000 * ( stream_Size( p_demux
->s
) / 50 ) / p_sys
->i_mux_rate
;
401 pf
= va_arg( args
, double * );
402 *pf
= (double)1000000.0 / (double)p_sys
->i_pcr_inc
;
405 case DEMUX_CAN_RECORD
:
406 pb_bool
= va_arg( args
, bool * );
410 case DEMUX_SET_RECORD_STATE
:
411 b_bool
= (bool)va_arg( args
, int );
414 vlc_stream_Control( p_demux
->s
, STREAM_SET_RECORD_STATE
, false );
415 p_sys
->b_start_record
= b_bool
;
425 /*****************************************************************************
427 *****************************************************************************/
428 static int ReSynch( demux_t
*p_demux
)
432 const uint8_t *p_peek
;
433 int i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, 1024 );
439 while( i_skip
< i_peek
- 4 )
441 if( !memcmp( p_peek
, "NSVf", 4 )
442 || !memcmp( p_peek
, "NSVs", 4 ) )
445 && vlc_stream_Read( p_demux
->s
, NULL
, i_skip
) < i_skip
)
453 if( vlc_stream_Read( p_demux
->s
, NULL
, i_skip
) < i_skip
)
459 /*****************************************************************************
461 *****************************************************************************/
462 static int ReadNSVf( demux_t
*p_demux
)
464 /* demux_sys_t *p_sys = p_demux->p_sys; */
467 msg_Dbg( p_demux
, "new NSVf chunk" );
468 if( vlc_stream_Peek( p_demux
->s
, &p
, 8 ) < 8 )
473 uint32_t i_header_size
= GetDWLE( &p
[4] );
474 msg_Dbg( p_demux
, " - size=%" PRIu32
, i_header_size
);
476 if( i_header_size
== 0 || i_header_size
== UINT32_MAX
)
480 return vlc_stream_Read( p_demux
->s
, NULL
, i_header_size
) == i_header_size
481 ? VLC_SUCCESS
: VLC_EGENERIC
;
483 /*****************************************************************************
485 *****************************************************************************/
486 static int ReadNSVs( demux_t
*p_demux
)
488 demux_sys_t
*p_sys
= p_demux
->p_sys
;
492 if( vlc_stream_Read( p_demux
->s
, header
, 19 ) < 19 )
494 msg_Warn( p_demux
, "cannot read" );
499 switch( ( fcc
= VLC_FOURCC( header
[4], header
[5], header
[6], header
[7] ) ) )
501 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
502 case VLC_FOURCC( 'V', 'P', '3', '0' ):
503 fcc
= VLC_FOURCC( 'V', 'P', '3', '0' );
506 case VLC_FOURCC( 'V', 'P', '3', '1' ):
507 fcc
= VLC_FOURCC( 'V', 'P', '3', '1' );
510 case VLC_FOURCC( 'V', 'P', '5', ' ' ):
511 case VLC_FOURCC( 'V', 'P', '5', '0' ):
512 fcc
= VLC_FOURCC( 'V', 'P', '5', '0' );
514 case VLC_FOURCC( 'V', 'P', '6', '0' ):
515 case VLC_FOURCC( 'V', 'P', '6', '1' ):
516 case VLC_FOURCC( 'V', 'P', '6', '2' ):
517 case VLC_FOURCC( 'V', 'P', '8', '0' ):
518 case VLC_FOURCC( 'H', '2', '6', '4' ):
519 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
522 msg_Err( p_demux
, "unsupported video codec %4.4s", (char *)&fcc
);
525 if( fcc
!= VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc
!= p_sys
->fmt_video
.i_codec
)
527 es_format_Init( &p_sys
->fmt_video
, VIDEO_ES
, fcc
);
528 p_sys
->fmt_video
.video
.i_width
= GetWLE( &header
[12] );
529 p_sys
->fmt_video
.video
.i_height
= GetWLE( &header
[14] );
530 p_sys
->fmt_video
.video
.i_visible_width
= p_sys
->fmt_video
.video
.i_width
;
531 p_sys
->fmt_video
.video
.i_visible_height
= p_sys
->fmt_video
.video
.i_height
;
534 es_out_Del( p_demux
->out
, p_sys
->p_video
);
536 p_sys
->p_video
= es_out_Add( p_demux
->out
, &p_sys
->fmt_video
);
538 msg_Dbg( p_demux
, " - video `%4.4s' %dx%d",
540 p_sys
->fmt_video
.video
.i_width
,
541 p_sys
->fmt_video
.video
.i_height
);
545 switch( ( fcc
= VLC_FOURCC( header
[8], header
[9], header
[10], header
[11] ) ) )
547 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
548 fcc
= VLC_FOURCC( 'm', 'p', 'g', 'a' );
550 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
551 fcc
= VLC_FOURCC( 'a', 'r', 'a', 'w' );
553 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
554 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
555 fcc
= VLC_FOURCC( 'm', 'p', '4', 'a' );
557 case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
558 fcc
= VLC_FOURCC( 's', 'p', 'x', ' ' );
560 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
563 msg_Err( p_demux
, "unsupported audio codec %4.4s", (char *)&fcc
);
567 if( fcc
!= VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc
!= p_sys
->fmt_audio
.i_codec
)
569 msg_Dbg( p_demux
, " - audio `%4.4s'", (char*)&fcc
);
573 es_out_Del( p_demux
->out
, p_sys
->p_audio
);
574 p_sys
->p_audio
= NULL
;
576 es_format_Init( &p_sys
->fmt_audio
, AUDIO_ES
, fcc
);
579 if( header
[16]&0x80 )
581 /* Fractional frame rate */
582 switch( header
[16]&0x03 )
585 p_sys
->i_pcr_inc
= 33333; /* 300000/9 */
587 case 1: /* 29.97 fps */
588 p_sys
->i_pcr_inc
= 33367; /* 300300/9 */
591 p_sys
->i_pcr_inc
= 40000; /* 360000/9 */
593 case 3: /* 23.98 fps */
594 p_sys
->i_pcr_inc
= 41700; /* 375300/9 */
598 if( header
[16] < 0xc0 )
599 p_sys
->i_pcr_inc
= p_sys
->i_pcr_inc
* (((header
[16] ^ 0x80) >> 2 ) +1 );
601 p_sys
->i_pcr_inc
= p_sys
->i_pcr_inc
/ (((header
[16] ^ 0xc0) >> 2 ) +1 );
603 else if( header
[16] != 0 )
605 /* Integer frame rate */
606 p_sys
->i_pcr_inc
= 1000000 / header
[16];
610 msg_Dbg( p_demux
, "invalid fps (0x00)" );
611 p_sys
->i_pcr_inc
= 40000;
613 //msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
615 if( p_sys
->p_audio
== NULL
&& p_sys
->p_video
== NULL
)
617 msg_Err( p_demux
, "unable to play neither audio nor video, aborting." );