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