]> git.sesse.net Git - vlc/blob - modules/demux/real.c
Most of demux/
[vlc] / modules / demux / real.c
1 /*****************************************************************************
2  * real.c: Real demuxer.
3  *****************************************************************************
4  * Copyright (C) 2004, 2006 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  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>                                      /* malloc(), free() */
31
32 #include <vlc/input.h>
33 #include "charset.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open    ( vlc_object_t * );
39 static void Close  ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("Real demuxer" ) );
43     set_capability( "demux2", 15 );
44     set_category( CAT_INPUT );
45     set_subcategory( SUBCAT_INPUT_DEMUX );
46     set_callbacks( Open, Close );
47     add_shortcut( "real" );
48     add_shortcut( "rm" );
49 vlc_module_end();
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54
55 typedef struct
56 {
57     int         i_id;
58     es_format_t fmt;
59
60     es_out_id_t *p_es;
61
62     int         i_frame;
63     block_t     *p_frame;
64
65     int         i_subpacket_h;
66     int         i_subpacket_size;
67     int         i_coded_frame_size;
68     int         i_frame_size;
69
70     int         i_subpacket;
71     int         i_subpackets;
72     block_t     **p_subpackets;
73     int         i_out_subpacket;
74
75 } real_track_t;
76
77 struct demux_sys_t
78 {
79     int64_t  i_data_offset;
80     int64_t  i_data_size;
81     uint32_t i_data_packets_count;
82     uint32_t i_data_packets;
83     int64_t  i_data_offset_next;
84
85     int  i_our_duration;
86     int  i_mux_rate;
87
88     char* psz_title;
89     char* psz_artist;
90     char* psz_copyright;
91     char* psz_description;
92
93     int          i_track;
94     real_track_t **track;
95
96     uint8_t buffer[65536];
97
98     int64_t     i_pcr;
99 };
100
101 static int Demux( demux_t *p_demux );
102 static int Control( demux_t *p_demux, int i_query, va_list args );
103
104 static int HeaderRead( demux_t *p_demux );
105 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
106
107 /*****************************************************************************
108  * Open
109  *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
111 {
112     demux_t     *p_demux = (demux_t*)p_this;
113     demux_sys_t *p_sys;
114
115     uint8_t     *p_peek;
116
117     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
118     if( strncmp( (char *)p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
119
120     /* Fill p_demux field */
121     p_demux->pf_demux = Demux;
122     p_demux->pf_control = Control;
123     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
124     memset( p_sys, 0, sizeof( demux_sys_t ) );
125     p_sys->i_data_offset = 0;
126     p_sys->i_track = 0;
127     p_sys->track   = NULL;
128     p_sys->i_pcr   = 1;
129
130
131     /* Parse the headers */
132     if( HeaderRead( p_demux ) )
133     {
134         int i;
135         msg_Err( p_demux, "invalid header" );
136         for( i = 0; i < p_sys->i_track; i++ )
137         {
138             real_track_t *tk = p_sys->track[i];
139
140             if( tk->p_es )
141             {
142                 es_out_Del( p_demux->out, tk->p_es );
143             }
144             free( tk );
145         }
146         if( p_sys->i_track > 0 )
147         {
148             free( p_sys->track );
149         }
150         free( p_sys );
151         return VLC_EGENERIC;
152     }
153
154     return VLC_SUCCESS;
155 }
156
157 /*****************************************************************************
158  * Close
159  *****************************************************************************/
160 static void Close( vlc_object_t *p_this )
161 {
162     demux_t *p_demux = (demux_t*)p_this;
163     demux_sys_t *p_sys = p_demux->p_sys;
164     int i;
165
166     for( i = 0; i < p_sys->i_track; i++ )
167     {
168         real_track_t *tk = p_sys->track[i];
169         int j = tk->i_subpackets;
170
171         if( tk->p_frame ) block_Release( tk->p_frame );
172         es_format_Clean( &tk->fmt );
173
174         while(  j-- )
175         {
176             if( tk->p_subpackets[ j ] )
177                 block_Release( tk->p_subpackets[ j ] );
178         }
179         if( !tk->i_subpackets ) free( tk->p_subpackets );
180
181         free( tk );
182     }
183
184     if( p_sys->i_track > 0 ) free( p_sys->track );
185     free( p_sys );
186 }
187
188
189 /*****************************************************************************
190  * Demux:
191  *****************************************************************************/
192 static int Demux( demux_t *p_demux )
193 {
194     demux_sys_t *p_sys = p_demux->p_sys;
195     uint8_t     header[18];
196     int         i_size, i_id, i_flags, i;
197     int64_t     i_pts;
198     real_track_t *tk = NULL;
199     vlc_bool_t  b_selected;
200
201     if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
202         p_sys->i_data_packets_count )
203     {
204         if( stream_Read( p_demux->s, header, 18 ) < 18 )
205         {
206             return 0;
207         }
208         if( strncmp( (char *)header, "DATA", 4 ) )
209         {
210             return 0;
211         }
212         p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
213         p_sys->i_data_size   = GetDWBE( &header[4] );
214         p_sys->i_data_packets_count = GetDWBE( &header[10] );
215         p_sys->i_data_packets = 0;
216         p_sys->i_data_offset_next = GetDWBE( &header[14] );
217
218         msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
219                  p_sys->i_data_packets_count,
220                  (uint32_t)p_sys->i_data_offset_next );
221     }
222
223     if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
224
225     i_size = GetWBE( &header[2] ) - 12;
226     i_id   = GetWBE( &header[4] );
227     i_pts  = 1000 * GetDWBE( &header[6] );
228     i_pts += 1000; /* Avoid 0 pts */
229     i_flags= header[11]; /* flags 0x02 -> keyframe */
230
231 #if 0
232     msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
233              p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
234 #endif
235
236     p_sys->i_data_packets++;
237
238     stream_Read( p_demux->s, p_sys->buffer, i_size );
239
240     for( i = 0; i < p_sys->i_track; i++ )
241     {
242         if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
243     }
244
245     if( tk == NULL )
246     {
247         msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
248         return 1;
249     }
250     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
251
252     if( tk->fmt.i_cat == VIDEO_ES && b_selected )
253     {
254         uint8_t *p = p_sys->buffer;
255
256         while( p < &p_sys->buffer[i_size - 2] )
257         {
258             uint8_t h = *p++;
259             int     i_len = 0;
260             int     i_copy;
261             int     i_subseq = 0;
262             int     i_seqnum = 0;
263             int     i_offset = 0;
264
265             if( (h&0xc0) == 0x40 )
266             {
267                 /* Short header */
268                 p++;
269                 i_len = &p_sys->buffer[i_size] - p;
270             }
271             else
272             {
273                 if( (h&0x40) == 0 )
274                 {
275                     i_subseq = (*p++)&0x7f;
276                 }
277                 i_len = (p[0] << 8)|p[1]; p += 2;
278                 if( (i_len&0xc000) == 0 )
279                 {
280                     i_len <<= 16;
281                     i_len |= (p[0] << 8)|p[1]; p += 2;
282                     i_len &= 0x3fffffff;
283                 }
284                 else
285                 {
286                     i_len &= 0x3fff;
287                 }
288
289                 i_offset = (p[0] << 8)|p[1]; p += 2;
290                 if( (i_offset&0xc000) == 0 )
291                 {
292                     i_offset <<= 16;
293                     i_offset |= (p[0] << 8)|p[1]; p += 2;
294                     i_offset &= 0x3fffffff;
295                 }
296                 else
297                 {
298                     i_offset &= 0x3fff;
299                 }
300                 i_seqnum = *p++;
301             }
302
303             i_copy = i_len - i_offset;
304             if( i_copy > &p_sys->buffer[i_size] - p )
305             {
306                 i_copy = &p_sys->buffer[i_size] - p;
307             }
308             else if( i_copy < 0 )
309             {
310                 break;
311             }
312
313             msg_Dbg( p_demux, "    - len=%d offset=%d size=%d subseq=%d seqnum=%d",
314                      i_len, i_offset, i_copy, i_subseq, i_seqnum );
315
316             if( (h&0xc0) == 0x80 )
317             {
318                 /* last fragment -> fixes */
319                 i_copy = i_offset;
320                 i_offset = i_len - i_copy;
321                 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
322                          i_copy, i_offset );
323             }
324
325             if( tk->p_frame &&
326                 ( tk->p_frame->i_dts != i_pts ||
327                   tk->i_frame != i_len ) )
328             {
329                 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
330
331                 if( p_sys->i_pcr < tk->p_frame->i_dts )
332                 {
333                     p_sys->i_pcr = tk->p_frame->i_dts;
334
335                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
336                                     (int64_t)p_sys->i_pcr );
337                 }
338                 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
339
340                 tk->i_frame = 0;
341                 tk->p_frame = NULL;
342             }
343
344             if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
345             {
346                 /* no fragment */
347                 i_len = i_copy;
348                 i_offset = 0;
349             }
350
351
352             if( tk->p_frame == NULL )
353             {
354                 msg_Dbg( p_demux, "new frame size=%d", i_len );
355                 tk->i_frame = i_len;
356                 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
357                 {
358                     return -1;
359                 }
360                 memset( &tk->p_frame->p_buffer[8], 0, i_len );
361                 tk->p_frame->i_dts = i_pts;
362                 tk->p_frame->i_pts = i_pts;
363
364                 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
365                 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0;      /* chunk counts */
366             }
367
368             if( i_offset < tk->i_frame)
369             {
370                 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
371
372                 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
373                          i_ck, i_offset, i_copy );
374
375                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
376
377                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
378             }
379
380             p += i_copy;
381
382             if( (h&0xc0) != 0x80 )
383             {
384                 break;
385             }
386
387 #if 0
388             if( tk->p_frame )
389             {
390                 /* append data */
391                 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
392
393                 if( (h&0xc0) == 0x80 )
394                 {
395                     /* last fragment */
396                     i_copy = i_offset;
397                     i_offset = i_len - i_offset;
398
399                     ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
400                     memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
401                     p += i_copy;
402
403                     if( p_sys->i_pcr < tk->p_frame->i_dts )
404                     {
405                         p_sys->i_pcr = tk->p_frame->i_dts;
406                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
407                                         (int64_t)p_sys->i_pcr );
408                     }
409                     es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
410
411                     tk->i_frame = 0;
412                     tk->p_frame = NULL;
413
414                     continue;
415                 }
416
417                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
418                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
419                 break;
420             }
421
422             if( (h&0xc0) != 0x00 )
423             {
424                 block_t *p_frame;
425
426                 /* not fragmented */
427                 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
428                 {
429                     return -1;
430                 }
431                 p_frame->i_dts = i_pts;
432                 p_frame->i_pts = i_pts;
433
434                 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
435                 ((uint32_t*)p_frame->p_buffer)[1] = 1;
436                 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
437                 memcpy( &p_frame->p_buffer[8], p, i_copy );
438
439                 p += i_copy;
440
441                 if( p_sys->i_pcr < p_frame->i_dts )
442                 {
443                     p_sys->i_pcr = p_frame->i_dts;
444                     es_out_Control( p_demux->out, ES_OUT_SET_PCR,
445                                     (int64_t)p_sys->i_pcr );
446                 }
447                 es_out_Send( p_demux->out, tk->p_es, p_frame );
448             }
449             else
450             {
451                 /* First fragment */
452                 tk->i_frame = i_len;
453                 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
454                 {
455                     return -1;
456                 }
457                 memset( &tk->p_frame->p_buffer[8], 0, i_len );
458                 tk->p_frame->i_dts = i_pts;
459                 tk->p_frame->i_pts = i_pts;
460
461                 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
462                 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1;      /* chunk counts */
463                 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
464                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
465
466                 break;
467             }
468 #endif
469         }
470     }
471     else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
472     {
473         /* Set PCR */
474         if( p_sys->i_pcr < i_pts )
475         {
476             p_sys->i_pcr = i_pts;
477             es_out_Control( p_demux->out, ES_OUT_SET_PCR,
478                             (int64_t)p_sys->i_pcr );
479         }
480
481         if( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
482         {
483             int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
484             uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
485
486             int i;
487             for( i = 0; i < i_sub; i++ )
488             {
489                 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
490                 block_t *p_block = block_New( p_demux, i_sub_size );
491                 if( p_block )
492                 {
493                     memcpy( p_block->p_buffer, p_sub, i_sub_size );
494                     p_sub += i_sub_size;
495
496                     p_block->i_dts =
497                     p_block->i_pts = ( i == 0 ? i_pts : 0 );
498
499                     es_out_Send( p_demux->out, tk->p_es, p_block );
500                 }
501             }
502         }
503         else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
504                  tk->fmt.i_codec == VLC_FOURCC('2','8','_','8') )
505         {
506             uint8_t *p_buf = p_sys->buffer;
507             int y = tk->i_subpacket / (tk->i_frame_size /tk->i_subpacket_size);
508             int i_index, i;
509
510             /* Sanity check */
511             if( i_flags & 2 ) y = tk->i_subpacket = 0;
512
513             if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) )
514             for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
515             {
516                 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
517                 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
518                 p_buf += tk->i_subpacket_size;
519
520                 i_index = tk->i_subpacket_h * i +
521                     ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
522
523                 p_block->i_dts = p_block->i_pts = i_pts;
524                 tk->p_subpackets[i_index] = p_block;
525                 tk->i_subpacket++;
526             }
527
528             if( tk->fmt.i_codec == VLC_FOURCC('2','8','_','8') )
529             for( i = 0; i < tk->i_subpacket_h / 2; i++ )
530             {
531                 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
532                 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
533                 p_buf += tk->i_coded_frame_size;
534
535                 i_index = (i * 2 * tk->i_frame_size) /
536                     tk->i_coded_frame_size + y;
537
538                 p_block->i_dts = p_block->i_pts = i_pts;
539                 tk->p_subpackets[i_index] = p_block;
540                 tk->i_subpacket++;
541             }
542
543             while( tk->i_out_subpacket != tk->i_subpackets &&
544                    tk->p_subpackets[tk->i_out_subpacket] )
545             {
546                 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
547                 tk->p_subpackets[tk->i_out_subpacket] = 0;
548
549                 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
550                 es_out_Send( p_demux->out, tk->p_es, p_block );
551
552                 tk->i_out_subpacket++;
553             }
554
555             if( tk->i_subpacket == tk->i_subpackets &&
556                 tk->i_out_subpacket != tk->i_subpackets )
557             {
558                 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
559                           "this shouldn't happen" );
560             }
561
562             if( tk->i_subpacket == tk->i_subpackets )
563             {
564                 tk->i_subpacket = 0;
565                 tk->i_out_subpacket = 0;
566             }
567         }
568         else
569         {
570             block_t *p_block = block_New( p_demux, i_size );
571
572             if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
573             {
574                 uint8_t *src = p_sys->buffer;
575                 uint8_t *dst = p_block->p_buffer;
576
577                 /* byte swap data */
578                 while( dst < &p_block->p_buffer[i_size- 1])
579                 {
580                     *dst++ = src[1];
581                     *dst++ = src[0];
582
583                     src += 2;
584                 }
585             }
586             else
587             {
588                 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
589             }
590             p_block->i_dts = p_block->i_pts = i_pts;
591             es_out_Send( p_demux->out, tk->p_es, p_block );
592         }
593     }
594
595
596     return 1;
597 }
598
599 /*****************************************************************************
600  * Control:
601  *****************************************************************************/
602 static int Control( demux_t *p_demux, int i_query, va_list args )
603 {
604     demux_sys_t *p_sys = p_demux->p_sys;
605 #if 0
606     double f, *pf;
607     int64_t i64;
608 #endif
609     int64_t *pi64;
610
611     switch( i_query )
612     {
613 #if 0
614         case DEMUX_GET_POSITION:
615             pf = (double*) va_arg( args, double* );
616             i64 = stream_Size( p_demux->s );
617             if( i64 > 0 )
618             {
619                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
620             }
621             else
622             {
623                 *pf = 0.0;
624             }
625             return VLC_SUCCESS;
626
627         case DEMUX_SET_POSITION:
628             f = (double) va_arg( args, double );
629             i64 = stream_Size( p_demux->s );
630
631             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
632
633             return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
634
635         case DEMUX_GET_TIME:
636             pi64 = (int64_t*)va_arg( args, int64_t * );
637             if( p_sys->i_mux_rate > 0 )
638             {
639                 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
640                 return VLC_SUCCESS;
641             }
642             *pi64 = 0;
643             return VLC_EGENERIC;
644 #endif
645
646         case DEMUX_GET_LENGTH:
647             pi64 = (int64_t*)va_arg( args, int64_t * );
648             
649             /* the commented following lines are fen's implementation, which doesn't seem to
650              * work for one reason or another -- FK */
651             /*if( p_sys->i_mux_rate > 0 )
652             {
653                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
654                 return VLC_SUCCESS;
655             }*/
656             if( p_sys->i_our_duration > 0 )
657             {
658                 /* our stored duration is in ms, so... */
659                 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
660                 
661                 return VLC_SUCCESS;
662             }
663             *pi64 = 0;
664             return VLC_EGENERIC;
665
666         case DEMUX_GET_META:
667         {
668             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
669
670             /* the core will crash if we provide NULL strings, so check 
671              * every string first */
672             if( p_sys->psz_title )
673                 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
674             if( p_sys->psz_artist )
675                 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
676             if( p_sys->psz_copyright )
677                 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
678             if( p_sys->psz_description )
679                 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
680             return VLC_SUCCESS;
681         }
682
683         case DEMUX_SET_TIME:
684         case DEMUX_GET_FPS:
685         default:
686             return VLC_EGENERIC;
687     }
688     return VLC_EGENERIC;
689 }
690
691 /*****************************************************************************
692  * HeaderRead:
693  *****************************************************************************/
694 static int HeaderRead( demux_t *p_demux )
695 {
696     demux_sys_t *p_sys = p_demux->p_sys;
697     uint8_t header[100];    /* FIXME */
698
699     uint32_t    i_id;
700     uint32_t    i_size;
701     int64_t     i_skip;
702     int         i_version;
703
704     for( ;; )
705     {
706         /* Read the header */
707         if( stream_Read( p_demux->s, header, 10 ) < 10 )
708         {
709             return VLC_EGENERIC;
710         }
711         i_id        = VLC_FOURCC( header[0], header[1], header[2], header[3] );
712         i_size      = GetDWBE( &header[4] );
713         i_version   = GetWBE( &header[8] );
714
715         msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
716                  (char*)&i_id, i_size, i_version );
717
718         if( i_size < 10 )
719         {
720             msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
721             return VLC_EGENERIC;
722         }
723         i_skip = i_size - 10;
724
725         if( i_id == VLC_FOURCC('.','R','M','F') )
726         {
727             if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
728             msg_Dbg( p_demux, "    - file version=0x%x num headers=%d",
729                      GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
730
731             i_skip -= 8;
732         }
733         else if( i_id == VLC_FOURCC('P','R','O','P') )
734         {
735             int i_flags;
736
737             if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
738
739             msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
740                      GetDWBE(&header[0]), GetDWBE(&header[4]) );
741             msg_Dbg( p_demux, "    - max packet size=%d avg bitrate=%d",
742                      GetDWBE(&header[8]), GetDWBE(&header[12]) );
743             msg_Dbg( p_demux, "    - packets count=%d", GetDWBE(&header[16]) );
744             msg_Dbg( p_demux, "    - duration=%d ms", GetDWBE(&header[20]) );
745             msg_Dbg( p_demux, "    - preroll=%d ms", GetDWBE(&header[24]) );
746             msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&header[28]) );
747             msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&header[32]) );
748             msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&header[36]) );
749             
750             /* set the duration for export in control */
751             p_sys->i_our_duration = (int)GetDWBE(&header[20]);
752             
753             i_flags = GetWBE(&header[38]);
754             msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
755                      i_flags,
756                      i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
757                      i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
758                      i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
759             i_skip -= 40;
760         }
761         else if( i_id == VLC_FOURCC('C','O','N','T') )
762         {
763             int i_len;
764             char *psz;
765             
766             /* FIXME FIXME: should convert from whatever the character
767              * encoding of the input meta data is to UTF-8. */
768
769             stream_Read( p_demux->s, header, 2 );
770             if( ( i_len = GetWBE( header ) ) > 0 )
771             {
772                 psz = malloc( i_len + 1 );
773                 stream_Read( p_demux->s, psz, i_len );
774                 psz[i_len] = '\0';
775
776                 msg_Dbg( p_demux, "    - title=`%s'", psz );
777                 EnsureUTF8( psz );
778                 asprintf( &p_sys->psz_title, psz );
779                 free( psz );
780                 i_skip -= i_len;
781             }
782             i_skip -= 2;
783
784             stream_Read( p_demux->s, header, 2 );
785             if( ( i_len = GetWBE( header ) ) > 0 )
786             {
787                 psz = malloc( i_len + 1 );
788                 stream_Read( p_demux->s, psz, i_len );
789                 psz[i_len] = '\0';
790
791                 msg_Dbg( p_demux, "    - author=`%s'", psz );
792                 EnsureUTF8( psz );
793                 asprintf( &p_sys->psz_artist, psz );
794                 free( psz );
795                 i_skip -= i_len;
796             }
797             i_skip -= 2;
798
799             stream_Read( p_demux->s, header, 2 );
800             if( ( i_len = GetWBE( header ) ) > 0 )
801             {
802                 psz = malloc( i_len + 1 );
803                 stream_Read( p_demux->s, psz, i_len );
804                 psz[i_len] = '\0';
805
806                 msg_Dbg( p_demux, "    - copyright=`%s'", psz );
807                 EnsureUTF8( psz );
808                 asprintf( &p_sys->psz_copyright, psz );
809                 free( psz );
810                 i_skip -= i_len;
811             }
812             i_skip -= 2;
813
814             stream_Read( p_demux->s, header, 2 );
815             if( ( i_len = GetWBE( header ) ) > 0 )
816             {
817                 psz = malloc( i_len + 1 );
818                 stream_Read( p_demux->s, psz, i_len );
819                 psz[i_len] = '\0';
820
821                 msg_Dbg( p_demux, "    - comment=`%s'", psz );
822                 EnsureUTF8( psz );
823                 asprintf( &p_sys->psz_description, psz );
824                 free( psz );
825                 i_skip -= i_len;
826             }
827             i_skip -= 2;
828         }
829         else if( i_id == VLC_FOURCC('M','D','P','R') )
830         {
831             /* Media properties header */
832             int  i_num;
833             int  i_len;
834             char *psz;
835
836             if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
837             i_num = GetWBE( header );
838             msg_Dbg( p_demux, "    - id=0x%x", i_num );
839             msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
840                      GetDWBE(&header[2]), GetDWBE(&header[6]) );
841             msg_Dbg( p_demux, "    - max packet size=%d avg packet size=%d",
842                      GetDWBE(&header[10]), GetDWBE(&header[14]) );
843             msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&header[18]) );
844             msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&header[22]) );
845             msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&header[26]) );
846             
847             i_skip -= 30;
848
849             stream_Read( p_demux->s, header, 1 );
850             if( ( i_len = header[0] ) > 0 )
851             {
852                 psz = malloc( i_len + 1 );
853                 stream_Read( p_demux->s, psz, i_len );
854                 psz[i_len] = '\0';
855
856                 msg_Dbg( p_demux, "    - name=`%s'", psz );
857                 free( psz );
858                 i_skip -= i_len;
859             }
860             i_skip--;
861
862             stream_Read( p_demux->s, header, 1 );
863             if( ( i_len = header[0] ) > 0 )
864             {
865                 psz = malloc( i_len + 1 );
866                 stream_Read( p_demux->s, psz, i_len );
867                 psz[i_len] = '\0';
868
869                 msg_Dbg( p_demux, "    - mime=`%s'", psz );
870                 free( psz );
871                 i_skip -= i_len;
872             }
873             i_skip--;
874
875             stream_Read( p_demux->s, header, 4 );
876             if( ( i_len = GetDWBE( header ) ) > 0 )
877             {
878                 ReadCodecSpecificData( p_demux, i_len, i_num );
879                 stream_Read( p_demux->s, NULL, i_len );
880
881                 i_skip -= i_len;
882             }
883             i_skip -= 4;
884         }
885         else if( i_id == VLC_FOURCC('D','A','T','A') )
886         {
887             stream_Read( p_demux->s, header, 8 );
888
889             p_sys->i_data_offset    = stream_Tell( p_demux->s ) - 10;
890             p_sys->i_data_size      = i_size;
891             p_sys->i_data_packets_count = GetDWBE( header );
892             p_sys->i_data_packets   = 0;
893             p_sys->i_data_offset_next = GetDWBE( &header[4] );
894
895             msg_Dbg( p_demux, "    - packets count=%d next=%u",
896                      p_sys->i_data_packets_count,
897                      (uint32_t)p_sys->i_data_offset_next );
898
899             /* we have finished the header */
900             break;
901         }
902         else
903         {
904             /* unknow header */
905             msg_Dbg( p_demux, "unknown chunk" );
906         }
907
908         if( i_skip < 0 ) return VLC_EGENERIC;
909         stream_Read( p_demux->s, NULL, i_skip );
910     }
911
912     /* TODO read index if possible */
913
914     return VLC_SUCCESS;
915 }
916
917 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
918 {
919     demux_sys_t *p_sys = p_demux->p_sys;
920     es_format_t fmt;
921     real_track_t *tk;
922     uint8_t *p_peek;
923
924     msg_Dbg( p_demux, "    - specific data len=%d", i_len );
925     if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
926
927     if( !strncmp( (char *)&p_peek[4], "VIDO", 4 ) )
928     {
929         es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
930                         p_peek[10], p_peek[11] ) );
931         fmt.video.i_width = GetWBE( &p_peek[12] );
932         fmt.video.i_height= GetWBE( &p_peek[14] );
933
934         fmt.i_extra = 8;
935         fmt.p_extra = malloc( 8 );
936         ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
937         ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
938
939         msg_Dbg( p_demux, "    - video 0x%08x 0x%08x",
940                  ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
941
942         if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
943             GetDWBE( &p_peek[30] ) == 0x10003001 )
944         {
945             fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
946         }
947         else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
948                  GetDWBE( &p_peek[30] ) == 0x20100001 ||
949                  GetDWBE( &p_peek[30] ) == 0x20200002 )
950         {
951             fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
952         }
953         else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
954         {
955             fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
956         }
957         else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
958         {
959             fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
960         }
961
962         msg_Dbg( p_demux, "    - video %4.4s %dx%d",
963                  (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
964
965         tk = malloc( sizeof( real_track_t ) );
966         tk->i_out_subpacket = 0;
967         tk->i_subpacket = 0;
968         tk->i_subpackets = 0;
969         tk->p_subpackets = NULL;
970         tk->i_id = i_num;
971         tk->fmt = fmt;
972         tk->i_frame = 0;
973         tk->p_frame = NULL;
974         tk->p_es = es_out_Add( p_demux->out, &fmt );
975
976         TAB_APPEND( p_sys->i_track, p_sys->track, tk );
977     }
978     else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
979     {
980         int i_version = GetWBE( &p_peek[4] );
981         int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
982         int i_frame_size, i_subpacket_size;
983
984         msg_Dbg( p_demux, "    - audio version=%d", i_version );
985
986         p_peek += 6;
987         es_format_Init( &fmt, AUDIO_ES, 0 );
988
989         if( i_version == 3 )
990         {
991             msg_Dbg( p_demux, "    - audio version 3 is not supported!" );
992             return VLC_EGENERIC;
993         }
994
995         p_peek += 2; /* 00 00 */
996         p_peek += 4; /* .ra4 or .ra5 */
997         p_peek += 4; /* ?? */
998         p_peek += 2; /* version (4 or 5) */
999         i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1000         i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1001         i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4;
1002         p_peek += 4; /* ?? */
1003         p_peek += 4; /* ?? */
1004         p_peek += 4; /* ?? */
1005         i_subpacket_h = GetWBE( p_peek ); p_peek += 2;
1006         i_frame_size = GetWBE( p_peek ); p_peek += 2;
1007         i_subpacket_size = GetWBE( p_peek ); p_peek += 2;
1008         p_peek += 2; /* ?? */
1009
1010         if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1011
1012         fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2;
1013         p_peek += 2; /* ?? */
1014         fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;
1015         fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2;
1016         fmt.audio.i_blockalign = i_frame_size;
1017
1018         if( i_version == 5 )
1019         {
1020             p_peek += 4; /* genr */
1021             memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1022         }
1023         else
1024         {
1025             p_peek += p_peek[0] + 1; /* descr 1 */
1026             memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1027             p_peek += p_peek[0] + 1;
1028         }
1029
1030         msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
1031                  (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1032
1033         p_peek += 3; /* ?? */
1034         if( i_version == 5 ) p_peek++;
1035
1036         switch( fmt.i_codec )
1037         {
1038         case VLC_FOURCC( 'd', 'n', 'e', 't' ):
1039             fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1040             break;
1041
1042         case VLC_FOURCC( 'r', 'a', 'a', 'c' ):
1043         case VLC_FOURCC( 'r', 'a', 'c', 'p' ):
1044             fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1045             if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1046             if( fmt.i_extra > 0 )
1047             {
1048                 fmt.p_extra = malloc( fmt.i_extra );
1049                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1050             }
1051
1052             fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1053             break;
1054
1055         case VLC_FOURCC('c','o','o','k'):
1056             fmt.audio.i_blockalign = i_subpacket_size;
1057             if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
1058             fmt.p_extra = malloc( fmt.i_extra );
1059             memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
1060             break;
1061
1062         case VLC_FOURCC('2','8','_','8'):
1063             fmt.audio.i_blockalign = i_coded_frame_size;
1064             break;
1065
1066         default:
1067             msg_Dbg( p_demux, "    - unknown audio codec=%4.4s",
1068                      (char*)&fmt.i_codec );
1069             break;
1070         }
1071
1072         if( fmt.i_codec != 0 )
1073         {
1074             int i;
1075
1076             msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );
1077
1078             tk = malloc( sizeof( real_track_t ) );
1079             tk->i_id = i_num;
1080             tk->fmt = fmt;
1081             tk->i_frame = 0;
1082             tk->p_frame = NULL;
1083
1084             tk->i_subpacket_h = i_subpacket_h;
1085             tk->i_subpacket_size = i_subpacket_size;
1086             tk->i_coded_frame_size = i_coded_frame_size;
1087             tk->i_frame_size = i_frame_size;
1088
1089             tk->i_out_subpacket = 0;
1090             tk->i_subpacket = 0;
1091             tk->i_subpackets = 0;
1092             tk->p_subpackets = NULL;
1093             if( fmt.i_codec == VLC_FOURCC('c','o','o','k') )
1094             {
1095                 tk->i_subpackets =
1096                     i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1097                 tk->p_subpackets =
1098                     malloc( tk->i_subpackets * sizeof(block_t *) );
1099             }
1100             else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1101             {
1102                 tk->i_subpackets =
1103                     i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1104                 tk->p_subpackets =
1105                     malloc( tk->i_subpackets * sizeof(block_t *) );
1106             }
1107
1108             for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1109
1110             tk->p_es = es_out_Add( p_demux->out, &fmt );
1111
1112             TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1113         }
1114     }
1115
1116     return VLC_SUCCESS;
1117 }