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