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