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