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