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