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