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