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