preparser: use new input event handling
[vlc.git] / modules / demux / nsv.c
blob6110750c8abf0d68d9b43e7ce8c8ec3afcac1582
1 /*****************************************************************************
2 * nsv.c: NullSoft Video demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004-2007 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /* TODO:
37 * - implement NSVf parsing (to get meta data)
38 * - implement missing Control (and in the right way)
39 * - ...
42 /*****************************************************************************
43 * Module descriptor
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
48 vlc_module_begin ()
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" )
55 vlc_module_end ()
57 /*****************************************************************************
58 * Local prototypes
59 *****************************************************************************/
61 typedef struct
63 es_format_t fmt_audio;
64 es_out_id_t *p_audio;
66 es_format_t fmt_video;
67 es_out_id_t *p_video;
69 es_format_t fmt_sub;
70 es_out_id_t *p_sub;
72 int64_t i_pcr;
73 int64_t i_time;
74 int64_t i_pcr_inc;
76 bool b_start_record;
77 } demux_sys_t;
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 /*****************************************************************************
88 * Open
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 demux_t *p_demux = (demux_t*)p_this;
93 demux_sys_t *p_sys;
95 const uint8_t *p_peek;
97 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
98 return VLC_EGENERIC;
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 ) )
104 return VLC_EGENERIC;
107 p_sys = malloc( sizeof( demux_sys_t ) );
108 if( unlikely(p_sys == NULL) )
109 return VLC_ENOMEM;
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 );
123 p_sys->p_sub = NULL;
125 p_sys->i_pcr = 0;
126 p_sys->i_time = 0;
127 p_sys->i_pcr_inc = 0;
129 p_sys->b_start_record = false;
131 return VLC_SUCCESS;
134 /*****************************************************************************
135 * Close
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;
142 free( p_sys );
146 /*****************************************************************************
147 * Demux:
148 *****************************************************************************/
149 static int Demux( demux_t *p_demux )
151 demux_sys_t *p_sys = p_demux->p_sys;
153 uint8_t header[5];
154 const uint8_t *p_peek;
156 int i_size;
157 block_t *p_frame;
159 for( ;; )
161 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
163 msg_Warn( p_demux, "cannot peek" );
164 return VLC_DEMUXER_EOF;
167 if( !memcmp( p_peek, "NSVf", 4 ) )
169 if( ReadNSVf( p_demux ) )
170 return VLC_DEMUXER_EGENERIC;
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 ) )
182 return VLC_DEMUXER_EGENERIC;
183 break;
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" );
191 return VLC_DEMUXER_EOF;
193 break;
195 else
197 msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
198 if( ReSynch( p_demux ) )
199 return VLC_DEMUXER_EGENERIC;
203 if( vlc_stream_Read( p_demux->s, header, 5 ) < 5 )
205 msg_Warn( p_demux, "cannot read" );
206 return VLC_DEMUXER_EOF;
209 /* Set PCR */
210 es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_pcr );
212 /* Read video */
213 i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
214 if( i_size > 0 )
216 /* extra data ? */
217 if( (header[0]&0x0f) != 0x0 )
219 uint8_t aux[6];
220 int i_aux;
221 vlc_fourcc_t fcc;
222 if( vlc_stream_Read( p_demux->s, aux, 6 ) < 6 )
224 msg_Warn( p_demux, "cannot read" );
225 return VLC_DEMUXER_EOF;
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 if( p_sys->p_sub )
240 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
242 if( vlc_stream_Read( p_demux->s, NULL, 2 ) < 2 )
243 return VLC_DEMUXER_EOF;
245 if( ( p_frame = vlc_stream_Block( p_demux->s, i_aux - 2 ) ) )
247 uint8_t *p = p_frame->p_buffer;
249 while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
251 p++;
253 if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
255 p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
256 p_frame->p_buffer = p + 1;
259 /* Skip the first part (it is the language name) */
260 p_frame->i_pts = VLC_TICK_0 + p_sys->i_pcr;
261 p_frame->i_dts = VLC_TICK_0 + p_sys->i_pcr + VLC_TICK_FROM_SEC(4);
263 if( p_sys->p_sub )
264 es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
265 else
266 block_Release( p_frame );
269 else
271 /* We skip this extra data */
272 if( vlc_stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
274 msg_Warn( p_demux, "cannot read" );
275 return VLC_DEMUXER_EOF;
278 i_size -= 6 + i_aux;
281 /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
282 if( i_size > 0 && ( p_frame = vlc_stream_Block( p_demux->s, i_size ) ) )
284 p_frame->i_dts = VLC_TICK_0 + p_sys->i_pcr;
286 if( p_sys->p_video )
287 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
288 else
290 block_Release( p_frame );
291 msg_Dbg( p_demux, "ignoring unsupported video frame (size=%d)",
292 i_size );
297 /* Read audio */
298 i_size = header[3] | ( header[4] << 8 );
299 if( i_size > 0 )
301 /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
302 if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
304 uint8_t h[4];
305 if( vlc_stream_Read( p_demux->s, h, 4 ) < 4 )
306 return VLC_DEMUXER_EOF;
308 p_sys->fmt_audio.audio.i_channels = h[1];
309 p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
311 i_size -= 4;
313 if( p_sys->p_audio == NULL )
315 p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
318 if( ( p_frame = vlc_stream_Block( p_demux->s, i_size ) ) )
320 p_frame->i_dts =
321 p_frame->i_pts = VLC_TICK_0 + p_sys->i_pcr;
323 if( p_sys->p_audio )
324 es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
325 else
327 block_Release( p_frame );
328 msg_Dbg( p_demux, "ignoring unsupported audio frame (size=%d)",
329 i_size );
334 p_sys->i_pcr += p_sys->i_pcr_inc;
335 if( p_sys->i_time >= 0 )
337 p_sys->i_time += p_sys->i_pcr_inc;
340 return VLC_DEMUXER_SUCCESS;
343 /*****************************************************************************
344 * Control:
345 *****************************************************************************/
346 static int Control( demux_t *p_demux, int i_query, va_list args )
348 demux_sys_t *p_sys = p_demux->p_sys;
349 double f, *pf;
350 bool b_bool, *pb_bool;
351 int64_t i64, *pi64;
353 switch( i_query )
355 case DEMUX_CAN_SEEK:
356 return vlc_stream_vaControl( p_demux->s, i_query, args );
358 case DEMUX_GET_POSITION:
359 pf = va_arg( args, double * );
360 i64 = stream_Size( p_demux->s );
361 if( i64 > 0 )
363 double current = vlc_stream_Tell( p_demux->s );
364 *pf = current / (double)i64;
366 else
368 *pf = 0.0;
370 return VLC_SUCCESS;
372 case DEMUX_SET_POSITION:
373 f = va_arg( args, double );
374 i64 = stream_Size( p_demux->s );
376 if( vlc_stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
377 return VLC_EGENERIC;
379 p_sys->i_time = -1; /* Invalidate time display */
380 return VLC_SUCCESS;
382 case DEMUX_GET_TIME:
383 pi64 = va_arg( args, int64_t * );
384 if( p_sys->i_time < 0 )
386 *pi64 = 0;
387 return VLC_EGENERIC;
389 *pi64 = p_sys->i_time;
390 return VLC_SUCCESS;
392 #if 0
393 case DEMUX_GET_LENGTH:
394 pi64 = va_arg( args, int64_t * );
395 if( p_sys->i_mux_rate > 0 )
397 *pi64 = CLOCK_FREQ * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
398 return VLC_SUCCESS;
400 *pi64 = 0;
401 return VLC_EGENERIC;
403 #endif
404 case DEMUX_GET_FPS:
405 pf = va_arg( args, double * );
406 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
407 return VLC_SUCCESS;
409 case DEMUX_CAN_RECORD:
410 pb_bool = va_arg( args, bool * );
411 *pb_bool = true;
412 return VLC_SUCCESS;
414 case DEMUX_SET_RECORD_STATE:
415 b_bool = (bool)va_arg( args, int );
417 if( !b_bool )
418 vlc_stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
419 p_sys->b_start_record = b_bool;
420 return VLC_SUCCESS;
423 case DEMUX_SET_TIME:
424 return VLC_EGENERIC;
426 case DEMUX_CAN_PAUSE:
427 case DEMUX_SET_PAUSE_STATE:
428 case DEMUX_CAN_CONTROL_PACE:
429 case DEMUX_GET_PTS_DELAY:
430 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
432 default:
433 return VLC_EGENERIC;
437 /*****************************************************************************
438 * ReSynch:
439 *****************************************************************************/
440 static int ReSynch( demux_t *p_demux )
442 for( ;; )
444 const uint8_t *p_peek;
445 int i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 1024 );
446 if( i_peek < 8 )
447 break;
449 int i_skip = 0;
451 while( i_skip < i_peek - 4 )
453 if( !memcmp( p_peek, "NSVf", 4 )
454 || !memcmp( p_peek, "NSVs", 4 ) )
456 if( i_skip > 0
457 && vlc_stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
458 return VLC_EGENERIC;
459 return VLC_SUCCESS;
461 p_peek++;
462 i_skip++;
465 if( vlc_stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
466 break;
468 return VLC_EGENERIC;
471 /*****************************************************************************
472 * ReadNSVf:
473 *****************************************************************************/
474 static int ReadNSVf( demux_t *p_demux )
476 /* demux_sys_t *p_sys = p_demux->p_sys; */
477 const uint8_t *p;
479 msg_Dbg( p_demux, "new NSVf chunk" );
480 if( vlc_stream_Peek( p_demux->s, &p, 8 ) < 8 )
482 return VLC_EGENERIC;
485 uint32_t i_header_size = GetDWLE( &p[4] );
486 msg_Dbg( p_demux, " - size=%" PRIu32, i_header_size );
488 if( i_header_size == 0 || i_header_size == UINT32_MAX )
489 return VLC_EGENERIC;
492 return vlc_stream_Read( p_demux->s, NULL, i_header_size ) == i_header_size
493 ? VLC_SUCCESS : VLC_EGENERIC;
495 /*****************************************************************************
496 * ReadNSVs:
497 *****************************************************************************/
498 static int ReadNSVs( demux_t *p_demux )
500 demux_sys_t *p_sys = p_demux->p_sys;
501 uint8_t header[19];
502 vlc_fourcc_t fcc;
504 if( vlc_stream_Read( p_demux->s, header, 19 ) < 19 )
506 msg_Warn( p_demux, "cannot read" );
507 return VLC_EGENERIC;
510 /* Video */
511 switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
513 case VLC_FOURCC( 'V', 'P', '3', ' ' ):
514 case VLC_FOURCC( 'V', 'P', '3', '0' ):
515 fcc = VLC_FOURCC( 'V', 'P', '3', '0' );
516 break;
518 case VLC_FOURCC( 'V', 'P', '3', '1' ):
519 fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
520 break;
522 case VLC_FOURCC( 'V', 'P', '5', ' ' ):
523 case VLC_FOURCC( 'V', 'P', '5', '0' ):
524 fcc = VLC_FOURCC( 'V', 'P', '5', '0' );
525 break;
526 case VLC_FOURCC( 'V', 'P', '6', '0' ):
527 case VLC_FOURCC( 'V', 'P', '6', '1' ):
528 case VLC_FOURCC( 'V', 'P', '6', '2' ):
529 case VLC_FOURCC( 'V', 'P', '8', '0' ):
530 case VLC_FOURCC( 'H', '2', '6', '4' ):
531 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
532 break;
533 default:
534 msg_Err( p_demux, "unsupported video codec %4.4s", (char *)&fcc );
535 break;
537 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec )
539 es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
540 p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
541 p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
542 p_sys->fmt_video.video.i_visible_width = p_sys->fmt_video.video.i_width;
543 p_sys->fmt_video.video.i_visible_height = p_sys->fmt_video.video.i_height;
544 if( p_sys->p_video )
546 es_out_Del( p_demux->out, p_sys->p_video );
548 p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
550 msg_Dbg( p_demux, " - video `%4.4s' %dx%d",
551 (char*)&fcc,
552 p_sys->fmt_video.video.i_width,
553 p_sys->fmt_video.video.i_height );
556 /* Audio */
557 switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
559 case VLC_FOURCC( 'M', 'P', '3', ' ' ):
560 fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
561 break;
562 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
563 fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
564 break;
565 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
566 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
567 fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
568 break;
569 case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
570 fcc = VLC_FOURCC( 's', 'p', 'x', ' ' );
571 break;
572 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
573 break;
574 default:
575 msg_Err( p_demux, "unsupported audio codec %4.4s", (char *)&fcc );
576 break;
579 if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
581 msg_Dbg( p_demux, " - audio `%4.4s'", (char*)&fcc );
583 if( p_sys->p_audio )
585 es_out_Del( p_demux->out, p_sys->p_audio );
586 p_sys->p_audio = NULL;
588 es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
591 if( header[16]&0x80 )
593 /* Fractional frame rate */
594 switch( header[16]&0x03 )
596 case 0: /* 30 fps */
597 p_sys->i_pcr_inc = 33333; /* 300000/9 */
598 break;
599 case 1: /* 29.97 fps */
600 p_sys->i_pcr_inc = 33367; /* 300300/9 */
601 break;
602 case 2: /* 25 fps */
603 p_sys->i_pcr_inc = 40000; /* 360000/9 */
604 break;
605 case 3: /* 23.98 fps */
606 p_sys->i_pcr_inc = 41700; /* 375300/9 */
607 break;
610 if( header[16] < 0xc0 )
611 p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
612 else
613 p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
615 else if( header[16] != 0 )
617 /* Integer frame rate */
618 p_sys->i_pcr_inc = 1000000 / header[16];
620 else
622 msg_Dbg( p_demux, "invalid fps (0x00)" );
623 p_sys->i_pcr_inc = 40000;
625 //msg_Dbg( p_demux, " - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
627 if( p_sys->p_audio == NULL && p_sys->p_video == NULL )
629 msg_Err( p_demux, "unable to play neither audio nor video, aborting." );
630 return VLC_EGENERIC;
633 return VLC_SUCCESS;