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