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