]> git.sesse.net Git - vlc/blob - modules/demux/nsv.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / demux / nsv.c
1 /*****************************************************************************
2  * nsv.c: NullSoft Video demuxer.
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35
36 /* TODO:
37  *  - implement NSVf parsing (to get meta data)
38  *  - implement missing Control (and in the right way)
39  *  - ...
40  */
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open    ( vlc_object_t * );
46 static void Close  ( vlc_object_t * );
47
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 ()
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60
61 struct demux_sys_t
62 {
63     es_format_t  fmt_audio;
64     es_out_id_t *p_audio;
65
66     es_format_t  fmt_video;
67     es_out_id_t *p_video;
68
69     es_format_t  fmt_sub;
70     es_out_id_t  *p_sub;
71
72     int64_t     i_pcr;
73     int64_t     i_time;
74     int64_t     i_pcr_inc;
75
76     bool b_start_record;
77 };
78
79 static int Demux  ( demux_t *p_demux );
80 static int Control( demux_t *p_demux, int i_query, va_list args );
81
82 static int ReSynch( demux_t *p_demux );
83
84 static int ReadNSVf( demux_t *p_demux );
85 static int ReadNSVs( demux_t *p_demux );
86
87 /*****************************************************************************
88  * Open
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     demux_t     *p_demux = (demux_t*)p_this;
93     demux_sys_t *p_sys;
94
95     const uint8_t *p_peek;
96
97     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
98         return VLC_EGENERIC;
99
100     if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
101     {
102        /* In case we had force this demuxer we try to resynch */
103         if( !p_demux->b_force || ReSynch( p_demux ) )
104             return VLC_EGENERIC;
105     }
106
107     /* Fill p_demux field */
108     p_demux->pf_demux = Demux;
109     p_demux->pf_control = Control;
110     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
111
112     es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
113     p_sys->p_audio = NULL;
114
115     es_format_Init( &p_sys->fmt_video, VIDEO_ES, 0 );
116     p_sys->p_video = NULL;
117
118     es_format_Init( &p_sys->fmt_sub, SPU_ES, 0 );
119     p_sys->p_sub = NULL;
120
121     p_sys->i_pcr   = 0;
122     p_sys->i_time  = 0;
123     p_sys->i_pcr_inc = 0;
124
125     p_sys->b_start_record = false;
126
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * Close
132  *****************************************************************************/
133 static void Close( vlc_object_t *p_this )
134 {
135     demux_t     *p_demux = (demux_t*)p_this;
136     demux_sys_t *p_sys = p_demux->p_sys;
137
138     free( p_sys );
139 }
140
141
142 /*****************************************************************************
143  * Demux:
144  *****************************************************************************/
145 static int Demux( demux_t *p_demux )
146 {
147     demux_sys_t *p_sys = p_demux->p_sys;
148
149     uint8_t     header[5];
150     const uint8_t *p_peek;
151
152     int         i_size;
153     block_t     *p_frame;
154
155     for( ;; )
156     {
157         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
158         {
159             msg_Warn( p_demux, "cannot peek" );
160             return 0;
161         }
162
163         if( !memcmp( p_peek, "NSVf", 4 ) )
164         {
165             if( ReadNSVf( p_demux ) )
166                 return -1;
167         }
168         else if( !memcmp( p_peek, "NSVs", 4 ) )
169         {
170             if( p_sys->b_start_record )
171             {
172                 /* Enable recording once synchronized */
173                 stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, true, "nsv" );
174                 p_sys->b_start_record = false;
175             }
176
177             if( ReadNSVs( p_demux ) )
178                 return -1;
179             break;
180         }
181         else if( GetWLE( p_peek ) == 0xbeef )
182         {
183             /* Next frame of the current NSVs chunk */
184             if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
185             {
186                 msg_Warn( p_demux, "cannot read" );
187                 return 0;
188             }
189             break;
190         }
191         else
192         {
193             msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
194             if( ReSynch( p_demux ) )
195                 return -1;
196         }
197     }
198
199     if( stream_Read( p_demux->s, header, 5 ) < 5 )
200     {
201         msg_Warn( p_demux, "cannot read" );
202         return 0;
203     }
204
205     /* Set PCR */
206     es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
207
208     /* Read video */
209     i_size = ( header[0] >> 4 ) | ( header[1] << 4 ) | ( header[2] << 12 );
210     if( i_size > 0 )
211     {
212         /* extra data ? */
213         if( (header[0]&0x0f) != 0x0 )
214         {
215             uint8_t      aux[6];
216             int          i_aux;
217             vlc_fourcc_t fcc;
218             if( stream_Read( p_demux->s, aux, 6 ) < 6 )
219             {
220                 msg_Warn( p_demux, "cannot read" );
221                 return 0;
222             }
223             i_aux = GetWLE( aux );
224             fcc   = VLC_FOURCC( aux[2], aux[3], aux[4], aux[5] );
225
226             msg_Dbg( p_demux, "Belekas: %d - size=%d fcc=%4.4s",
227                      header[0]&0xf, i_aux, (char*)&fcc );
228
229             if( fcc == VLC_FOURCC( 'S', 'U', 'B', 'T' ) && i_aux > 2 )
230             {
231                 if( p_sys->p_sub == NULL )
232                 {
233                     p_sys->fmt_sub.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
234                     p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
235                     es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
236                 }
237                 stream_Read( p_demux->s, NULL, 2 );
238
239                 if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
240                 {
241                     uint8_t *p = p_frame->p_buffer;
242
243                     while( p < &p_frame->p_buffer[p_frame->i_buffer] && *p != 0 )
244                     {
245                         p++;
246                     }
247                     if( *p == 0 && p + 1 < &p_frame->p_buffer[p_frame->i_buffer] )
248                     {
249                         p_frame->i_buffer -= p + 1 - p_frame->p_buffer;
250                         p_frame->p_buffer = p + 1;
251                     }
252
253                     /* Skip the first part (it is the language name) */
254                     p_frame->i_pts = VLC_TS_0 + p_sys->i_pcr;
255                     p_frame->i_dts = VLC_TS_0 + p_sys->i_pcr + 4000000;    /* 4s */
256
257                     es_out_Send( p_demux->out, p_sys->p_sub, p_frame );
258                 }
259             }
260             else
261             {
262                 /* We skip this extra data */
263                 if( stream_Read( p_demux->s, NULL, i_aux ) < i_aux )
264                 {
265                     msg_Warn( p_demux, "cannot read" );
266                     return 0;
267                 }
268             }
269             i_size -= 6 + i_aux;
270         }
271
272         /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
273         if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
274         {
275             p_frame->i_dts = VLC_TS_0 + p_sys->i_pcr;
276             es_out_Send( p_demux->out, p_sys->p_video, p_frame );
277         }
278     }
279
280     /* Read audio */
281     i_size = header[3] | ( header[4] << 8 );
282     if( i_size > 0 )
283     {
284         /* msg_Dbg( p_demux, "frame audio size=%d", i_size ); */
285         if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
286         {
287             uint8_t h[4];
288             stream_Read( p_demux->s, h, 4 );
289
290             p_sys->fmt_audio.audio.i_channels = h[1];
291             p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
292
293             i_size -= 4;
294         }
295         if( p_sys->p_audio == NULL )
296         {
297             p_sys->p_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
298         }
299
300         if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
301         {
302             p_frame->i_dts =
303             p_frame->i_pts = VLC_TS_0 + p_sys->i_pcr;
304             es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
305         }
306     }
307
308     p_sys->i_pcr += p_sys->i_pcr_inc;
309     if( p_sys->i_time >= 0 )
310     {
311         p_sys->i_time += p_sys->i_pcr_inc;
312     }
313
314     return 1;
315 }
316
317 /*****************************************************************************
318  * Control:
319  *****************************************************************************/
320 static int Control( demux_t *p_demux, int i_query, va_list args )
321 {
322     demux_sys_t *p_sys = p_demux->p_sys;
323     double f, *pf;
324     bool b_bool, *pb_bool;
325     int64_t i64, *pi64;
326
327     switch( i_query )
328     {
329         case DEMUX_GET_POSITION:
330             pf = (double*) va_arg( args, double* );
331             i64 = stream_Size( p_demux->s );
332             if( i64 > 0 )
333             {
334                 double current = stream_Tell( p_demux->s );
335                 *pf = current / (double)i64;
336             }
337             else
338             {
339                 *pf = 0.0;
340             }
341             return VLC_SUCCESS;
342
343         case DEMUX_SET_POSITION:
344             f = (double) va_arg( args, double );
345             i64 = stream_Size( p_demux->s );
346
347             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
348                 return VLC_EGENERIC;
349
350             p_sys->i_time = -1; /* Invalidate time display */
351             return VLC_SUCCESS;
352
353         case DEMUX_GET_TIME:
354             pi64 = (int64_t*)va_arg( args, int64_t * );
355             if( p_sys->i_time < 0 )
356             {
357                 *pi64 = 0;
358                 return VLC_EGENERIC;
359             }
360             *pi64 = p_sys->i_time;
361             return VLC_SUCCESS;
362
363 #if 0
364         case DEMUX_GET_LENGTH:
365             pi64 = (int64_t*)va_arg( args, int64_t * );
366             if( p_sys->i_mux_rate > 0 )
367             {
368                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
369                 return VLC_SUCCESS;
370             }
371             *pi64 = 0;
372             return VLC_EGENERIC;
373
374 #endif
375         case DEMUX_GET_FPS:
376             pf = (double*)va_arg( args, double * );
377             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
378             return VLC_SUCCESS;
379
380         case DEMUX_CAN_RECORD:
381             pb_bool = (bool*)va_arg( args, bool * );
382             *pb_bool = true;
383             return VLC_SUCCESS;
384
385         case DEMUX_SET_RECORD_STATE:
386             b_bool = (bool)va_arg( args, int );
387
388             if( !b_bool )
389                 stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
390             p_sys->b_start_record = b_bool;
391             return VLC_SUCCESS;
392
393
394         case DEMUX_SET_TIME:
395         default:
396             return VLC_EGENERIC;
397     }
398 }
399
400 /*****************************************************************************
401  * ReSynch:
402  *****************************************************************************/
403 static int ReSynch( demux_t *p_demux )
404 {
405     const uint8_t *p_peek;
406     int      i_skip;
407     int      i_peek;
408
409     while( vlc_object_alive (p_demux) )
410     {
411         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
412         {
413             return VLC_EGENERIC;
414         }
415         i_skip = 0;
416
417         while( i_skip < i_peek - 4 )
418         {
419             if( !memcmp( p_peek, "NSVf", 4 )
420              || !memcmp( p_peek, "NSVs", 4 ) )
421             {
422                 if( i_skip > 0 )
423                 {
424                     stream_Read( p_demux->s, NULL, i_skip );
425                 }
426                 return VLC_SUCCESS;
427             }
428             p_peek++;
429             i_skip++;
430         }
431
432         stream_Read( p_demux->s, NULL, i_skip );
433     }
434     return VLC_EGENERIC;
435 }
436
437 /*****************************************************************************
438  * ReadNSVf:
439  *****************************************************************************/
440 static int ReadNSVf( demux_t *p_demux )
441 {
442     /* demux_sys_t *p_sys = p_demux->p_sys; */
443     const uint8_t     *p;
444     int         i_size;
445
446     msg_Dbg( p_demux, "new NSVf chunk" );
447     if( stream_Peek( p_demux->s, &p, 8 ) < 8 )
448     {
449         return VLC_EGENERIC;
450     }
451
452     i_size = GetDWLE( &p[4] );
453     msg_Dbg( p_demux, "    - size=%d", i_size );
454
455     return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
456 }
457 /*****************************************************************************
458  * ReadNSVs:
459  *****************************************************************************/
460 static int ReadNSVs( demux_t *p_demux )
461 {
462     demux_sys_t *p_sys = p_demux->p_sys;
463     uint8_t      header[19];
464     vlc_fourcc_t fcc;
465
466     if( stream_Read( p_demux->s, header, 19 ) < 19 )
467     {
468         msg_Warn( p_demux, "cannot read" );
469         return VLC_EGENERIC;
470     }
471
472     /* Video */
473     switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
474     {
475         case VLC_FOURCC( 'V', 'P', '3', ' ' ):
476         case VLC_FOURCC( 'V', 'P', '3', '0' ):
477             fcc = VLC_FOURCC( 'V', 'P', '3', '0' );
478             break;
479
480         case VLC_FOURCC( 'V', 'P', '3', '1' ):
481             fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
482             break;
483
484         case VLC_FOURCC( 'V', 'P', '5', ' ' ):
485         case VLC_FOURCC( 'V', 'P', '5', '0' ):
486             fcc = VLC_FOURCC( 'V', 'P', '5', '0' );
487             break;
488         case VLC_FOURCC( 'V', 'P', '6', '0' ):
489         case VLC_FOURCC( 'V', 'P', '6', '1' ):
490         case VLC_FOURCC( 'V', 'P', '6', '2' ):
491         case VLC_FOURCC( 'H', '2', '6', '4' ):
492         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
493             break;
494         default:
495             msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
496             break;
497     }
498     if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
499     {
500         es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
501         p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
502         p_sys->fmt_video.video.i_height = GetWLE( &header[14] );
503         if( p_sys->p_video )
504         {
505             es_out_Del( p_demux->out, p_sys->p_video );
506         }
507         p_sys->p_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
508
509         msg_Dbg( p_demux, "    - video `%4.4s' %dx%d",
510                  (char*)&fcc,
511                  p_sys->fmt_video.video.i_width,
512                  p_sys->fmt_video.video.i_height );
513     }
514
515     /* Audio */
516     switch( ( fcc = VLC_FOURCC( header[8], header[9], header[10], header[11] ) ) )
517     {
518         case VLC_FOURCC( 'M', 'P', '3', ' ' ):
519             fcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
520             break;
521         case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
522             fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
523             break;
524         case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
525         case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
526             fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
527             break;
528         case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
529             fcc = VLC_FOURCC( 's', 'p', 'x', ' ' );
530             break;
531         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
532             break;
533         default:
534             msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
535             break;
536     }
537
538     if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
539     {
540         msg_Dbg( p_demux, "    - audio `%4.4s'", (char*)&fcc );
541
542         if( p_sys->p_audio )
543         {
544             es_out_Del( p_demux->out, p_sys->p_audio );
545             p_sys->p_audio = NULL;
546         }
547         es_format_Init( &p_sys->fmt_audio, AUDIO_ES, fcc );
548     }
549
550     if( header[16]&0x80 )
551     {
552         /* Fractional frame rate */
553         switch( header[16]&0x03 )
554         {
555             case 0: /* 30 fps */
556                 p_sys->i_pcr_inc = 33333; /* 300000/9 */
557                 break;
558             case 1: /* 29.97 fps */
559                 p_sys->i_pcr_inc = 33367; /* 300300/9 */
560                 break;
561             case 2: /* 25 fps */
562                 p_sys->i_pcr_inc = 40000; /* 360000/9 */
563                 break;
564             case 3: /* 23.98 fps */
565                 p_sys->i_pcr_inc = 41700; /* 375300/9 */
566                 break;
567         }
568
569         if( header[16] < 0xc0 )
570             p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
571         else
572             p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
573     }
574     else if( header[16] != 0 )
575     {
576         /* Integer frame rate */
577         p_sys->i_pcr_inc = 1000000 / header[16];
578     }
579     else
580     {
581         msg_Dbg( p_demux, "invalid fps (0x00)" );
582         p_sys->i_pcr_inc = 40000;
583     }
584     //msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
585
586     return VLC_SUCCESS;
587 }
588