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