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