]> git.sesse.net Git - vlc/blob - modules/demux/real.c
Real/Sipr, try to use a correct flavor detection, without using the MPlayer hacks...
[vlc] / modules / demux / real.c
1 /*****************************************************************************
2  * real.c: Real demuxer.
3  *****************************************************************************
4  * Copyright (C) 2004, 2006-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  * Status of this demuxer:
26  * Real Media format
27  * -----------------
28  *
29  * version v3 w/ 14_4/lpcJ is ok.
30  * version v4/5: - atrac3 is ok.
31  *               - cook is ok.
32  *               - raac, racp are ok.
33  *               - dnet is twisted "The byte order of the data is reversed
34  *                                  from standard AC3"
35  *               - 28_8 seem problematic.
36  *               - sipr should be fine, but our decoder suxx :)
37  *               - ralf is unsupported, but hardly any sample exist.
38  *               - mp3 is unsupported, one sample exists...
39  *
40  * Real Audio Only
41  * ---------------
42  * Not supported...
43  */
44
45 /*****************************************************************************
46  * Preamble
47  *****************************************************************************/
48 #include <vlc/vlc.h>
49
50
51 #include <vlc_demux.h>
52 #include <vlc_charset.h>
53 #include <vlc_meta.h>
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 static int  Open    ( vlc_object_t * );
59 static void Close  ( vlc_object_t * );
60
61 vlc_module_begin();
62     set_description( _("Real demuxer" ) );
63     set_capability( "demux2", 15 );
64     set_category( CAT_INPUT );
65     set_subcategory( SUBCAT_INPUT_DEMUX );
66     set_callbacks( Open, Close );
67     add_shortcut( "real" );
68     add_shortcut( "rm" );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74
75 typedef struct
76 {
77     int         i_id;
78     es_format_t fmt;
79
80     es_out_id_t *p_es;
81
82     int         i_frame;
83     block_t     *p_frame;
84
85     int         i_subpacket_h;
86     int         i_subpacket_size;
87     int         i_coded_frame_size;
88     int         i_frame_size;
89
90     int         i_subpacket;
91     int         i_subpackets;
92     block_t     **p_subpackets;
93     int         i_out_subpacket;
94
95 } real_track_t;
96
97 struct demux_sys_t
98 {
99     int64_t  i_data_offset;
100     int64_t  i_data_size;
101     uint32_t i_data_packets_count;
102     uint32_t i_data_packets;
103     int64_t  i_data_offset_next;
104
105     int  i_our_duration;
106     int  i_mux_rate;
107
108     char* psz_title;
109     char* psz_artist;
110     char* psz_copyright;
111     char* psz_description;
112
113     int          i_track;
114     real_track_t **track;
115
116     uint8_t buffer[65536];
117
118     int64_t     i_pcr;
119 };
120
121 static int Demux( demux_t *p_demux );
122 static int Control( demux_t *p_demux, int i_query, va_list args );
123
124 static int HeaderRead( demux_t *p_demux );
125 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
126
127 /*****************************************************************************
128  * Open
129  *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
131 {
132     demux_t     *p_demux = (demux_t*)p_this;
133     demux_sys_t *p_sys;
134
135     const uint8_t *p_peek;
136
137     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
138     if( !memcmp( p_peek, ".ra", 3 ) )
139     {
140         msg_Warn( p_demux, ".ra files unsuported" );
141         return VLC_EGENERIC;
142     }
143     if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
144
145     /* Fill p_demux field */
146     p_demux->pf_demux = Demux;
147     p_demux->pf_control = Control;
148     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
149     memset( p_sys, 0, sizeof( demux_sys_t ) );
150     p_sys->i_data_offset = 0;
151     p_sys->i_track = 0;
152     p_sys->track   = NULL;
153     p_sys->i_pcr   = 1;
154
155
156     /* Parse the headers */
157     if( HeaderRead( p_demux ) )
158     {
159         int i;
160         msg_Err( p_demux, "invalid header" );
161         for( i = 0; i < p_sys->i_track; i++ )
162         {
163             real_track_t *tk = p_sys->track[i];
164
165             if( tk->p_es )
166             {
167                 es_out_Del( p_demux->out, tk->p_es );
168             }
169             free( tk );
170         }
171         if( p_sys->i_track > 0 )
172         {
173             free( p_sys->track );
174         }
175         free( p_sys );
176         return VLC_EGENERIC;
177     }
178
179     return VLC_SUCCESS;
180 }
181
182 /*****************************************************************************
183  * Close
184  *****************************************************************************/
185 static void Close( vlc_object_t *p_this )
186 {
187     demux_t *p_demux = (demux_t*)p_this;
188     demux_sys_t *p_sys = p_demux->p_sys;
189     int i;
190
191     for( i = 0; i < p_sys->i_track; i++ )
192     {
193         real_track_t *tk = p_sys->track[i];
194         int j = tk->i_subpackets;
195
196         if( tk->p_frame ) block_Release( tk->p_frame );
197         es_format_Clean( &tk->fmt );
198
199         while(  j-- )
200         {
201             if( tk->p_subpackets[ j ] )
202                 block_Release( tk->p_subpackets[ j ] );
203         }
204         if( tk->i_subpackets ) free( tk->p_subpackets );
205
206         free( tk );
207     }
208
209     if( p_sys->psz_title ) free( p_sys->psz_title );
210     if( p_sys->psz_artist ) free( p_sys->psz_artist );
211     if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
212     if( p_sys->psz_description ) free( p_sys->psz_description );
213
214     if( p_sys->i_track > 0 ) free( p_sys->track );
215     free( p_sys );
216 }
217
218
219 /*****************************************************************************
220  * Demux:
221  *****************************************************************************/
222 static int Demux( demux_t *p_demux )
223 {
224     demux_sys_t *p_sys = p_demux->p_sys;
225     uint8_t     header[18];
226     int         i_size, i_id, i_flags, i;
227     int64_t     i_pts;
228     real_track_t *tk = NULL;
229     vlc_bool_t  b_selected;
230
231     if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
232         p_sys->i_data_packets_count )
233     {
234         if( stream_Read( p_demux->s, header, 18 ) < 18 )
235         {
236             return 0;
237         }
238         if( strncmp( (char *)header, "DATA", 4 ) )
239         {
240             return 0;
241         }
242         p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
243         p_sys->i_data_size   = GetDWBE( &header[4] );
244         p_sys->i_data_packets_count = GetDWBE( &header[10] );
245         p_sys->i_data_packets = 0;
246         p_sys->i_data_offset_next = GetDWBE( &header[14] );
247
248         msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
249                  p_sys->i_data_packets_count,
250                  (uint32_t)p_sys->i_data_offset_next );
251     }
252
253     if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
254
255     i_size = GetWBE( &header[2] ) - 12;
256     i_id   = GetWBE( &header[4] );
257     i_pts  = 1000 * GetDWBE( &header[6] );
258     i_pts += 1000; /* Avoid 0 pts */
259     i_flags= header[11]; /* flags 0x02 -> keyframe */
260
261 #if 0
262     msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
263              p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
264 #endif
265
266     p_sys->i_data_packets++;
267
268     stream_Read( p_demux->s, p_sys->buffer, i_size );
269
270     for( i = 0; i < p_sys->i_track; i++ )
271     {
272         if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
273     }
274
275     if( tk == NULL )
276     {
277         msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
278         return 1;
279     }
280     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
281
282     if( tk->fmt.i_cat == VIDEO_ES && b_selected )
283     {
284         uint8_t *p = p_sys->buffer;
285
286         while( p < &p_sys->buffer[i_size - 2] )
287         {
288             uint8_t h = *p++;
289             int     i_len = 0;
290             int     i_copy;
291             int     i_subseq = 0;
292             int     i_seqnum = 0;
293             int     i_offset = 0;
294
295             if( (h&0xc0) == 0x40 )
296             {
297                 /* Short header */
298                 p++;
299                 i_len = &p_sys->buffer[i_size] - p;
300             }
301             else
302             {
303                 if( (h&0x40) == 0 )
304                 {
305                     i_subseq = (*p++)&0x7f;
306                 }
307                 i_len = (p[0] << 8)|p[1]; p += 2;
308                 if( (i_len&0xc000) == 0 )
309                 {
310                     i_len <<= 16;
311                     i_len |= (p[0] << 8)|p[1]; p += 2;
312                     i_len &= 0x3fffffff;
313                 }
314                 else
315                 {
316                     i_len &= 0x3fff;
317                 }
318
319                 i_offset = (p[0] << 8)|p[1]; p += 2;
320                 if( (i_offset&0xc000) == 0 )
321                 {
322                     i_offset <<= 16;
323                     i_offset |= (p[0] << 8)|p[1]; p += 2;
324                     i_offset &= 0x3fffffff;
325                 }
326                 else
327                 {
328                     i_offset &= 0x3fff;
329                 }
330                 i_seqnum = *p++;
331             }
332
333             i_copy = i_len - i_offset;
334             if( i_copy > &p_sys->buffer[i_size] - p )
335             {
336                 i_copy = &p_sys->buffer[i_size] - p;
337             }
338             else if( i_copy < 0 )
339             {
340                 break;
341             }
342
343             msg_Dbg( p_demux, "    - len=%d offset=%d size=%d subseq=%d seqnum=%d",
344                      i_len, i_offset, i_copy, i_subseq, i_seqnum );
345
346             if( (h&0xc0) == 0x80 )
347             {
348                 /* last fragment -> fixes */
349                 i_copy = i_offset;
350                 i_offset = i_len - i_copy;
351                 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
352                          i_copy, i_offset );
353             }
354
355             if( tk->p_frame &&
356                 ( tk->p_frame->i_dts != i_pts ||
357                   tk->i_frame != i_len ) )
358             {
359                 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
360
361                 if( p_sys->i_pcr < tk->p_frame->i_dts )
362                 {
363                     p_sys->i_pcr = tk->p_frame->i_dts;
364
365                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
366                                     (int64_t)p_sys->i_pcr );
367                 }
368                 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
369
370                 tk->i_frame = 0;
371                 tk->p_frame = NULL;
372             }
373
374             if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
375             {
376                 /* no fragment */
377                 i_len = i_copy;
378                 i_offset = 0;
379             }
380
381
382             if( tk->p_frame == NULL )
383             {
384                 msg_Dbg( p_demux, "new frame size=%d", i_len );
385                 tk->i_frame = i_len;
386                 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
387                 {
388                     return -1;
389                 }
390                 memset( &tk->p_frame->p_buffer[8], 0, i_len );
391                 tk->p_frame->i_dts = i_pts;
392                 tk->p_frame->i_pts = i_pts;
393
394                 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
395                 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0;      /* chunk counts */
396             }
397
398             if( i_offset < tk->i_frame)
399             {
400                 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
401
402                 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
403                          i_ck, i_offset, i_copy );
404
405                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
406
407                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
408             }
409
410             p += i_copy;
411
412             if( (h&0xc0) != 0x80 )
413             {
414                 break;
415             }
416
417 #if 0
418             if( tk->p_frame )
419             {
420                 /* append data */
421                 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
422
423                 if( (h&0xc0) == 0x80 )
424                 {
425                     /* last fragment */
426                     i_copy = i_offset;
427                     i_offset = i_len - i_offset;
428
429                     ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
430                     memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
431                     p += i_copy;
432
433                     if( p_sys->i_pcr < tk->p_frame->i_dts )
434                     {
435                         p_sys->i_pcr = tk->p_frame->i_dts;
436                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
437                                         (int64_t)p_sys->i_pcr );
438                     }
439                     es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
440
441                     tk->i_frame = 0;
442                     tk->p_frame = NULL;
443
444                     continue;
445                 }
446
447                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
448                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
449                 break;
450             }
451
452             if( (h&0xc0) != 0x00 )
453             {
454                 block_t *p_frame;
455
456                 /* not fragmented */
457                 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
458                 {
459                     return -1;
460                 }
461                 p_frame->i_dts = i_pts;
462                 p_frame->i_pts = i_pts;
463
464                 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
465                 ((uint32_t*)p_frame->p_buffer)[1] = 1;
466                 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
467                 memcpy( &p_frame->p_buffer[8], p, i_copy );
468
469                 p += i_copy;
470
471                 if( p_sys->i_pcr < p_frame->i_dts )
472                 {
473                     p_sys->i_pcr = p_frame->i_dts;
474                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
475                                     (int64_t)p_sys->i_pcr );
476                 }
477                 es_out_Send( p_demux->out, tk->p_es, p_frame );
478             }
479             else
480             {
481                 /* First fragment */
482                 tk->i_frame = i_len;
483                 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
484                 {
485                     return -1;
486                 }
487                 memset( &tk->p_frame->p_buffer[8], 0, i_len );
488                 tk->p_frame->i_dts = i_pts;
489                 tk->p_frame->i_pts = i_pts;
490
491                 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
492                 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1;      /* chunk counts */
493                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
494                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
495
496                 break;
497             }
498 #endif
499         }
500     }
501     else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
502     {
503         /* Set PCR */
504         if( p_sys->i_pcr < i_pts )
505         {
506             p_sys->i_pcr = i_pts;
507             es_out_Control( p_demux->out, ES_OUT_SET_PCR,
508                             (int64_t)p_sys->i_pcr );
509         }
510
511         if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
512         {
513             int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
514             uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
515
516             int i;
517             for( i = 0; i < i_sub; i++ )
518             {
519                 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
520                 block_t *p_block = block_New( p_demux, i_sub_size );
521                 if( p_block )
522                 {
523                     memcpy( p_block->p_buffer, p_sub, i_sub_size );
524                     p_sub += i_sub_size;
525
526                     p_block->i_dts =
527                     p_block->i_pts = ( i == 0 ? i_pts : 0 );
528
529                     es_out_Send( p_demux->out, tk->p_es, p_block );
530                 }
531             }
532         }
533         else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
534                  tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
535                  tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
536                  tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
537         {
538             uint8_t *p_buf = p_sys->buffer;
539             int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
540             int i_index, i;
541
542             /* Sanity check */
543             if( i_flags & 2 ) y = tk->i_subpacket = 0;
544
545             if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
546                 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
547             for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
548             {
549                 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
550                 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
551                 p_buf += tk->i_subpacket_size;
552
553                 i_index = tk->i_subpacket_h * i +
554                     ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
555
556                 p_block->i_dts = p_block->i_pts = i_pts;
557                 tk->p_subpackets[i_index] = p_block;
558                 tk->i_subpacket++;
559             }
560
561             if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
562                 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
563             for( i = 0; i < tk->i_subpacket_h / 2; i++ )
564             {
565                 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
566                 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
567                 p_buf += tk->i_coded_frame_size;
568
569                 i_index = (i * 2 * tk->i_frame_size) /
570                     tk->i_coded_frame_size + y;
571
572                 p_block->i_dts = p_block->i_pts = i_pts;
573                 tk->p_subpackets[i_index] = p_block;
574                 tk->i_subpacket++;
575             }
576
577             while( tk->i_out_subpacket != tk->i_subpackets &&
578                    tk->p_subpackets[tk->i_out_subpacket] )
579             {
580                 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
581                 tk->p_subpackets[tk->i_out_subpacket] = 0;
582
583                 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
584                 es_out_Send( p_demux->out, tk->p_es, p_block );
585
586                 tk->i_out_subpacket++;
587             }
588
589             if( tk->i_subpacket == tk->i_subpackets &&
590                 tk->i_out_subpacket != tk->i_subpackets )
591             {
592                 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
593                           "this shouldn't happen" );
594             }
595
596             if( tk->i_subpacket == tk->i_subpackets )
597             {
598                 tk->i_subpacket = 0;
599                 tk->i_out_subpacket = 0;
600             }
601         }
602         else
603         {
604             block_t *p_block = block_New( p_demux, i_size );
605
606             if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
607             {
608                 uint8_t *src = p_sys->buffer;
609                 uint8_t *dst = p_block->p_buffer;
610
611                 /* byte swap data */
612                 while( dst < &p_block->p_buffer[i_size- 1])
613                 {
614                     *dst++ = src[1];
615                     *dst++ = src[0];
616
617                     src += 2;
618                 }
619             }
620             else
621             {
622                 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
623             }
624             p_block->i_dts = p_block->i_pts = i_pts;
625             es_out_Send( p_demux->out, tk->p_es, p_block );
626         }
627     }
628
629
630     return 1;
631 }
632
633 /*****************************************************************************
634  * Control:
635  *****************************************************************************/
636 static int Control( demux_t *p_demux, int i_query, va_list args )
637 {
638     demux_sys_t *p_sys = p_demux->p_sys;
639 #if 0
640     double f, *pf;
641     int64_t i64;
642 #endif
643     int64_t *pi64;
644
645     switch( i_query )
646     {
647 #if 0
648         case DEMUX_GET_POSITION:
649             pf = (double*) va_arg( args, double* );
650             i64 = stream_Size( p_demux->s );
651             if( i64 > 0 )
652             {
653                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
654             }
655             else
656             {
657                 *pf = 0.0;
658             }
659             return VLC_SUCCESS;
660
661         case DEMUX_SET_POSITION:
662             f = (double) va_arg( args, double );
663             i64 = stream_Size( p_demux->s );
664
665             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
666
667             return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
668
669         case DEMUX_GET_TIME:
670             pi64 = (int64_t*)va_arg( args, int64_t * );
671             if( p_sys->i_mux_rate > 0 )
672             {
673                 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
674                 return VLC_SUCCESS;
675             }
676             *pi64 = 0;
677             return VLC_EGENERIC;
678 #endif
679
680         case DEMUX_GET_LENGTH:
681             pi64 = (int64_t*)va_arg( args, int64_t * );
682  
683             /* the commented following lines are fen's implementation, which doesn't seem to
684              * work for one reason or another -- FK */
685             /*if( p_sys->i_mux_rate > 0 )
686             {
687                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
688                 return VLC_SUCCESS;
689             }*/
690             if( p_sys->i_our_duration > 0 )
691             {
692                 /* our stored duration is in ms, so... */
693                 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
694  
695                 return VLC_SUCCESS;
696             }
697             *pi64 = 0;
698             return VLC_EGENERIC;
699
700         case DEMUX_GET_META:
701         {
702             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
703
704             /* the core will crash if we provide NULL strings, so check
705              * every string first */
706             if( p_sys->psz_title )
707                 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
708             if( p_sys->psz_artist )
709                 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
710             if( p_sys->psz_copyright )
711                 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
712             if( p_sys->psz_description )
713                 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
714             return VLC_SUCCESS;
715         }
716
717         case DEMUX_SET_TIME:
718         case DEMUX_GET_FPS:
719         default:
720             return VLC_EGENERIC;
721     }
722     return VLC_EGENERIC;
723 }
724
725 /*****************************************************************************
726  * HeaderRead:
727  *****************************************************************************/
728 static int HeaderRead( demux_t *p_demux )
729 {
730     demux_sys_t *p_sys = p_demux->p_sys;
731     uint8_t header[100];    /* FIXME */
732
733     uint32_t    i_id;
734     uint32_t    i_size;
735     int64_t     i_skip;
736     int         i_version;
737
738     for( ;; )
739     {
740         /* Read the header */
741         if( stream_Read( p_demux->s, header, 10 ) < 10 )
742         {
743             return VLC_EGENERIC;
744         }
745         i_id        = VLC_FOURCC( header[0], header[1], header[2], header[3] );
746         i_size      = GetDWBE( &header[4] );
747         i_version   = GetWBE( &header[8] );
748
749         msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
750                  (char*)&i_id, i_size, i_version );
751
752         if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
753         {
754             msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
755             return VLC_EGENERIC;
756         }
757         i_skip = i_size - 10;
758
759         if( i_id == VLC_FOURCC('.','R','M','F') )
760         {
761             if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
762             msg_Dbg( p_demux, "    - file version=0x%x num headers=%d",
763                      GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
764
765             i_skip -= 8;
766         }
767         else if( i_id == VLC_FOURCC('P','R','O','P') )
768         {
769             int i_flags;
770
771             if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
772
773             msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
774                      GetDWBE(&header[0]), GetDWBE(&header[4]) );
775             msg_Dbg( p_demux, "    - max packet size=%d avg bitrate=%d",
776                      GetDWBE(&header[8]), GetDWBE(&header[12]) );
777             msg_Dbg( p_demux, "    - packets count=%d", GetDWBE(&header[16]) );
778             msg_Dbg( p_demux, "    - duration=%d ms", GetDWBE(&header[20]) );
779             msg_Dbg( p_demux, "    - preroll=%d ms", GetDWBE(&header[24]) );
780             msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&header[28]) );
781             msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&header[32]) );
782             msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&header[36]) );
783  
784             /* set the duration for export in control */
785             p_sys->i_our_duration = (int)GetDWBE(&header[20]);
786  
787             i_flags = GetWBE(&header[38]);
788             msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
789                      i_flags,
790                      i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
791                      i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
792                      i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
793             i_skip -= 40;
794         }
795         else if( i_id == VLC_FOURCC('C','O','N','T') )
796         {
797             int i_len;
798             char *psz;
799  
800             /* FIXME FIXME: should convert from whatever the character
801              * encoding of the input meta data is to UTF-8. */
802
803             stream_Read( p_demux->s, header, 2 );
804             if( ( i_len = GetWBE( header ) ) > 0 )
805             {
806                 psz = malloc( i_len + 1 );
807                 stream_Read( p_demux->s, psz, i_len );
808                 psz[i_len] = '\0';
809
810                 msg_Dbg( p_demux, "    - title=`%s'", psz );
811                 EnsureUTF8( psz );
812                 asprintf( &p_sys->psz_title, psz );
813                 free( psz );
814                 i_skip -= i_len;
815             }
816             i_skip -= 2;
817
818             stream_Read( p_demux->s, header, 2 );
819             if( ( i_len = GetWBE( header ) ) > 0 )
820             {
821                 psz = malloc( i_len + 1 );
822                 stream_Read( p_demux->s, psz, i_len );
823                 psz[i_len] = '\0';
824
825                 msg_Dbg( p_demux, "    - author=`%s'", psz );
826                 EnsureUTF8( psz );
827                 asprintf( &p_sys->psz_artist, psz );
828                 free( psz );
829                 i_skip -= i_len;
830             }
831             i_skip -= 2;
832
833             stream_Read( p_demux->s, header, 2 );
834             if( ( i_len = GetWBE( header ) ) > 0 )
835             {
836                 psz = malloc( i_len + 1 );
837                 stream_Read( p_demux->s, psz, i_len );
838                 psz[i_len] = '\0';
839
840                 msg_Dbg( p_demux, "    - copyright=`%s'", psz );
841                 EnsureUTF8( psz );
842                 asprintf( &p_sys->psz_copyright, psz );
843                 free( psz );
844                 i_skip -= i_len;
845             }
846             i_skip -= 2;
847
848             stream_Read( p_demux->s, header, 2 );
849             if( ( i_len = GetWBE( header ) ) > 0 )
850             {
851                 psz = malloc( i_len + 1 );
852                 stream_Read( p_demux->s, psz, i_len );
853                 psz[i_len] = '\0';
854
855                 msg_Dbg( p_demux, "    - comment=`%s'", psz );
856                 EnsureUTF8( psz );
857                 asprintf( &p_sys->psz_description, psz );
858                 free( psz );
859                 i_skip -= i_len;
860             }
861             i_skip -= 2;
862         }
863         else if( i_id == VLC_FOURCC('M','D','P','R') )
864         {
865             /* Media properties header */
866             int  i_num;
867             int  i_len;
868             char *psz;
869
870             if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
871             i_num = GetWBE( header );
872             msg_Dbg( p_demux, "    - id=0x%x", i_num );
873             msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
874                      GetDWBE(&header[2]), GetDWBE(&header[6]) );
875             msg_Dbg( p_demux, "    - max packet size=%d avg packet size=%d",
876                      GetDWBE(&header[10]), GetDWBE(&header[14]) );
877             msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&header[18]) );
878             msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&header[22]) );
879             msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&header[26]) );
880  
881             i_skip -= 30;
882
883             stream_Read( p_demux->s, header, 1 );
884             if( ( i_len = header[0] ) > 0 )
885             {
886                 psz = malloc( i_len + 1 );
887                 stream_Read( p_demux->s, psz, i_len );
888                 psz[i_len] = '\0';
889
890                 msg_Dbg( p_demux, "    - name=`%s'", psz );
891                 free( psz );
892                 i_skip -= i_len;
893             }
894             i_skip--;
895
896             stream_Read( p_demux->s, header, 1 );
897             if( ( i_len = header[0] ) > 0 )
898             {
899                 psz = malloc( i_len + 1 );
900                 stream_Read( p_demux->s, psz, i_len );
901                 psz[i_len] = '\0';
902
903                 msg_Dbg( p_demux, "    - mime=`%s'", psz );
904                 free( psz );
905                 i_skip -= i_len;
906             }
907             i_skip--;
908
909             stream_Read( p_demux->s, header, 4 );
910             if( ( i_len = GetDWBE( header ) ) > 0 )
911             {
912                 ReadCodecSpecificData( p_demux, i_len, i_num );
913                 stream_Read( p_demux->s, NULL, i_len );
914
915                 i_skip -= i_len;
916             }
917             i_skip -= 4;
918         }
919         else if( i_id == VLC_FOURCC('D','A','T','A') )
920         {
921             stream_Read( p_demux->s, header, 8 );
922
923             p_sys->i_data_offset    = stream_Tell( p_demux->s ) - 10;
924             p_sys->i_data_size      = i_size;
925             p_sys->i_data_packets_count = GetDWBE( header );
926             p_sys->i_data_packets   = 0;
927             p_sys->i_data_offset_next = GetDWBE( &header[4] );
928
929             msg_Dbg( p_demux, "    - packets count=%d next=%u",
930                      p_sys->i_data_packets_count,
931                      (uint32_t)p_sys->i_data_offset_next );
932
933             /* we have finished the header */
934             break;
935         }
936         else
937         {
938             /* unknow header */
939             msg_Dbg( p_demux, "unknown chunk" );
940         }
941
942         if( i_skip < 0 ) return VLC_EGENERIC;
943         stream_Read( p_demux->s, NULL, i_skip );
944     }
945
946     /* TODO read index if possible */
947
948     return VLC_SUCCESS;
949 }
950
951 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
952 {
953     demux_sys_t *p_sys = p_demux->p_sys;
954     es_format_t fmt;
955     real_track_t *tk;
956     const uint8_t *p_peek;
957
958     msg_Dbg( p_demux, "    - specific data len=%d", i_len );
959     if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
960
961     if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
962     {
963         es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
964                         p_peek[10], p_peek[11] ) );
965         fmt.video.i_width = GetWBE( &p_peek[12] );
966         fmt.video.i_height= GetWBE( &p_peek[14] );
967
968         fmt.i_extra = 8;
969         fmt.p_extra = malloc( 8 );
970         ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
971         ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
972
973         msg_Dbg( p_demux, "    - video 0x%08x 0x%08x",
974                  ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
975
976         if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
977             GetDWBE( &p_peek[30] ) == 0x10003001 )
978         {
979             fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
980         }
981         else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
982                  GetDWBE( &p_peek[30] ) == 0x20100001 ||
983                  GetDWBE( &p_peek[30] ) == 0x20200002 )
984         {
985             fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
986         }
987         else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
988         {
989             fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
990         }
991         else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
992         {
993             fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
994         }
995
996         msg_Dbg( p_demux, "    - video %4.4s %dx%d",
997                  (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
998
999         tk = malloc( sizeof( real_track_t ) );
1000         tk->i_out_subpacket = 0;
1001         tk->i_subpacket = 0;
1002         tk->i_subpackets = 0;
1003         tk->p_subpackets = NULL;
1004         tk->i_id = i_num;
1005         tk->fmt = fmt;
1006         tk->i_frame = 0;
1007         tk->p_frame = NULL;
1008         tk->p_es = es_out_Add( p_demux->out, &fmt );
1009
1010         TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1011     }
1012     else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1013     {
1014         int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
1015         int i_frame_size, i_subpacket_size;
1016         char p_genr[4];
1017         int i_version = GetWBE( &p_peek[4] );   /* [0..3] = '.','r','a',0xfd */
1018         msg_Dbg( p_demux, "    - audio version=%d", i_version );
1019
1020         p_peek += 6;                                          /* 4 + version */
1021         es_format_Init( &fmt, AUDIO_ES, 0 );
1022
1023         if( i_version == 3 )
1024         {
1025             int i_len;
1026             char *psz;
1027
1028             i_header_size = GetWBE( p_peek ); p_peek += 2;  /* Size from now */
1029             p_peek += 10;                                         /* Unknown */
1030
1031             p_peek += 4;                                        /* Data Size */
1032
1033             /* Title */
1034             i_len = *p_peek ; p_peek++;
1035             if( i_len > 0 )
1036             {
1037                 psz = malloc( i_len + 1 );
1038                 memcpy( psz, p_peek, i_len );
1039                 psz[i_len] = '\0';
1040
1041                 msg_Dbg( p_demux, "    - title=`%s'", psz );
1042                 EnsureUTF8( psz );
1043                 asprintf( &p_sys->psz_title, psz );
1044                 free( psz );
1045             }
1046             p_peek += i_len;
1047
1048             /* Authors */
1049             i_len = *p_peek ; p_peek++;
1050             if( i_len > 0 )
1051             {
1052                 psz = malloc( i_len + 1 );
1053                 memcpy( psz, p_peek, i_len );
1054                 psz[i_len] = '\0';
1055
1056                 msg_Dbg( p_demux, "    - artist=`%s'", psz );
1057                 EnsureUTF8( psz );
1058                 asprintf( &p_sys->psz_artist, psz );
1059                 free( psz );
1060             }
1061             p_peek += i_len;
1062
1063             /* Copyright */
1064             i_len = *p_peek ; p_peek++;
1065             if( i_len > 0 )
1066             {
1067                 psz = malloc( i_len + 1 );
1068                 memcpy( psz, p_peek, i_len );
1069                 psz[i_len] = '\0';
1070
1071                 msg_Dbg( p_demux, "    - Copyright=`%s'", psz );
1072                 EnsureUTF8( psz );
1073                 asprintf( &p_sys->psz_copyright, psz );
1074                 free( psz );
1075             }
1076             p_peek += i_len;
1077
1078             /* Comment */
1079             i_len = *p_peek ; p_peek++;
1080             if( i_len > 0 )
1081             {
1082                 psz = malloc( i_len + 1 );
1083                 memcpy( psz, p_peek, i_len );
1084                 psz[i_len] = '\0';
1085
1086                 msg_Dbg( p_demux, "    - Comment=`%s'", psz );
1087                 EnsureUTF8( psz );
1088                 asprintf( &p_sys->psz_description, psz );
1089                 free( psz );
1090             }
1091             /* This might be unusefull */
1092             p_peek += i_len;
1093
1094             p_peek ++;                                           /* Unknown */
1095             p_peek ++;                                 /* FourCC length = 4 */
1096             memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1097             /* Up to here :) */
1098
1099             fmt.audio.i_channels = 1;      /* This is always the case in rm3 */
1100             fmt.audio.i_rate = 8000;
1101
1102             msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1103                  (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1104         }
1105         else /* RMF version 4/5 */
1106         {
1107             p_peek += 2;                                           /* 00 00 */
1108             p_peek += 4;                                    /* .ra4 or .ra5 */
1109             p_peek += 4;                                       /* data size */
1110             p_peek += 2;                                /* version (4 or 5) */
1111             i_header_size = GetDWBE( p_peek ); p_peek += 4;   /* header size */
1112             i_flavor = GetWBE( p_peek ); p_peek += 2;        /* codec flavor */
1113             i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1114             p_peek += 4;                                               /* ?? */
1115             p_peek += 4;                                               /* ?? */
1116             p_peek += 4;                                               /* ?? */
1117             i_subpacket_h = GetWBE( p_peek ); p_peek += 2;              /* 1 */
1118             i_frame_size = GetWBE( p_peek ); p_peek += 2;      /* frame size */
1119             i_subpacket_size = GetWBE( p_peek ); p_peek += 2;  /* subpacket_size */
1120             p_peek += 2;                                               /* ?? */
1121
1122             if( i_version == 5 ) p_peek += 6;                 /* 0, srate, 0 */
1123
1124             fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1125             p_peek += 2;                                               /* ?? */
1126             fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1127             fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1128             fmt.audio.i_blockalign = i_frame_size;
1129
1130             if( i_version == 5 )
1131             {
1132                 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4;    /* genr */
1133                 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1134             }
1135             else
1136             {
1137                 p_peek += p_peek[0] + 1;                          /* descr 1 */
1138                 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 );    /* descr 2 */
1139                 p_peek += p_peek[0] + 1;
1140             }
1141
1142             msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1143                  (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1144
1145             p_peek += 3;                                               /* ?? */
1146             if( i_version == 5 ) p_peek++;
1147             /* Extra Data then: DWord + byte[] */
1148             fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1149         }
1150
1151         switch( fmt.i_codec )
1152         {
1153         case VLC_FOURCC('1','4','_','4'):
1154             /* fmt.audio.i_blockalign = 0x14 */
1155             break;
1156         case VLC_FOURCC('l','p','c','J'):
1157             /* fmt.audio.i_blockalign = 0x14 */
1158             fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1159             break;
1160
1161         case VLC_FOURCC('2','8','_','8'):
1162             fmt.i_extra = 0;
1163             fmt.audio.i_blockalign = i_coded_frame_size;
1164             break;
1165
1166         case VLC_FOURCC( 'd','n','e','t' ):
1167             fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1168             break;
1169
1170         case VLC_FOURCC( 'r','a','a','c' ):
1171         case VLC_FOURCC( 'r','a','c','p' ):
1172             if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1173             if( fmt.i_extra > 0 )
1174             {
1175                 fmt.p_extra = malloc( fmt.i_extra );
1176                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1177             }
1178
1179             fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1180             break;
1181
1182         case VLC_FOURCC('s','i','p','r'):
1183             fmt.audio.i_flavor = i_flavor;
1184         case VLC_FOURCC('c','o','o','k'):
1185         case VLC_FOURCC('a','t','r','c'):
1186             if( !memcmp( p_genr, "genr", 4 ) )
1187                 fmt.audio.i_blockalign = i_subpacket_size;
1188             else
1189                 fmt.audio.i_blockalign = i_coded_frame_size;
1190             if( !fmt.i_extra ) break;
1191             fmt.p_extra = malloc( fmt.i_extra );
1192             memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1193             break;
1194
1195         case VLC_FOURCC('r','a','l','f'):
1196             msg_Dbg( p_demux, "    - audio codec not supported=%4.4s",
1197                      (char*)&fmt.i_codec );
1198             break;
1199
1200         default:
1201             msg_Dbg( p_demux, "    - unknown audio codec=%4.4s",
1202                      (char*)&fmt.i_codec );
1203             break;
1204         }
1205
1206         if( fmt.i_codec != 0 )
1207         {
1208             int i;
1209
1210             msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );
1211
1212             tk = malloc( sizeof( real_track_t ) );
1213             tk->i_id = i_num;
1214             tk->fmt = fmt;
1215             tk->i_frame = 0;
1216             tk->p_frame = NULL;
1217
1218             tk->i_subpacket_h = i_subpacket_h;
1219             tk->i_subpacket_size = i_subpacket_size;
1220             tk->i_coded_frame_size = i_coded_frame_size;
1221             tk->i_frame_size = i_frame_size;
1222
1223             tk->i_out_subpacket = 0;
1224             tk->i_subpacket = 0;
1225             tk->i_subpackets = 0;
1226             tk->p_subpackets = NULL;
1227             if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1228              || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1229             {
1230                 tk->i_subpackets =
1231                     i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1232                 tk->p_subpackets =
1233                     malloc( tk->i_subpackets * sizeof(block_t *) );
1234             }
1235             else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1236             {
1237                 tk->i_subpackets =
1238                     i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1239                 tk->p_subpackets =
1240                     malloc( tk->i_subpackets * sizeof(block_t *) );
1241             }
1242
1243             for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1244
1245             tk->p_es = es_out_Add( p_demux->out, &fmt );
1246
1247             TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1248         }
1249     }
1250
1251     return VLC_SUCCESS;
1252 }