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