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