]> git.sesse.net Git - vlc/blob - modules/demux/real.c
real demux: Set to prio 0.
[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  * v3 and v4/5 headers are parsed.
43  * Doesn't work yet...
44  */
45
46 /*****************************************************************************
47  * Preamble
48  *****************************************************************************/
49
50 #ifdef HAVE_CONFIG_H
51 # include "config.h"
52 #endif
53
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
56
57 #include <vlc_demux.h>
58 #include <vlc_charset.h>
59 #include <vlc_meta.h>
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 static int  Open    ( vlc_object_t * );
65 static void Close  ( vlc_object_t * );
66
67 vlc_module_begin();
68     set_description( N_("Real demuxer" ) );
69     set_capability( "demux", 0 );
70     set_category( CAT_INPUT );
71     set_subcategory( SUBCAT_INPUT_DEMUX );
72     set_callbacks( Open, Close );
73     add_shortcut( "real" );
74     add_shortcut( "rm" );
75 vlc_module_end();
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80
81 typedef struct
82 {
83     int         i_id;
84     es_format_t fmt;
85
86     es_out_id_t *p_es;
87
88     int         i_frame;
89     block_t     *p_frame;
90
91     int         i_subpacket_h;
92     int         i_subpacket_size;
93     int         i_coded_frame_size;
94     int         i_frame_size;
95
96     int         i_subpacket;
97     int         i_subpackets;
98     block_t     **p_subpackets;
99     int64_t     *p_subpackets_timecode;
100     int         i_out_subpacket;
101
102 } real_track_t;
103
104 typedef struct
105 {
106     uint32_t file_offset;
107     uint32_t time_offset;
108     uint32_t frame_index;
109 } rm_index_t;
110
111 struct demux_sys_t
112 {
113     int64_t  i_data_offset;
114     int64_t  i_data_size;
115     uint32_t i_data_packets_count;
116     uint32_t i_data_packets;
117     int64_t  i_data_offset_next;
118
119     bool     b_is_real_audio;
120
121     int  i_our_duration;
122     int  i_mux_rate;
123
124     char* psz_title;
125     char* psz_artist;
126     char* psz_copyright;
127     char* psz_description;
128
129     int          i_track;
130     real_track_t **track;
131
132     uint8_t buffer[65536];
133
134     int64_t     i_pcr;
135     vlc_meta_t *p_meta;
136
137     int64_t     i_index_offset;
138     int         b_seek;
139     rm_index_t *p_index;
140 };
141
142 static int Demux( demux_t *p_demux );
143 static int Control( demux_t *p_demux, int i_query, va_list args );
144
145 static int HeaderRead( demux_t *p_demux );
146 static const uint8_t * MetaRead( demux_t *p_demux, const uint8_t *p_peek );
147 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
148
149 /*****************************************************************************
150  * Open
151  *****************************************************************************/
152 static int Open( vlc_object_t *p_this )
153 {
154     demux_t     *p_demux = (demux_t*)p_this;
155     demux_sys_t *p_sys;
156
157     const uint8_t *p_peek;
158     bool           b_is_real_audio = false;
159
160     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
161
162     /* Real Audio */
163     if( !memcmp( p_peek, ".ra", 3 ) )
164     {
165         msg_Err( p_demux, ".ra files unsuported" );
166         b_is_real_audio = true;
167     }
168     /* Real Media Format */
169     else if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
170
171     /* Fill p_demux field */
172     p_demux->pf_demux = Demux;
173     p_demux->pf_control = Control;
174
175     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
176     if( p_sys == NULL )
177         return VLC_ENOMEM;
178
179     memset( p_sys, 0, sizeof( demux_sys_t ) );
180
181     p_sys->i_data_offset = 0;
182     p_sys->i_track = 0;
183     p_sys->track   = NULL;
184     p_sys->i_pcr   = 1;
185
186     p_sys->b_seek  = 0;
187     p_sys->b_is_real_audio = b_is_real_audio;
188
189     /* Parse the headers */
190     /* Real Audio files */
191     if( b_is_real_audio )
192     {
193         ReadCodecSpecificData( p_demux, 32, 0 ); /* At least 32 */
194         return VLC_EGENERIC;                     /* We don't know how to read
195                                                     correctly the data yet */
196     }
197     /* RMF files */
198     else if( HeaderRead( p_demux ) )
199     {
200         int i;
201         msg_Err( p_demux, "invalid header" );
202         for( i = 0; i < p_sys->i_track; i++ )
203         {
204             real_track_t *tk = p_sys->track[i];
205
206             if( tk->p_es )
207             {
208                 es_out_Del( p_demux->out, tk->p_es );
209             }
210             free( tk );
211         }
212         if( p_sys->i_track > 0 )
213         {
214             free( p_sys->track );
215         }
216         free( p_sys );
217         return VLC_EGENERIC;
218     }
219
220     return VLC_SUCCESS;
221 }
222
223 /*****************************************************************************
224  * Close
225  *****************************************************************************/
226 static void Close( vlc_object_t *p_this )
227 {
228     demux_t *p_demux = (demux_t*)p_this;
229     demux_sys_t *p_sys = p_demux->p_sys;
230     int i;
231
232     for( i = 0; i < p_sys->i_track; i++ )
233     {
234         real_track_t *tk = p_sys->track[i];
235         int j = tk->i_subpackets;
236
237         if( tk->p_frame ) block_Release( tk->p_frame );
238         es_format_Clean( &tk->fmt );
239
240         while(  j-- )
241         {
242             if( tk->p_subpackets[ j ] )
243                 block_Release( tk->p_subpackets[ j ] );
244         }
245         if( tk->i_subpackets )
246         {
247             free( tk->p_subpackets );
248             free( tk->p_subpackets_timecode );
249         }
250
251         free( tk );
252     }
253
254     free( p_sys->psz_title );
255     free( p_sys->psz_artist );
256     free( p_sys->psz_copyright );
257     free( p_sys->psz_description );
258     free( p_sys->p_index );
259
260     if( p_sys->i_track > 0 ) free( p_sys->track );
261     free( p_sys );
262 }
263
264
265 /*****************************************************************************
266  * Demux:
267  *****************************************************************************/
268 static int Demux( demux_t *p_demux )
269 {
270     demux_sys_t *p_sys = p_demux->p_sys;
271     uint8_t     header[18];
272     int         i_id, i_flags, i;
273     unsigned int i_size;
274     int64_t     i_pts;
275     real_track_t *tk = NULL;
276     bool  b_selected;
277
278     if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
279         p_sys->i_data_packets_count )
280     {
281         if( stream_Read( p_demux->s, header, 18 ) < 18 )
282         {
283             return 0;
284         }
285         if( strncmp( (char *)header, "DATA", 4 ) )
286         {
287             return 0;
288         }
289         p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
290         p_sys->i_data_size   = GetDWBE( &header[4] );
291         p_sys->i_data_packets_count = GetDWBE( &header[10] );
292         p_sys->i_data_packets = 0;
293         p_sys->i_data_offset_next = GetDWBE( &header[14] );
294
295         msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
296                  p_sys->i_data_packets_count,
297                  (uint32_t)p_sys->i_data_offset_next );
298     }
299
300     if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
301
302     //    int i_version = GetWBE( &header[0] );
303     i_size = GetWBE( &header[2] ) - 12;
304     i_id   = GetWBE( &header[4] );
305     i_pts  = 1000 * GetDWBE( &header[6] );
306     i_pts += 1000; /* Avoid 0 pts */
307     i_flags= header[11]; /* flags 0x02 -> keyframe */
308
309     msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
310              p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
311
312     p_sys->i_data_packets++;
313
314     if( i_size == 0 )
315     {
316         msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
317         return 1;
318     }
319
320     if( i_size > sizeof(p_sys->buffer) )
321     {
322         msg_Err( p_demux, "Got a size to read bigger than our buffer. (Invalid format?)" );
323         return 1;
324     }
325
326     stream_Read( p_demux->s, p_sys->buffer, i_size );
327
328     for( i = 0; i < p_sys->i_track; i++ )
329     {
330         if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
331     }
332
333     if( tk == NULL )
334     {
335         msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
336         return 1;
337     }
338     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
339
340     if( tk->fmt.i_cat == VIDEO_ES && b_selected )
341     {
342         uint8_t *p = p_sys->buffer;
343
344         while( p < &p_sys->buffer[i_size - 2] )
345         {
346             uint8_t h = *p++;
347             int     i_len = 0;
348             int     i_copy;
349             int     i_subseq = 0;
350             int     i_seqnum = 0;
351             int     i_offset = 0;
352
353             if( (h&0xc0) == 0x40 )
354             {
355                 /* Short header */
356                 p++;
357                 i_len = &p_sys->buffer[i_size] - p;
358             }
359             else
360             {
361                 if( (h&0x40) == 0 )
362                 {
363                     i_subseq = (*p++)&0x7f;
364                 }
365                 i_len = (p[0] << 8)|p[1]; p += 2;
366                 if( (i_len&0xc000) == 0 )
367                 {
368                     i_len <<= 16;
369                     i_len |= (p[0] << 8)|p[1]; p += 2;
370                     i_len &= 0x3fffffff;
371                 }
372                 else
373                 {
374                     i_len &= 0x3fff;
375                 }
376
377                 i_offset = (p[0] << 8)|p[1]; p += 2;
378                 if( (i_offset&0xc000) == 0 )
379                 {
380                     i_offset <<= 16;
381                     i_offset |= (p[0] << 8)|p[1]; p += 2;
382                     i_offset &= 0x3fffffff;
383                 }
384                 else
385                 {
386                     i_offset &= 0x3fff;
387                 }
388                 i_seqnum = *p++;
389             }
390
391             i_copy = i_len - i_offset;
392             if( i_copy > &p_sys->buffer[i_size] - p )
393             {
394                 i_copy = &p_sys->buffer[i_size] - p;
395             }
396             else if( i_copy < 0 )
397             {
398                 break;
399             }
400
401             msg_Dbg( p_demux, "    - len=%d offset=%d size=%d subseq=%d seqnum=%d",
402                      i_len, i_offset, i_copy, i_subseq, i_seqnum );
403
404             if( (h&0xc0) == 0x80 )
405             {
406                 /* last fragment -> fixes */
407                 i_copy = i_offset;
408                 i_offset = i_len - i_copy;
409                 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
410                          i_copy, i_offset );
411             }
412
413             if( tk->p_frame &&
414                 ( tk->p_frame->i_dts != i_pts ||
415                   tk->i_frame != i_len ) )
416             {
417                 msg_Dbg( p_demux, "sending size=%zu", tk->p_frame->i_buffer );
418
419                 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
420
421                 tk->i_frame = 0;
422                 tk->p_frame = NULL;
423             }
424
425             if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
426             {
427                 /* no fragment */
428                 i_len = i_copy;
429                 i_offset = 0;
430             }
431
432
433             if( tk->p_frame == NULL )
434             {
435                 msg_Dbg( p_demux, "new frame size=%d", i_len );
436                 tk->i_frame = i_len;
437                 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
438                 {
439                     return -1;
440                 }
441                 memset( &tk->p_frame->p_buffer[8], 0, i_len );
442                 tk->p_frame->i_dts = i_pts;
443                 tk->p_frame->i_pts = i_pts;
444
445                 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
446                 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0;      /* chunk counts */
447             }
448
449             if( i_offset < tk->i_frame)
450             {
451                 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
452
453                 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
454                          i_ck, i_offset, i_copy );
455
456                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
457                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
458
459
460                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
461             }
462
463             p += i_copy;
464
465             if( (h&0xc0) != 0x80 )
466             {
467                 break;
468             }
469
470 #if 0
471             if( tk->p_frame )
472             {
473                 /* append data */
474                 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
475
476                 if( (h&0xc0) == 0x80 )
477                 {
478                     /* last fragment */
479                     i_copy = i_offset;
480                     i_offset = i_len - i_offset;
481
482                     ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
483                     memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
484                     p += i_copy;
485
486                     if( p_sys->i_pcr < tk->p_frame->i_dts )
487                     {
488                         p_sys->i_pcr = tk->p_frame->i_dts;
489                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
490                                         (int64_t)p_sys->i_pcr );
491                     }
492                     es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
493
494                     tk->i_frame = 0;
495                     tk->p_frame = NULL;
496
497                     continue;
498                 }
499
500                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
501                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
502                 break;
503             }
504
505             if( (h&0xc0) != 0x00 )
506             {
507                 block_t *p_frame;
508
509                 /* not fragmented */
510                 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
511                 {
512                     return -1;
513                 }
514                 p_frame->i_dts = i_pts;
515                 p_frame->i_pts = i_pts;
516
517                 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
518                 ((uint32_t*)p_frame->p_buffer)[1] = 1;
519                 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
520                 memcpy( &p_frame->p_buffer[8], p, i_copy );
521
522                 p += i_copy;
523
524                 if( p_sys->i_pcr < p_frame->i_dts )
525                 {
526                     p_sys->i_pcr = p_frame->i_dts;
527                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
528                                     (int64_t)p_sys->i_pcr );
529                 }
530                 es_out_Send( p_demux->out, tk->p_es, p_frame );
531             }
532             else
533             {
534                 /* First fragment */
535                 tk->i_frame = i_len;
536                 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
537                 {
538                     return -1;
539                 }
540                 memset( &tk->p_frame->p_buffer[8], 0, i_len );
541                 tk->p_frame->i_dts = i_pts;
542                 tk->p_frame->i_pts = i_pts;
543
544                 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
545                 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1;      /* chunk counts */
546                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
547                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
548
549                 break;
550             }
551 #endif
552         }
553     }
554     else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
555     {
556         if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
557             tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
558             tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
559             tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
560         {
561             uint8_t *p_buf = p_sys->buffer;
562             int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
563             int i_index, i;
564
565             /* Sanity check */
566             if( i_flags & 2 || ( p_sys->b_seek ) )
567             {
568                 y = tk->i_subpacket = 0;
569                 tk->i_out_subpacket = 0;
570                 p_sys->b_seek = 0;
571             }
572
573             if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
574                 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
575             {
576                 int num = tk->i_frame_size / tk->i_subpacket_size;
577                 for( i = 0; i <  num; i++ )
578                 {
579                     block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
580                     memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
581                     p_buf += tk->i_subpacket_size;
582
583                     i_index = tk->i_subpacket_h * i +
584                         ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
585
586                     if ( tk->p_subpackets[i_index]  != NULL )
587                     {
588                         msg_Dbg(p_demux, "p_subpackets[ %d ] not null!",  i_index );
589                         free( tk->p_subpackets[i_index] );
590                     }
591
592                     tk->p_subpackets[i_index] = p_block;
593                     tk->i_subpacket++;
594                     p_block->i_dts = p_block->i_pts = 0;
595                 }
596                 tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
597             }
598
599             if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
600                 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
601             for( i = 0; i < tk->i_subpacket_h / 2; i++ )
602             {
603                 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
604                 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
605                 p_buf += tk->i_coded_frame_size;
606
607                 i_index = (i * 2 * tk->i_frame_size) /
608                     tk->i_coded_frame_size + y;
609
610                 p_block->i_dts = p_block->i_pts = i_pts;
611                 tk->p_subpackets[i_index] = p_block;
612                 tk->i_subpacket++;
613             }
614
615             while( tk->i_out_subpacket != tk->i_subpackets &&
616                    tk->p_subpackets[tk->i_out_subpacket] )
617             {
618                 /* Set the PCR */
619 #if 0
620                 if (tk->i_out_subpacket == 0)
621                 {
622                     p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
623                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
624                             (int64_t)p_sys->i_pcr );
625                 }
626
627                 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
628                 tk->p_subpackets[tk->i_out_subpacket] = 0;
629
630                 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
631 #endif
632
633                 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
634                 tk->p_subpackets[tk->i_out_subpacket] = 0;
635                 if ( tk->p_subpackets_timecode[tk->i_out_subpacket]  )
636                 {
637                     p_block->i_dts = p_block->i_pts =
638                         tk->p_subpackets_timecode[tk->i_out_subpacket];
639                     tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
640
641                     p_sys->i_pcr = p_block->i_dts;
642                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
643                             (int64_t)p_sys->i_pcr );
644                 }
645                 es_out_Send( p_demux->out, tk->p_es, p_block );
646
647                 tk->i_out_subpacket++;
648             }
649
650             if( tk->i_subpacket == tk->i_subpackets &&
651                 tk->i_out_subpacket != tk->i_subpackets )
652             {
653                 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
654                           "this shouldn't happen" );
655             }
656
657             if( tk->i_subpacket == tk->i_subpackets )
658             {
659                 tk->i_subpacket = 0;
660                 tk->i_out_subpacket = 0;
661             }
662         }
663         else
664         {
665             /* Set PCR */
666             if( p_sys->i_pcr < i_pts )
667             {
668                 p_sys->i_pcr = i_pts;
669                 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
670                         (int64_t)p_sys->i_pcr );
671             }
672
673             if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
674             {
675                 int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
676                 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
677
678                 int i;
679                 for( i = 0; i < i_sub; i++ )
680                 {
681                     int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
682                     block_t *p_block = block_New( p_demux, i_sub_size );
683                     if( p_block )
684                     {
685                         memcpy( p_block->p_buffer, p_sub, i_sub_size );
686                         p_sub += i_sub_size;
687
688                         p_block->i_dts =
689                             p_block->i_pts = ( i == 0 ? i_pts : 0 );
690
691                         es_out_Send( p_demux->out, tk->p_es, p_block );
692                     }
693                 }
694             }
695             else
696             {
697                 block_t *p_block = block_New( p_demux, i_size );
698
699                 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
700                 {
701                     uint8_t *src = p_sys->buffer;
702                     uint8_t *dst = p_block->p_buffer;
703
704                     /* byte swap data */
705                     while( dst < &p_block->p_buffer[i_size- 1])
706                     {
707                         *dst++ = src[1];
708                         *dst++ = src[0];
709
710                         src += 2;
711                     }
712                 }
713                 else
714                 {
715                     memcpy( p_block->p_buffer, p_sys->buffer, i_size );
716                 }
717                 p_block->i_dts = p_block->i_pts = i_pts;
718                 es_out_Send( p_demux->out, tk->p_es, p_block );
719             }
720         }
721     }
722
723
724     return 1;
725 }
726
727 /*****************************************************************************
728  * Control:
729  *****************************************************************************/
730 static int Control( demux_t *p_demux, int i_query, va_list args )
731 {
732     demux_sys_t *p_sys = p_demux->p_sys;
733     double f, *pf;
734     int64_t i64;
735     rm_index_t * p_index;
736     int64_t *pi64;
737
738     switch( i_query )
739     {
740         case DEMUX_GET_POSITION:
741             pf = (double*) va_arg( args, double* );
742
743             if( p_sys->i_our_duration > 0 )
744             {
745                 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
746                 return VLC_SUCCESS;
747             }
748
749             /* read stream size maybe failed in rtsp streaming, 
750                so use duration to determin the position at first  */
751             i64 = stream_Size( p_demux->s );
752             if( i64 > 0 )
753             {
754                 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
755             }
756             else
757             {
758                 *pf = 0.0;
759             }
760             return VLC_SUCCESS;
761
762         case DEMUX_GET_TIME:
763             pi64 = (int64_t*)va_arg( args, int64_t * );
764
765             if( p_sys->i_our_duration > 0 )
766             {
767                 *pi64 = p_sys->i_pcr;
768                 return VLC_SUCCESS;
769             }
770
771             /* same as GET_POSTION */
772             i64 = stream_Size( p_demux->s );
773             if( p_sys->i_our_duration > 0 && i64 > 0 )
774             {
775                 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
776                 return VLC_SUCCESS;
777             }
778
779             *pi64 = 0;
780             return VLC_EGENERIC;
781
782         case DEMUX_SET_POSITION:
783             f = (double) va_arg( args, double );
784             i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
785
786             if ( p_sys->i_index_offset == 0 && i64 != 0 )
787             {
788                 msg_Err(p_demux,"Seek No Index Real File failed!" );
789                 return VLC_EGENERIC; // no index!
790             }
791             if ( i64 == 0 )
792             {
793                 /* it is a rtsp stream , it is specials in access/rtsp/... */
794                 msg_Dbg(p_demux, "Seek in real rtsp stream!");
795                 p_sys->i_pcr = (int64_t)1000 * ( p_sys->i_our_duration * f  );
796
797                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
798                 p_sys->b_seek = 1;
799
800                 return stream_Seek( p_demux->s, p_sys->i_pcr );
801             }
802
803             if ( p_sys->i_index_offset > 0 )
804             {
805                 p_index = p_sys->p_index;
806                 while( p_index->file_offset !=0 )
807                 {
808                     if ( p_index->file_offset > i64 )
809                     {
810                         msg_Dbg( p_demux, "Seek Real find! %d %d %d",
811                                  p_index->time_offset, p_index->file_offset,
812                                  (uint32_t) i64 );
813                         if ( p_index != p_sys->p_index ) p_index --;
814                         i64 = p_index->file_offset;
815                         break;
816                     }
817                     p_index++;
818                 }
819
820                 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
821
822                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
823
824                 return stream_Seek( p_demux->s, i64 );
825             }
826         case DEMUX_SET_TIME:
827             i64 = (int64_t) va_arg( args, int64_t ) / 1000;
828
829             p_index = p_sys->p_index;
830             while( p_index->file_offset !=0 )
831             {
832                 if ( p_index->time_offset > i64 )
833                 {
834                     if ( p_index != p_sys->p_index )
835                         p_index --;
836                     i64 = p_index->file_offset;
837                     break;
838                 }
839                 p_index++;
840             }
841
842             p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
843             es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
844
845             return stream_Seek( p_demux->s, i64 );
846
847         case DEMUX_GET_LENGTH:
848             pi64 = (int64_t*)va_arg( args, int64_t * );
849  
850             /* the commented following lines are fen's implementation, which doesn't seem to
851              * work for one reason or another -- FK */
852             /*if( p_sys->i_mux_rate > 0 )
853             {
854                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
855                 return VLC_SUCCESS;
856             }*/
857             if( p_sys->i_our_duration > 0 )
858             {
859                 /* our stored duration is in ms, so... */
860                 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
861  
862                 return VLC_SUCCESS;
863             }
864             *pi64 = 0;
865             return VLC_EGENERIC;
866
867         case DEMUX_GET_META:
868         {
869             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
870
871             /* the core will crash if we provide NULL strings, so check
872              * every string first */
873             if( p_sys->psz_title )
874                 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
875             if( p_sys->psz_artist )
876                 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
877             if( p_sys->psz_copyright )
878                 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
879             if( p_sys->psz_description )
880                 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
881             return VLC_SUCCESS;
882         }
883
884         case DEMUX_GET_FPS:
885         default:
886             return VLC_EGENERIC;
887     }
888     return VLC_EGENERIC;
889 }
890
891 /*****************************************************************************
892  * ReadRealIndex:
893  *****************************************************************************/
894
895 static void ReadRealIndex( demux_t *p_demux )
896 {
897     demux_sys_t *p_sys = p_demux->p_sys;
898     uint8_t       buffer[100];
899     uint32_t      i_id;
900     uint32_t      i_size;
901     int           i_version;
902     unsigned int  i;
903
904     uint32_t      i_index_count;
905
906     if ( p_sys->i_index_offset == 0 )
907         return;
908
909     stream_Seek( p_demux->s, p_sys->i_index_offset );
910
911     if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
912         return ;
913
914     i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
915     i_size      = GetDWBE( &buffer[4] );
916     i_version   = GetWBE( &buffer[8] );
917
918     msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
919                  (char*)&i_id, i_size, i_version );
920
921     if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
922         return;
923
924     i_index_count = GetDWBE( &buffer[10] );
925
926     msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
927
928     if( i_index_count == 0 )
929         return;
930
931     if( GetDWBE( &buffer[16] ) > 0 )
932         msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
933                         GetDWBE( &buffer[16] )  );
934
935     p_sys->p_index = 
936             (rm_index_t *)malloc( sizeof( rm_index_t ) * (i_index_count+1) );
937     if( p_sys->p_index == NULL )
938     {
939         msg_Err( p_demux, "Memory allocation error" ); 
940         return;
941     }
942
943     memset( p_sys->p_index, 0, sizeof(rm_index_t) * (i_index_count+1) );
944
945     for( i=0; i<i_index_count; i++ )
946     {
947         if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
948             return ;
949
950         if( GetWBE( &buffer[0] ) != 0 )
951         {
952             msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
953                               GetWBE( &buffer[0] ) );
954             return;
955         }
956
957         p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] );
958         p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] );
959         p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] );
960         msg_Dbg( p_demux, "Real Index: time %d file %d frame %d ",
961                         p_sys->p_index[i].time_offset,
962                         p_sys->p_index[i].file_offset,
963                         p_sys->p_index[i].frame_index );
964
965     }
966 }
967
968 /*****************************************************************************
969  * HeaderRead:
970  *****************************************************************************/
971 static int HeaderRead( demux_t *p_demux )
972 {
973     demux_sys_t *p_sys = p_demux->p_sys;
974     uint8_t header[100];    /* FIXME */
975
976     uint32_t    i_id;
977     uint32_t    i_size;
978     int64_t     i_skip;
979     int         i_version;
980     p_sys->p_meta = vlc_meta_New();
981
982     for( ;; )
983     {
984         /* Read the header */
985         if( stream_Read( p_demux->s, header, 10 ) < 10 )
986         {
987             return VLC_EGENERIC;
988         }
989         i_id        = VLC_FOURCC( header[0], header[1], header[2], header[3] );
990         i_size      = GetDWBE( &header[4] );
991         i_version   = GetWBE( &header[8] );
992
993         msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
994                  (char*)&i_id, i_size, i_version );
995
996         if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
997         {
998             msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
999             return VLC_EGENERIC;
1000         }
1001         i_skip = i_size - 10;
1002
1003         if( i_id == VLC_FOURCC('.','R','M','F') )
1004         {
1005             if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
1006             msg_Dbg( p_demux, "    - file version=0x%x num headers=%d",
1007                      GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
1008
1009             i_skip -= 8;
1010         }
1011         else if( i_id == VLC_FOURCC('P','R','O','P') )
1012         {
1013             int i_flags;
1014
1015             if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
1016
1017             msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
1018                      GetDWBE(&header[0]), GetDWBE(&header[4]) );
1019             msg_Dbg( p_demux, "    - max packet size=%d avg bitrate=%d",
1020                      GetDWBE(&header[8]), GetDWBE(&header[12]) );
1021             msg_Dbg( p_demux, "    - packets count=%d", GetDWBE(&header[16]) );
1022             msg_Dbg( p_demux, "    - duration=%d ms", GetDWBE(&header[20]) );
1023             msg_Dbg( p_demux, "    - preroll=%d ms", GetDWBE(&header[24]) );
1024             msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&header[28]) );
1025             msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&header[32]) );
1026             msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&header[36]) );
1027  
1028             /* set the duration for export in control */
1029             p_sys->i_our_duration = (int)GetDWBE(&header[20]);
1030  
1031             p_sys->i_index_offset = GetDWBE(&header[28]);
1032
1033             i_flags = GetWBE(&header[38]);
1034             msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
1035                      i_flags,
1036                      i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
1037                      i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
1038                      i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
1039             i_skip -= 40;
1040         }
1041         else if( i_id == VLC_FOURCC('C','O','N','T') )
1042         {
1043             int i_len;
1044             char *psz;
1045  
1046             /* FIXME FIXME: should convert from whatever the character
1047              * encoding of the input meta data is to UTF-8. */
1048
1049             stream_Read( p_demux->s, header, 2 );
1050             if( ( i_len = GetWBE( header ) ) > 0 )
1051             {
1052                 psz = malloc( i_len + 1 );
1053                 stream_Read( p_demux->s, psz, i_len );
1054                 psz[i_len] = '\0';
1055
1056                 EnsureUTF8( psz );
1057                 msg_Dbg( p_demux, "    - title=`%s'", psz );
1058                 p_sys->psz_title = psz;
1059                 i_skip -= i_len;
1060             }
1061             i_skip -= 2;
1062
1063             stream_Read( p_demux->s, header, 2 );
1064             if( ( i_len = GetWBE( header ) ) > 0 )
1065             {
1066                 psz = malloc( i_len + 1 );
1067                 stream_Read( p_demux->s, psz, i_len );
1068                 psz[i_len] = '\0';
1069
1070                 EnsureUTF8( psz );
1071                 msg_Dbg( p_demux, "    - author=`%s'", psz );
1072                 p_sys->psz_artist = psz;
1073                 i_skip -= i_len;
1074             }
1075             i_skip -= 2;
1076
1077             stream_Read( p_demux->s, header, 2 );
1078             if( ( i_len = GetWBE( header ) ) > 0 )
1079             {
1080                 psz = malloc( i_len + 1 );
1081                 stream_Read( p_demux->s, psz, i_len );
1082                 psz[i_len] = '\0';
1083
1084                 EnsureUTF8( psz );
1085                 msg_Dbg( p_demux, "    - copyright=`%s'", psz );
1086                 p_sys->psz_copyright = psz;
1087                 i_skip -= i_len;
1088             }
1089             i_skip -= 2;
1090
1091             stream_Read( p_demux->s, header, 2 );
1092             if( ( i_len = GetWBE( header ) ) > 0 )
1093             {
1094                 psz = malloc( i_len + 1 );
1095                 stream_Read( p_demux->s, psz, i_len );
1096                 psz[i_len] = '\0';
1097
1098                 EnsureUTF8( psz );
1099                 msg_Dbg( p_demux, "    - comment=`%s'", psz );
1100                 p_sys->psz_description = psz;
1101                 i_skip -= i_len;
1102             }
1103             i_skip -= 2;
1104         }
1105         else if( i_id == VLC_FOURCC('M','D','P','R') )
1106         {
1107             /* Media properties header */
1108             int  i_num;
1109             int  i_len;
1110             char *psz;
1111
1112             if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
1113             i_num = GetWBE( header );
1114             msg_Dbg( p_demux, "    - id=0x%x", i_num );
1115             msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
1116                      GetDWBE(&header[2]), GetDWBE(&header[6]) );
1117             msg_Dbg( p_demux, "    - max packet size=%d avg packet size=%d",
1118                      GetDWBE(&header[10]), GetDWBE(&header[14]) );
1119             msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&header[18]) );
1120             msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&header[22]) );
1121             msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&header[26]) );
1122  
1123             i_skip -= 30;
1124
1125             stream_Read( p_demux->s, header, 1 );
1126             if( ( i_len = header[0] ) > 0 )
1127             {
1128                 psz = malloc( i_len + 1 );
1129                 stream_Read( p_demux->s, psz, i_len );
1130                 psz[i_len] = '\0';
1131
1132                 msg_Dbg( p_demux, "    - name=`%s'", psz );
1133                 free( psz );
1134                 i_skip -= i_len;
1135             }
1136             i_skip--;
1137
1138             stream_Read( p_demux->s, header, 1 );
1139             if( ( i_len = header[0] ) > 0 )
1140             {
1141                 psz = malloc( i_len + 1 );
1142                 stream_Read( p_demux->s, psz, i_len );
1143                 psz[i_len] = '\0';
1144
1145                 msg_Dbg( p_demux, "    - mime=`%s'", psz );
1146                 free( psz );
1147                 i_skip -= i_len;
1148             }
1149             i_skip--;
1150
1151             stream_Read( p_demux->s, header, 4 );
1152             if( ( i_len = GetDWBE( header ) ) > 0 )
1153             {
1154                 ReadCodecSpecificData( p_demux, i_len, i_num );
1155                 stream_Read( p_demux->s, NULL, i_len );
1156
1157                 i_skip -= i_len;
1158             }
1159             i_skip -= 4;
1160         }
1161         else if( i_id == VLC_FOURCC('D','A','T','A') )
1162         {
1163             stream_Read( p_demux->s, header, 8 );
1164
1165             p_sys->i_data_offset    = stream_Tell( p_demux->s ) - 10;
1166             p_sys->i_data_size      = i_size;
1167             p_sys->i_data_packets_count = GetDWBE( header );
1168             p_sys->i_data_packets   = 0;
1169             p_sys->i_data_offset_next = GetDWBE( &header[4] );
1170
1171             msg_Dbg( p_demux, "    - packets count=%d next=%u",
1172                      p_sys->i_data_packets_count,
1173                      (uint32_t)p_sys->i_data_offset_next );
1174
1175             /* we have finished the header */
1176             break;
1177         }
1178         else
1179         {
1180             /* unknow header */
1181             msg_Dbg( p_demux, "unknown chunk" );
1182         }
1183
1184         if( i_skip < 0 ) return VLC_EGENERIC;
1185         stream_Read( p_demux->s, NULL, i_skip );
1186     }
1187
1188     /* TODO read index if possible */
1189
1190     if ( p_sys->i_index_offset > 0 )
1191     {
1192         int64_t pos;
1193         pos = stream_Tell( p_demux->s );
1194         ReadRealIndex( p_demux );
1195         stream_Seek( p_demux->s, pos );
1196     }
1197
1198     return VLC_SUCCESS;
1199 }
1200
1201 static const uint8_t * MetaRead( demux_t *p_demux, const uint8_t *p_peek )
1202 {
1203     demux_sys_t *p_sys = p_demux->p_sys;
1204
1205     int i_len;
1206     char *psz;
1207
1208     /* Title */
1209     i_len = *p_peek ; p_peek++;
1210     if( i_len > 0 )
1211     {
1212         psz = malloc( i_len + 1 );
1213         memcpy( psz, p_peek, i_len );
1214         psz[i_len] = '\0';
1215
1216         EnsureUTF8( psz );
1217         msg_Dbg( p_demux, "    - title=`%s'", psz );
1218         p_sys->psz_title = psz;
1219     }
1220     p_peek += i_len;
1221
1222     /* Authors */
1223     i_len = *p_peek ; p_peek++;
1224     if( i_len > 0 )
1225     {
1226         psz = malloc( i_len + 1 );
1227         memcpy( psz, p_peek, i_len );
1228         psz[i_len] = '\0';
1229
1230         EnsureUTF8( psz );
1231         msg_Dbg( p_demux, "    - artist=`%s'", psz );
1232         p_sys->psz_artist = psz;
1233     }
1234     p_peek += i_len;
1235
1236     /* Copyright */
1237     i_len = *p_peek ; p_peek++;
1238     if( i_len > 0 )
1239     {
1240         psz = malloc( i_len + 1 );
1241         memcpy( psz, p_peek, i_len );
1242         psz[i_len] = '\0';
1243
1244         EnsureUTF8( psz );
1245         msg_Dbg( p_demux, "    - Copyright=`%s'", psz );
1246         p_sys->psz_copyright = psz;
1247     }
1248     p_peek += i_len;
1249
1250     /* Comment */
1251     i_len = *p_peek ; p_peek++;
1252     if( i_len > 0 )
1253     {
1254         psz = malloc( i_len + 1 );
1255         memcpy( psz, p_peek, i_len );
1256         psz[i_len] = '\0';
1257
1258         EnsureUTF8( psz );
1259         msg_Dbg( p_demux, "    - Comment=`%s'", psz );
1260         p_sys->psz_description = psz;
1261     }
1262     p_peek += i_len;
1263
1264     return p_peek;
1265 }
1266
1267
1268 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
1269 {
1270     demux_sys_t *p_sys = p_demux->p_sys;
1271     es_format_t fmt;
1272     real_track_t *tk;
1273     const uint8_t *p_peek;
1274
1275     msg_Dbg( p_demux, "    - specific data len=%d", i_len );
1276     if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
1277
1278     if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
1279     {
1280         es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
1281                         p_peek[10], p_peek[11] ) );
1282         fmt.video.i_width = GetWBE( &p_peek[12] );
1283         fmt.video.i_height= GetWBE( &p_peek[14] );
1284
1285         fmt.i_extra = 8;
1286         fmt.p_extra = malloc( 8 );
1287         ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
1288         ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
1289
1290         msg_Dbg( p_demux, "    - video 0x%08x 0x%08x",
1291                  ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
1292
1293         if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
1294             GetDWBE( &p_peek[30] ) == 0x10003001 )
1295         {
1296             fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1297         }
1298         else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
1299                  GetDWBE( &p_peek[30] ) == 0x20100001 ||
1300                  GetDWBE( &p_peek[30] ) == 0x20200002 )
1301         {
1302             fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1303         }
1304         else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
1305         {
1306             fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1307         }
1308         else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
1309         {
1310             fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1311         }
1312
1313         msg_Dbg( p_demux, "    - video %4.4s %dx%d",
1314                  (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1315
1316         tk = malloc( sizeof( real_track_t ) );
1317         tk->i_out_subpacket = 0;
1318         tk->i_subpacket = 0;
1319         tk->i_subpackets = 0;
1320         tk->p_subpackets = NULL;
1321         tk->p_subpackets_timecode = NULL;
1322         tk->i_id = i_num;
1323         tk->fmt = fmt;
1324         tk->i_frame = 0;
1325         tk->p_frame = NULL;
1326         tk->p_es = es_out_Add( p_demux->out, &fmt );
1327
1328         TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1329     }
1330     else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1331     {
1332         int i_header_size = 0;
1333         int i_flavor = 0;
1334         int i_coded_frame_size = 0;
1335         int i_subpacket_h = 0;
1336         int i_frame_size = 0;
1337         int i_subpacket_size = 0;
1338         char p_genr[4];
1339         int i_version = GetWBE( &p_peek[4] );   /* [0..3] = '.','r','a',0xfd */
1340         msg_Dbg( p_demux, "    - audio version=%d", i_version );
1341
1342         p_peek += 6;                                          /* 4 + version */
1343         es_format_Init( &fmt, AUDIO_ES, 0 );
1344
1345         if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1346         {
1347             i_header_size = GetWBE( p_peek ); p_peek += 2;  /* Size from now */
1348             p_peek += 10;                                         /* Unknown */
1349
1350             msg_Dbg( p_demux, "Data Size: %i", GetDWBE( p_peek ) );
1351             p_peek += 4;                                        /* Data Size */
1352
1353             /* Meta Datas */
1354             p_peek = MetaRead( p_demux, p_peek );
1355
1356             p_peek ++;                                            /* Unknown */
1357             p_peek ++;                                  /* FourCC length = 4 */
1358             memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4; /* FourCC*/
1359
1360             fmt.audio.i_channels = 1;      /* This is always the case in rm3 */
1361             fmt.audio.i_rate = 8000;
1362
1363             msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1364                  (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1365         }
1366         else  /* RMF version 4/5 or .ra version 4 */
1367         {
1368             p_peek += 2;                                            /* 00 00 */
1369             p_peek += 4;                                     /* .ra4 or .ra5 */
1370             p_peek += 4;                                        /* data size */
1371             p_peek += 2;                                 /* version (4 or 5) */
1372             i_header_size = GetDWBE( p_peek ); p_peek += 4;   /* header size */
1373             i_flavor = GetWBE( p_peek ); p_peek += 2;        /* codec flavor */
1374             i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1375             p_peek += 4;                                               /* ?? */
1376             p_peek += 4;                                               /* ?? */
1377             p_peek += 4;                                               /* ?? */
1378             i_subpacket_h = GetWBE( p_peek ); p_peek += 2;              /* 1 */
1379             i_frame_size = GetWBE( p_peek ); p_peek += 2;      /* frame size */
1380             i_subpacket_size = GetWBE( p_peek ); p_peek += 2;  /* subpacket_size */
1381             if( (!i_subpacket_size && !p_sys->b_is_real_audio )
1382                 || !i_frame_size || !i_coded_frame_size )
1383                  return VLC_EGENERIC;
1384             p_peek += 2;                                               /* ?? */
1385
1386             if( i_version == 5 ) p_peek += 6;                 /* 0, srate, 0 */
1387
1388             fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2;  /* Sample Rate */
1389             p_peek += 2;                                                /* ?? */
1390             fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1391             fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1392             fmt.audio.i_blockalign = i_frame_size;
1393
1394             if( i_version == 5 )
1395             {
1396                 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4;    /* genr */
1397                 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1398             }
1399             else /* version 4 */
1400             {
1401                 p_peek += 1 + p_peek[0];   /* Inteleaver ID string and lenth */
1402                 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 );     /* FourCC */
1403                 p_peek += 1 + p_peek[0];
1404             }
1405
1406             msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1407                  (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1408
1409             p_peek += 3;                                               /* ?? */
1410
1411             if( p_sys->b_is_real_audio )
1412             {
1413                 p_peek = MetaRead( p_demux, p_peek );
1414             }
1415             else
1416             {
1417                 if( i_version == 5 ) p_peek++;
1418                 /* Extra Data then: DWord + byte[] */
1419                 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1420             }
1421         }
1422
1423         switch( fmt.i_codec )
1424         {
1425         case VLC_FOURCC('l','p','c','J'):
1426             fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1427         case VLC_FOURCC('1','4','_','4'):
1428             fmt.audio.i_blockalign = 0x14 ;
1429             break;
1430
1431         case VLC_FOURCC('2','8','_','8'):
1432             fmt.i_extra = 0;
1433             fmt.audio.i_blockalign = i_coded_frame_size;
1434             break;
1435
1436         case VLC_FOURCC( 'd','n','e','t' ):
1437             fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1438             break;
1439
1440         case VLC_FOURCC( 'r','a','a','c' ):
1441         case VLC_FOURCC( 'r','a','c','p' ):
1442             fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1443
1444             if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1445             if( fmt.i_extra > 0 )
1446             {
1447                 fmt.p_extra = malloc( fmt.i_extra );
1448                 if( fmt.p_extra == NULL )
1449                 {
1450                     msg_Err( p_demux, "Error in the extra data" );
1451                     return VLC_EGENERIC;
1452                 }
1453                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1454             }
1455             break;
1456
1457         case VLC_FOURCC('s','i','p','r'):
1458             fmt.audio.i_flavor = i_flavor;
1459         case VLC_FOURCC('c','o','o','k'):
1460         case VLC_FOURCC('a','t','r','c'):
1461             if( !memcmp( p_genr, "genr", 4 ) )
1462                 fmt.audio.i_blockalign = i_subpacket_size;
1463             else
1464                 fmt.audio.i_blockalign = i_coded_frame_size;
1465
1466             if( fmt.i_extra > 0 )
1467             {
1468                 fmt.p_extra = malloc( fmt.i_extra );
1469                 if( fmt.p_extra == NULL )
1470                 {
1471                     msg_Err( p_demux, "Error in the extra data" );
1472                     return VLC_EGENERIC;
1473                 }
1474                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1475             }
1476             break;
1477
1478         case VLC_FOURCC('r','a','l','f'):
1479             msg_Dbg( p_demux, "    - audio codec not supported=%4.4s",
1480                      (char*)&fmt.i_codec );
1481             break;
1482
1483         default:
1484             msg_Dbg( p_demux, "    - unknown audio codec=%4.4s",
1485                      (char*)&fmt.i_codec );
1486             break;
1487         }
1488
1489         if( fmt.i_codec != 0 )
1490         {
1491             msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );
1492
1493             tk = malloc( sizeof( real_track_t ) );
1494             tk->i_id = i_num;
1495             tk->fmt = fmt;
1496             tk->i_frame = 0;
1497             tk->p_frame = NULL;
1498
1499             tk->i_subpacket_h = i_subpacket_h;
1500             tk->i_subpacket_size = i_subpacket_size;
1501             tk->i_coded_frame_size = i_coded_frame_size;
1502             tk->i_frame_size = i_frame_size;
1503
1504             tk->i_out_subpacket = 0;
1505             tk->i_subpacket = 0;
1506             tk->i_subpackets = 0;
1507             tk->p_subpackets = NULL;
1508             tk->p_subpackets_timecode = NULL;
1509             if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1510              || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1511             {
1512                 tk->i_subpackets =
1513                     i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1514                 tk->p_subpackets =
1515                     calloc( tk->i_subpackets, sizeof(block_t *) );
1516                 tk->p_subpackets_timecode =
1517                     calloc( tk->i_subpackets , sizeof( int64_t ) );
1518             }
1519             else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1520             {
1521                 tk->i_subpackets =
1522                     i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1523                 tk->p_subpackets =
1524                     calloc( tk->i_subpackets, sizeof(block_t *) );
1525                 tk->p_subpackets_timecode =
1526                     calloc( tk->i_subpackets , sizeof( int64_t ) );
1527             }
1528
1529             /* Check if the calloc went correctly */
1530             if( tk->p_subpackets == NULL )
1531             {
1532                 tk->i_subpackets = 0;
1533                 msg_Err( p_demux, "Can't alloc subpacket" );
1534                 return VLC_EGENERIC;
1535             }
1536
1537             tk->p_es = es_out_Add( p_demux->out, &fmt );
1538
1539             TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1540         }
1541     }
1542
1543     return VLC_SUCCESS;
1544 }
1545