qml: Create MediaGroupDisplay
[vlc.git] / modules / demux / nsv.c
blobf540b103b253b2bcbb76fc41ad5b45c69696bf4e
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 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <limits.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
37 /* TODO:
38 * - implement NSVf parsing (to get meta data)
39 * - implement missing Control (and in the right way)
40 * - ...
43 /*****************************************************************************
44 * Module descriptor
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
49 vlc_module_begin ()
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 )
55 add_shortcut( "nsv" )
56 add_file_extension("nsv")
57 vlc_module_end ()
59 /*****************************************************************************
60 * Local prototypes
61 *****************************************************************************/
63 typedef struct
65 es_format_t fmt_audio;
66 es_out_id_t *p_audio;
68 es_format_t fmt_video;
69 es_out_id_t *p_video;
71 es_format_t fmt_sub;
72 es_out_id_t *p_sub;
74 vlc_tick_t i_pcr;
75 vlc_tick_t i_time;
76 vlc_tick_t i_pcr_inc;
78 bool b_start_record;
79 } demux_sys_t;
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 /*****************************************************************************
90 * Open
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 demux_t *p_demux = (demux_t*)p_this;
95 demux_sys_t *p_sys;
97 const uint8_t *p_peek;
99 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
100 return VLC_EGENERIC;
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 ) )
106 return VLC_EGENERIC;
109 p_sys = malloc( sizeof( demux_sys_t ) );
110 if( unlikely(p_sys == NULL) )
111 return VLC_ENOMEM;
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 );
125 p_sys->p_sub = NULL;
127 p_sys->i_pcr = 0;
128 p_sys->i_time = 0;
129 p_sys->i_pcr_inc = 0;
131 p_sys->b_start_record = false;
133 return VLC_SUCCESS;
136 /*****************************************************************************
137 * Close
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;
144 free( p_sys );
148 /*****************************************************************************
149 * Demux:
150 *****************************************************************************/
151 static int Demux( demux_t *p_demux )
153 demux_sys_t *p_sys = p_demux->p_sys;
155 uint8_t header[5];
156 const uint8_t *p_peek;
158 int i_size;
159 block_t *p_frame;
161 for( ;; )
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;
182 break;
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;
192 break;
194 else
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;
208 /* Set PCR */
209 es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_pcr );
211 /* Read video */
212 i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
213 if( i_size > 0 )
215 /* extra data ? */
216 if( (header[0]&0x0f) != 0x0 )
218 uint8_t aux[6];
219 int i_aux;
220 vlc_fourcc_t fcc;
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 );
238 if( p_sys->p_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 )
250 p++;
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);
262 if( p_sys->p_sub )
263 es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
264 else
265 block_Release( p_frame );
268 else
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;
277 i_size -= 6 + i_aux;
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;
285 if( p_sys->p_video )
286 es_out_Send( p_demux->out, p_sys->p_video, p_frame );
287 else
289 block_Release( p_frame );
290 msg_Dbg( p_demux, "ignoring unsupported video frame (size=%d)",
291 i_size );
296 /* Read audio */
297 i_size = header[3] | ( header[4] << 8 );
298 if( i_size > 0 )
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' ) )
303 uint8_t h[4];
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] );
310 i_size -= 4;
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 ) ) )
319 p_frame->i_dts =
320 p_frame->i_pts = VLC_TICK_0 + p_sys->i_pcr;
322 if( p_sys->p_audio )
323 es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
324 else
326 block_Release( p_frame );
327 msg_Dbg( p_demux, "ignoring unsupported audio frame (size=%d)",
328 i_size );
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 /*****************************************************************************
343 * Control:
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;
348 double f, *pf;
349 bool b_bool, *pb_bool;
350 int64_t i64;
352 switch( i_query )
354 case DEMUX_CAN_SEEK:
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 );
360 if( i64 > 0 )
362 double current = vlc_stream_Tell( p_demux->s );
363 *pf = current / (double)i64;
365 else
367 *pf = 0.0;
369 return VLC_SUCCESS;
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 ) )
376 return VLC_EGENERIC;
378 p_sys->i_time = -1; /* Invalidate time display */
379 return VLC_SUCCESS;
381 case DEMUX_GET_TIME:
382 if( p_sys->i_time < 0 )
384 *va_arg( args, vlc_tick_t * ) = 0;
385 return VLC_EGENERIC;
387 *va_arg( args, vlc_tick_t * ) = p_sys->i_time;
388 return VLC_SUCCESS;
390 #if 0
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);
395 return VLC_SUCCESS;
397 *va_arg( args, vlc_tick_t * ) = 0;
398 return VLC_EGENERIC;
400 #endif
401 case DEMUX_GET_FPS:
402 pf = va_arg( args, double * );
403 *pf = (double)CLOCK_FREQ / (double)p_sys->i_pcr_inc;
404 return VLC_SUCCESS;
406 case DEMUX_CAN_RECORD:
407 pb_bool = va_arg( args, bool * );
408 *pb_bool = true;
409 return VLC_SUCCESS;
411 case DEMUX_SET_RECORD_STATE:
412 b_bool = (bool)va_arg( args, int );
414 if( !b_bool )
415 vlc_stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
416 p_sys->b_start_record = b_bool;
417 return VLC_SUCCESS;
420 case DEMUX_SET_TIME:
421 return VLC_EGENERIC;
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 );
429 default:
430 return VLC_EGENERIC;
434 /*****************************************************************************
435 * ReSynch:
436 *****************************************************************************/
437 static int ReSynch( demux_t *p_demux )
439 for( ;; )
441 const uint8_t *p_peek;
442 int i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 1024 );
443 if( i_peek < 8 )
444 break;
446 int i_skip = 0;
448 while( i_skip < i_peek - 4 )
450 if( !memcmp( p_peek, "NSVf", 4 )
451 || !memcmp( p_peek, "NSVs", 4 ) )
453 if( i_skip > 0
454 && vlc_stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
455 return VLC_EGENERIC;
456 return VLC_SUCCESS;
458 p_peek++;
459 i_skip++;
462 if( vlc_stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
463 break;
465 return VLC_EGENERIC;
468 /*****************************************************************************
469 * ReadNSVf:
470 *****************************************************************************/
471 static int ReadNSVf( demux_t *p_demux )
473 /* demux_sys_t *p_sys = p_demux->p_sys; */
474 const uint8_t *p;
476 msg_Dbg( p_demux, "new NSVf chunk" );
477 if( vlc_stream_Peek( p_demux->s, &p, 8 ) < 8 )
479 return VLC_EGENERIC;
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 )
486 return VLC_EGENERIC;
487 #if (UINT32_MAX > SSIZE_MAX)
488 if( i_header_size > SSIZE_MAX )
489 return VLC_EGENERIC;
490 #endif
491 if ( vlc_stream_Read( p_demux->s, NULL, i_header_size )
492 < (ssize_t)i_header_size )
493 return VLC_EGENERIC;
494 return VLC_SUCCESS;
496 /*****************************************************************************
497 * ReadNSVs:
498 *****************************************************************************/
499 static int ReadNSVs( demux_t *p_demux )
501 demux_sys_t *p_sys = p_demux->p_sys;
502 uint8_t header[19];
503 vlc_fourcc_t fcc;
505 if( vlc_stream_Read( p_demux->s, header, 19 ) < 19 )
507 msg_Warn( p_demux, "cannot read" );
508 return VLC_EGENERIC;
511 /* Video */
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' );
517 break;
519 case VLC_FOURCC( 'V', 'P', '3', '1' ):
520 fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
521 break;
523 case VLC_FOURCC( 'V', 'P', '5', ' ' ):
524 case VLC_FOURCC( 'V', 'P', '5', '0' ):
525 fcc = VLC_FOURCC( 'V', 'P', '5', '0' );
526 break;
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' ):
533 break;
534 default:
535 msg_Err( p_demux, "unsupported video codec %4.4s", (char *)&fcc );
536 break;
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;
545 if( p_sys->p_video )
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",
552 (char*)&fcc,
553 p_sys->fmt_video.video.i_width,
554 p_sys->fmt_video.video.i_height );
557 /* Audio */
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' );
562 break;
563 case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
564 fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
565 break;
566 case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
567 case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
568 fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
569 break;
570 case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
571 fcc = VLC_FOURCC( 's', 'p', 'x', ' ' );
572 break;
573 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
574 break;
575 default:
576 msg_Err( p_demux, "unsupported audio codec %4.4s", (char *)&fcc );
577 break;
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 );
584 if( p_sys->p_audio )
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 )
597 case 0: /* 30 fps */
598 p_sys->i_pcr_inc = 33333; /* 300000/9 */
599 break;
600 case 1: /* 29.97 fps */
601 p_sys->i_pcr_inc = 33367; /* 300300/9 */
602 break;
603 case 2: /* 25 fps */
604 p_sys->i_pcr_inc = 40000; /* 360000/9 */
605 break;
606 case 3: /* 23.98 fps */
607 p_sys->i_pcr_inc = 41700; /* 375300/9 */
608 break;
611 if( header[16] < 0xc0 )
612 p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
613 else
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]);
621 else
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." );
631 return VLC_EGENERIC;
634 return VLC_SUCCESS;