]> git.sesse.net Git - vlc/blob - modules/demux/real.c
Cosmetics real demux reorganization.
[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] = 0;
705         if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
706         {
707             p_block->i_dts =
708             p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
709
710             tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
711         }
712         tk->i_out_subpacket++;
713
714         CheckPcr( p_demux, tk, p_block->i_pts );
715         es_out_Send( p_demux->out, tk->p_es, p_block );
716     }
717
718     if( tk->i_subpacket == tk->i_subpackets &&
719         tk->i_out_subpacket != tk->i_subpackets )
720     {
721         msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
722                   "this shouldn't happen" );
723     }
724
725     if( tk->i_subpacket == tk->i_subpackets )
726     {
727         tk->i_subpacket = 0;
728         tk->i_out_subpacket = 0;
729     }
730 }
731 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
732 {
733     demux_sys_t *p_sys = p_demux->p_sys;
734
735     if( p_sys->i_buffer < 2 )
736         return;
737
738     int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
739     if( p_sys->i_buffer < 2+2*i_sub )
740         return;
741
742     uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
743
744     for( int i = 0; i < i_sub; i++ )
745     {
746         const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
747         block_t *p_block = block_New( p_demux, i_sub_size );
748         if( !p_block )
749             break;
750
751         if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
752             break;
753
754         memcpy( p_block->p_buffer, p_sub, i_sub_size );
755         p_sub += i_sub_size;
756
757         p_block->i_dts =
758         p_block->i_pts = ( i == 0 ? i_pts : 0 );
759
760         CheckPcr( p_demux, tk, p_block->i_pts );
761         es_out_Send( p_demux->out, tk->p_es, p_block );
762     }
763 }
764 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
765 {
766     demux_sys_t *p_sys = p_demux->p_sys;
767
768     if( p_sys->i_buffer <= 0 )
769         return;
770
771     block_t *p_block = block_New( p_demux, p_sys->i_buffer );
772     if( !p_block )
773         return;
774
775     if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
776     {
777         uint8_t *p_src = p_sys->buffer;
778         uint8_t *p_dst = p_block->p_buffer;
779
780         /* byte swap data */
781         while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
782         {
783             *p_dst++ = p_src[1];
784             *p_dst++ = p_src[0];
785
786             p_src += 2;
787         }
788     }
789     else
790     {
791         memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
792     }
793     p_block->i_dts =
794     p_block->i_pts = i_pts;
795
796     CheckPcr( p_demux, tk, p_block->i_pts );
797     es_out_Send( p_demux->out, tk->p_es, p_block );
798 }
799
800 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
801 {
802     switch( tk->fmt.i_codec )
803     {
804     case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
805     case VLC_FOURCC( 'a', 't', 'r', 'c' ):
806     case VLC_FOURCC( 's', 'i', 'p', 'r' ):
807     case VLC_FOURCC( '2', '8', '_', '8' ):
808         DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
809         break;
810     case VLC_FOURCC( 'm','p','4','a' ):
811         DemuxAudioMethod2( p_demux, tk, i_pts );
812         break;
813     default:
814         DemuxAudioMethod3( p_demux, tk, i_pts );
815         break;
816     }
817 }
818
819 /*****************************************************************************
820  * Helpers: seek/control
821  *****************************************************************************/
822 static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
823 {
824     demux_sys_t *p_sys = p_demux->p_sys;
825
826     p_sys->b_seek = true;
827     p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
828     for( int i = 0; i < p_sys->i_track; i++ )
829         p_sys->track[i]->i_last_dts = 0;
830     return stream_Seek( p_demux->s, p_index->i_file_offset );
831 }
832 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
833 {
834     demux_sys_t *p_sys = p_demux->p_sys;
835     real_index_t *p_index = p_sys->p_index;
836
837     while( p_index->i_file_offset != 0 )
838     {
839         if( p_index->i_time_offset * INT64_C(1000) > i_time )
840         {
841             if( p_index != p_sys->p_index )
842                 p_index--;
843             break;
844         }
845         p_index++;
846     }
847     if( p_index->i_file_offset == 0 )
848         return VLC_EGENERIC;
849     return ControlGoToIndex( p_demux, p_index );
850 }
851 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
852 {
853     demux_sys_t *p_sys = p_demux->p_sys;
854     real_index_t *p_index = p_sys->p_index;
855
856     while( p_index->i_file_offset != 0 )
857     {
858         if( p_index->i_file_offset > i_bytes )
859         {
860             if( p_index != p_sys->p_index )
861                 p_index--;
862             break;
863         }
864         p_index++;
865     }
866     if( p_index->i_file_offset == 0 )
867         return VLC_EGENERIC;
868     return ControlGoToIndex( p_demux, p_index );
869 }
870
871 /*****************************************************************************
872  * Helpers: header reading
873  *****************************************************************************/
874
875 /**
876  * This function will read a pascal string with size stored in 2 bytes from
877  * a stream_t.
878  *
879  * FIXME what is the right charset ?
880  */
881 static char *StreamReadString2( stream_t *s )
882 {
883     uint8_t p_tmp[2];
884
885     if( stream_Read( s, p_tmp, 2 ) < 2 )
886         return NULL;
887
888     const int i_length = GetWBE( p_tmp );
889     if( i_length <= 0 )
890         return NULL;
891
892     char *psz_string = calloc( 1, i_length + 1 );
893
894     stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
895
896     if( psz_string )
897         EnsureUTF8( psz_string );
898     return psz_string;
899 }
900
901 /**
902  * This function will read a pascal string with size stored in 1 byte from a
903  * memory buffer.
904  *
905  * FIXME what is the right charset ?
906  */
907 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
908 {
909     const uint8_t *p_data = *pp_data;
910     int           i_data = *pi_data;
911
912     char *psz_string = NULL;
913
914     if( i_data < 1 )
915         goto exit;
916
917     int i_length = *p_data++; i_data--;
918     if( i_length > i_data )
919         i_length = i_data;
920
921     if( i_length > 0 )
922     {
923         psz_string = strndup( (const char*)p_data, i_length );
924         if( psz_string )
925             EnsureUTF8( psz_string );
926
927         p_data += i_length;
928         i_data -= i_length;
929     }
930
931 exit:
932     *pp_data = p_data;
933     *pi_data = i_data;
934     return psz_string;
935 }
936
937 /**
938  * This function parses(skip) the .RMF identification chunk.
939  */
940 static int HeaderRMF( demux_t *p_demux )
941 {
942     uint8_t p_buffer[8];
943
944     if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
945         return VLC_EGENERIC;
946
947     msg_Dbg( p_demux, "    - file version=0x%x num headers=%d",
948              GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
949     return VLC_SUCCESS;
950 }
951 /**
952  * This function parses the PROP properties chunk.
953  */
954 static int HeaderPROP( demux_t *p_demux )
955 {
956     demux_sys_t *p_sys = p_demux->p_sys;
957
958     uint8_t p_buffer[40];
959     int i_flags;
960
961     if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
962         return VLC_EGENERIC;
963
964     msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
965              GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
966     msg_Dbg( p_demux, "    - max packet size=%d avg bitrate=%d",
967              GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
968     msg_Dbg( p_demux, "    - packets count=%d", GetDWBE(&p_buffer[16]) );
969     msg_Dbg( p_demux, "    - duration=%d ms", GetDWBE(&p_buffer[20]) );
970     msg_Dbg( p_demux, "    - preroll=%d ms", GetDWBE(&p_buffer[24]) );
971     msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&p_buffer[28]) );
972     msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&p_buffer[32]) );
973     msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&p_buffer[36]) );
974
975     /* set the duration for export in control */
976     p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
977
978     p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
979
980     i_flags = GetWBE(&p_buffer[38]);
981     msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
982              i_flags,
983              i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
984              i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
985              i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
986
987     return VLC_SUCCESS;
988 }
989 /**
990  * This functions parses the CONT commentairs chunk.
991  */
992 static int HeaderCONT( demux_t *p_demux )
993 {
994     demux_sys_t *p_sys = p_demux->p_sys;
995
996     /* */
997     p_sys->psz_title = StreamReadString2( p_demux->s );
998     if( p_sys->psz_title )
999         msg_Dbg( p_demux, "    - title=`%s'", p_sys->psz_title );
1000
1001     /* */
1002     p_sys->psz_artist = StreamReadString2( p_demux->s );
1003     if( p_sys->psz_artist )
1004         msg_Dbg( p_demux, "    - artist=`%s'", p_sys->psz_artist );
1005
1006     /* */
1007     p_sys->psz_copyright = StreamReadString2( p_demux->s );
1008     if( p_sys->psz_copyright )
1009         msg_Dbg( p_demux, "    - copyright=`%s'", p_sys->psz_copyright );
1010
1011     /* */
1012     p_sys->psz_description = StreamReadString2( p_demux->s );
1013     if( p_sys->psz_description )
1014         msg_Dbg( p_demux, "    - comment=`%s'", p_sys->psz_description );
1015
1016     return VLC_SUCCESS;
1017 }
1018 /**
1019  * This function parses the MDPR (Media properties) chunk.
1020  */
1021 static int HeaderMDPR( demux_t *p_demux )
1022 {
1023     uint8_t p_buffer[30];
1024
1025     if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1026         return VLC_EGENERIC;
1027
1028     const int i_num = GetWBE( &p_buffer[0] );
1029     msg_Dbg( p_demux, "    - id=0x%x", i_num );
1030     msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
1031              GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1032     msg_Dbg( p_demux, "    - max packet size=%d avg packet size=%d",
1033              GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1034     msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&p_buffer[18]) );
1035     msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&p_buffer[22]) );
1036     msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&p_buffer[26]) );
1037
1038     /* */
1039     const uint8_t *p_peek;
1040     int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1041     int i_peek = i_peek_org;
1042     if( i_peek <= 0 )
1043         return VLC_EGENERIC;
1044
1045     char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1046     if( psz_name )
1047     {
1048         msg_Dbg( p_demux, "    - name=`%s'", psz_name );
1049         free( psz_name );
1050     }
1051     char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1052     if( psz_mime )
1053     {
1054         msg_Dbg( p_demux, "    - mime=`%s'", psz_mime );
1055         free( psz_mime );
1056     }
1057     const int i_skip = i_peek_org - i_peek;
1058     if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1059         return VLC_EGENERIC;
1060
1061     /* */
1062     if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1063         return VLC_EGENERIC;
1064
1065     const uint32_t i_size = GetDWBE( p_buffer );
1066     if( i_size > 0 )
1067     {
1068         CodecParse( p_demux, i_size, i_num );
1069         if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
1070             return VLC_EGENERIC;
1071     }
1072     return VLC_SUCCESS;
1073 }
1074 /**
1075  * This function parses DATA chunk (it contains the actual movie data).
1076  */
1077 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1078 {
1079     demux_sys_t *p_sys = p_demux->p_sys;
1080     uint8_t p_buffer[8];
1081
1082     if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1083         return VLC_EGENERIC;
1084
1085     p_sys->i_data_offset    = stream_Tell( p_demux->s ) - 10;
1086     p_sys->i_data_size      = i_size;
1087     p_sys->i_data_packets_count = GetDWBE( p_buffer );
1088     p_sys->i_data_packets   = 0;
1089     p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1090
1091     msg_Dbg( p_demux, "    - packets count=%d next=%u",
1092              p_sys->i_data_packets_count,
1093              (unsigned int)p_sys->i_data_offset_next );
1094     return VLC_SUCCESS;
1095 }
1096 /**
1097  * This function parses the INDX (movie index chunk).
1098  * It is optional but seeking without it is ... hard.
1099  */
1100 static void HeaderINDX( demux_t *p_demux )
1101 {
1102     demux_sys_t *p_sys = p_demux->p_sys;
1103     uint8_t       buffer[20];
1104
1105     uint32_t      i_index_count;
1106
1107     if( p_sys->i_index_offset == 0 )
1108         return;
1109
1110     stream_Seek( p_demux->s, p_sys->i_index_offset );
1111
1112     if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
1113         return ;
1114
1115     const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
1116     const uint32_t i_size      = GetDWBE( &buffer[4] );
1117     int i_version   = GetWBE( &buffer[8] );
1118
1119     msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
1120                  (char*)&i_id, i_size, i_version );
1121
1122     if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
1123         return;
1124
1125     i_index_count = GetDWBE( &buffer[10] );
1126
1127     msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
1128
1129     if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
1130         return;
1131
1132     if( GetDWBE( &buffer[16] ) > 0 )
1133         msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
1134                         GetDWBE( &buffer[16] )  );
1135
1136     /* One extra entry is allocated (that MUST be set to 0) to identify the
1137      * end of the index.
1138      * TODO add a clean entry count (easier to build index on the fly) */
1139     p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
1140     if( !p_sys->p_index )
1141         return;
1142
1143     for( unsigned int i = 0; i < i_index_count; i++ )
1144     {
1145         uint8_t p_entry[14];
1146
1147         if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
1148             return ;
1149
1150         if( GetWBE( &p_entry[0] ) != 0 )
1151         {
1152             msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
1153                               GetWBE( &p_entry[0] ) );
1154             return;
1155         }
1156
1157         real_index_t *p_idx = &p_sys->p_index[i];
1158         
1159         p_idx->i_time_offset = GetDWBE( &p_entry[2] );
1160         p_idx->i_file_offset = GetDWBE( &p_entry[6] );
1161         p_idx->i_frame_index = GetDWBE( &p_entry[10] );
1162
1163         msg_Dbg( p_demux,
1164                  "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
1165                  p_idx->i_time_offset,
1166                  p_idx->i_file_offset,
1167                  p_idx->i_frame_index );
1168     }
1169 }
1170
1171
1172 /**
1173  * This function parses the complete RM headers and move the
1174  * stream pointer to the data to be read.
1175  */
1176 static int HeaderRead( demux_t *p_demux )
1177 {
1178     demux_sys_t *p_sys = p_demux->p_sys;
1179
1180     p_sys->p_meta = vlc_meta_New();
1181
1182     for( ;; )
1183     {
1184         const int64_t i_stream_position = stream_Tell( p_demux->s );
1185         uint8_t header[100];    /* FIXME */
1186
1187         /* Read the header */
1188         if( stream_Read( p_demux->s, header, 10 ) < 10 )
1189             return VLC_EGENERIC;
1190
1191         const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1192                                           header[2], header[3] );
1193         const uint32_t i_size = GetDWBE( &header[4] );
1194         const int i_version = GetWBE( &header[8] );
1195
1196         msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1197                  (char*)&i_id, i_size, i_version );
1198
1199         /* */
1200         if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1201         {
1202             msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1203             return VLC_EGENERIC;
1204         }
1205
1206         int i_ret;
1207         switch( i_id )
1208         {
1209         case VLC_FOURCC('.','R','M','F'):
1210             i_ret = HeaderRMF( p_demux );
1211             break;
1212         case VLC_FOURCC('P','R','O','P'):
1213             i_ret = HeaderPROP( p_demux );
1214             break;
1215         case VLC_FOURCC('C','O','N','T'):
1216             i_ret = HeaderCONT( p_demux );
1217             break;
1218         case VLC_FOURCC('M','D','P','R'):
1219             i_ret = HeaderMDPR( p_demux );
1220             break;
1221         case VLC_FOURCC('D','A','T','A'):
1222             i_ret = HeaderDATA( p_demux, i_size );
1223             break;
1224         default:
1225             /* unknow header */
1226             msg_Dbg( p_demux, "unknown chunk" );
1227             i_ret = VLC_SUCCESS;
1228             break;
1229         }
1230         if( i_ret )
1231             return i_ret;
1232
1233         if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1234             break;
1235
1236         /* Skip unread data */
1237         const int64_t i_stream_current = stream_Tell( p_demux->s );
1238         const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1239
1240         if( i_stream_skip > 0 )
1241         {
1242             if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1243                 return VLC_EGENERIC;
1244         }
1245         else if( i_stream_skip < 0 )
1246         {
1247             return VLC_EGENERIC;
1248         }
1249     }
1250
1251     /* read index if possible */
1252     if( p_sys->i_index_offset > 0 )
1253     {
1254         const int64_t i_position = stream_Tell( p_demux->s );
1255
1256         HeaderINDX( p_demux );
1257
1258         if( stream_Seek( p_demux->s, i_position ) )
1259             return VLC_EGENERIC;
1260     }
1261     return VLC_SUCCESS;
1262 }
1263
1264 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1265 {
1266     demux_sys_t *p_sys = p_demux->p_sys;
1267
1268     /* Title */
1269     p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1270     if( p_sys->psz_title )
1271         msg_Dbg( p_demux, "    - title=`%s'", p_sys->psz_title );
1272
1273     /* Authors */
1274     p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1275     if( p_sys->psz_artist )
1276         msg_Dbg( p_demux, "    - artist=`%s'", p_sys->psz_artist );
1277
1278     /* Copyright */
1279     p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1280     if( p_sys->psz_copyright )
1281         msg_Dbg( p_demux, "    - copyright=`%s'", p_sys->psz_copyright );
1282
1283     /* Comment */
1284     p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1285     if( p_sys->psz_description )
1286         msg_Dbg( p_demux, "    - Comment=`%s'", p_sys->psz_description );
1287 }
1288
1289 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1290 {
1291     demux_sys_t *p_sys = p_demux->p_sys;
1292
1293     if( i_data < 34 )
1294         return VLC_EGENERIC;
1295
1296     /* */
1297     es_format_t fmt;
1298     es_format_Init( &fmt, VIDEO_ES,
1299                     VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1300     fmt.video.i_width = GetWBE( &p_data[12] );
1301     fmt.video.i_height= GetWBE( &p_data[14] );
1302     fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1303     fmt.video.i_frame_rate_base = 1 << 16;
1304
1305     fmt.i_extra = 8;
1306     fmt.p_extra = malloc( 8 );
1307     if( !fmt.p_extra )
1308         return VLC_ENOMEM;
1309
1310     memcpy( fmt.p_extra, &p_data[26], 8 );
1311
1312     //msg_Dbg( p_demux, "    - video 0x%08x 0x%08x", dw0, dw1 );
1313
1314     /* */
1315     switch( GetDWBE( &p_data[30] ) )
1316     {
1317     case 0x10003000:
1318     case 0x10003001:
1319         fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1320         break;
1321     case 0x20001000:
1322     case 0x20100001:
1323     case 0x20200002:
1324     case 0x20201002:
1325         fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1326         break;
1327     case 0x30202002:
1328         fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1329         break;
1330     case 0x40000000:
1331         fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1332         break;
1333     }
1334     msg_Dbg( p_demux, "    - video %4.4s %dx%d - %8.8x",
1335              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1336
1337     real_track_t *tk = malloc( sizeof( *tk ) );
1338     if( !tk )
1339     {
1340         es_format_Clean( &fmt );
1341         return VLC_ENOMEM;
1342     }
1343     tk->i_out_subpacket = 0;
1344     tk->i_subpacket = 0;
1345     tk->i_subpackets = 0;
1346     tk->p_subpackets = NULL;
1347     tk->p_subpackets_timecode = NULL;
1348     tk->i_id = i_tk_id;
1349     tk->fmt = fmt;
1350     tk->i_frame_num = -1;
1351     tk->i_frame_size = 0;
1352     tk->p_frame = NULL;
1353     tk->i_last_dts = 0;
1354     tk->p_es = es_out_Add( p_demux->out, &fmt );
1355
1356     TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1357     return VLC_SUCCESS;
1358 }
1359 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1360 {
1361     demux_sys_t *p_sys = p_demux->p_sys;
1362     es_format_t fmt;
1363
1364     if( i_data < 6 )
1365         return VLC_EGENERIC;
1366     
1367     int i_flavor = 0;
1368     int i_coded_frame_size = 0;
1369     int i_subpacket_h = 0;
1370     int i_frame_size = 0;
1371     int i_subpacket_size = 0;
1372     char p_genr[4];
1373     int i_version = GetWBE( &p_data[4] );
1374     int i_extra_codec = 0;
1375
1376     msg_Dbg( p_demux, "    - audio version=%d", i_version );
1377
1378     es_format_Init( &fmt, AUDIO_ES, 0 );
1379
1380     RVoid( &p_data, &i_data, 6 );                         /* 4 + version */
1381     if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1382     {
1383         RVoid( &p_data, &i_data, 2 + 10 + 4 );
1384
1385         /* Meta Datas */
1386         CodecMetaRead( p_demux, &p_data, &i_data );
1387
1388         RVoid( &p_data, &i_data, 1 + 1 );
1389         if( i_data >= 4 )
1390             memcpy( &fmt.i_codec, p_data, 4 );
1391         RVoid( &p_data, &i_data, 4 );
1392
1393         fmt.audio.i_channels = 1;      /* This is always the case in rm3 */
1394         fmt.audio.i_rate = 8000;
1395
1396         msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1397              (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1398     }
1399     else  /* RMF version 4/5 or .ra version 4 */
1400     {
1401         RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1402         i_flavor = R16( &p_data, &i_data );
1403         i_coded_frame_size = R32( &p_data, &i_data );
1404         RVoid( &p_data, &i_data, 4 + 4 + 4 );
1405         i_subpacket_h = R16( &p_data, &i_data );
1406         i_frame_size = R16( &p_data, &i_data );
1407         i_subpacket_size = R16( &p_data, &i_data );
1408         if( (!i_subpacket_size && !p_sys->b_real_audio ) ||
1409             !i_frame_size || !i_coded_frame_size )
1410              return VLC_EGENERIC;
1411         RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1412
1413         fmt.audio.i_rate = R16( &p_data, &i_data );
1414         RVoid( &p_data, &i_data, 2 );
1415         fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1416         fmt.audio.i_channels = R16( &p_data, &i_data );
1417         fmt.audio.i_blockalign = i_frame_size;
1418
1419         if( i_version == 5 )
1420         {
1421             if( i_data >= 8 )
1422             {
1423                 memcpy( p_genr,       &p_data[0], 4 );
1424                 memcpy( &fmt.i_codec, &p_data[4], 4 );
1425             }
1426             RVoid( &p_data, &i_data, 8 );
1427         }
1428         else /* version 4 */
1429         {
1430             if( i_data > 0 )
1431                 RVoid( &p_data, &i_data, 1 + *p_data );
1432             if( i_data >= 1 + 4 )
1433                 memcpy( &fmt.i_codec, &p_data[1], 4 );
1434             if( i_data > 0 )
1435                 RVoid( &p_data, &i_data, 1 + *p_data );
1436         }
1437
1438         msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1439              (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1440
1441         RVoid( &p_data, &i_data, 3 );
1442
1443         if( p_sys->b_real_audio )
1444         {
1445             CodecMetaRead( p_demux, &p_data, &i_data );
1446         }
1447         else
1448         {
1449             if( i_version == 5 )
1450                 RVoid( &p_data, &i_data, 1 );
1451             i_extra_codec = R32( &p_data, &i_data );
1452         }
1453     }
1454
1455     switch( fmt.i_codec )
1456     {
1457     case VLC_FOURCC('l','p','c','J'):
1458         fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1459     case VLC_FOURCC('1','4','_','4'):
1460         fmt.audio.i_blockalign = 0x14 ;
1461         break;
1462
1463     case VLC_FOURCC('2','8','_','8'):
1464         fmt.audio.i_blockalign = i_coded_frame_size;
1465         break;
1466
1467     case VLC_FOURCC( 'd','n','e','t' ):
1468         fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1469         break;
1470
1471     case VLC_FOURCC( 'r','a','a','c' ):
1472     case VLC_FOURCC( 'r','a','c','p' ):
1473         fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1474
1475         if( i_extra_codec > 0 )
1476         {
1477             i_extra_codec--;
1478             RVoid( &p_data, &i_data, 1 );
1479         }
1480         if( i_extra_codec > 0 )
1481         {
1482             fmt.p_extra = malloc( fmt.i_extra );
1483             if( !fmt.p_extra || fmt.i_extra > i_data )
1484                 return VLC_ENOMEM;
1485
1486             fmt.i_extra = i_extra_codec;
1487             memcpy( fmt.p_extra, p_data, fmt.i_extra );
1488         }
1489         break;
1490
1491     case VLC_FOURCC('s','i','p','r'):
1492         fmt.audio.i_flavor = i_flavor;
1493     case VLC_FOURCC('c','o','o','k'):
1494     case VLC_FOURCC('a','t','r','c'):
1495         if( !memcmp( p_genr, "genr", 4 ) )
1496             fmt.audio.i_blockalign = i_subpacket_size;
1497         else
1498             fmt.audio.i_blockalign = i_coded_frame_size;
1499
1500         if( i_extra_codec > 0 )
1501         {
1502             fmt.p_extra = malloc( fmt.i_extra );
1503             if( !fmt.p_extra || fmt.i_extra > i_data )
1504                 return VLC_ENOMEM;
1505
1506             fmt.i_extra = i_extra_codec;
1507             memcpy( fmt.p_extra, p_data, fmt.i_extra );
1508         }
1509
1510         break;
1511
1512     case VLC_FOURCC('r','a','l','f'):
1513         msg_Dbg( p_demux, "    - audio codec not supported=%4.4s",
1514                  (char*)&fmt.i_codec );
1515         break;
1516
1517     default:
1518         msg_Dbg( p_demux, "    - unknown audio codec=%4.4s",
1519                  (char*)&fmt.i_codec );
1520         break;
1521     }
1522     msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );
1523
1524     /* */
1525     real_track_t *tk = malloc( sizeof( *tk ) );
1526     if( !tk )
1527     {
1528         es_format_Clean( &fmt );
1529         return VLC_ENOMEM;
1530     }
1531     tk->i_id = i_tk_id;
1532     tk->fmt = fmt;
1533     tk->i_frame_size = 0;
1534     tk->p_frame = NULL;
1535
1536     tk->i_subpacket_h = i_subpacket_h;
1537     tk->i_subpacket_size = i_subpacket_size;
1538     tk->i_coded_frame_size = i_coded_frame_size;
1539     tk->i_frame_size = i_frame_size;
1540
1541     tk->i_out_subpacket = 0;
1542     tk->i_subpacket = 0;
1543     tk->i_subpackets = 0;
1544     tk->p_subpackets = NULL;
1545     tk->p_subpackets_timecode = NULL;
1546     if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
1547         fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1548     {
1549         tk->i_subpackets =
1550             i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1551         tk->p_subpackets =
1552             calloc( tk->i_subpackets, sizeof(block_t *) );
1553         tk->p_subpackets_timecode =
1554             calloc( tk->i_subpackets , sizeof( int64_t ) );
1555     }
1556     else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1557     {
1558         tk->i_subpackets =
1559             i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1560         tk->p_subpackets =
1561             calloc( tk->i_subpackets, sizeof(block_t *) );
1562         tk->p_subpackets_timecode =
1563             calloc( tk->i_subpackets , sizeof( int64_t ) );
1564     }
1565
1566     /* Check if the calloc went correctly */
1567     if( !tk->p_subpackets && !tk->p_subpackets_timecode)
1568     {
1569         free( tk->p_subpackets_timecode );
1570         free( tk->p_subpackets );
1571         free( tk );
1572         msg_Err( p_demux, "Can't alloc subpacket" );
1573         return VLC_EGENERIC;
1574     }
1575
1576     tk->i_last_dts = 0;
1577     tk->p_es = es_out_Add( p_demux->out, &fmt );
1578
1579     TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1580
1581     return VLC_SUCCESS;
1582 }
1583
1584
1585 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1586 {
1587     const uint8_t *p_peek;
1588
1589     msg_Dbg( p_demux, "    - specific data len=%d", i_len );
1590     if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1591         return VLC_EGENERIC;
1592
1593     if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1594     {
1595         return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1596     }
1597     else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1598     {
1599         return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1600     }
1601     return VLC_SUCCESS;
1602 }
1603
1604 /*****************************************************************************
1605  * Helpers: memory buffer fct.
1606  *****************************************************************************/
1607 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1608 {
1609     if( i_size > *pi_data )
1610         i_size = *pi_data;
1611
1612     *pp_data += i_size;
1613     *pi_data -= i_size;
1614 }
1615 #define RX(name, type, size, code ) \
1616 static type name( const uint8_t **pp_data, int *pi_data ) { \
1617     if( *pi_data < (size) )          \
1618         return 0;                    \
1619     type v = code;                   \
1620     RVoid( pp_data, pi_data, size ); \
1621     return v;                        \
1622 }
1623 RX(R8,  uint8_t, 1, **pp_data )
1624 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1625 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1626 static int RLength( const uint8_t **pp_data, int *pi_data )
1627 {
1628     const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1629     if( v0 >= 0x4000 )
1630         return v0 - 0x4000;
1631     return (v0 << 16) | R16( pp_data, pi_data );
1632 }
1633
1634