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