1 /*****************************************************************************
2 * real.c: Real demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004, 2006-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 * Status of this demuxer:
29 * version v3 w/ 14_4/lpcJ is ok.
30 * version v4/5: - atrac3 is ok.
32 * - raac, racp are ok.
33 * - dnet is twisted "The byte order of the data is reversed
34 * from standard AC3" but ok
37 * - ralf is unsupported, but hardly any sample exist.
38 * - mp3 is unsupported, one sample exists...
42 * v3 and v4/5 headers are parsed.
46 /*****************************************************************************
48 *****************************************************************************/
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
57 #include <vlc_demux.h>
58 #include <vlc_charset.h>
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
70 set_description( N_("Real demuxer" ) )
71 set_capability( "demux", 0 )
72 set_category( CAT_INPUT )
73 set_subcategory( SUBCAT_INPUT_DEMUX )
74 set_callbacks( Open, Close )
75 add_shortcut( "real", "rm" )
78 /*****************************************************************************
80 *****************************************************************************/
89 unsigned i_frame_size;
94 int i_frame_slice_count;
99 int i_coded_frame_size;
103 block_t **p_subpackets;
104 mtime_t *p_subpackets_timecode;
107 block_t *p_sipr_packet;
108 int i_sipr_subpacket_count;
114 uint32_t i_file_offset;
115 uint32_t i_time_offset;
116 uint32_t i_frame_index;
121 int64_t i_data_offset;
123 uint32_t i_data_packets_count;
124 uint32_t i_data_packets;
125 int64_t i_data_offset_next;
129 int64_t i_our_duration;
134 char* psz_description;
137 real_track_t **track;
140 uint8_t buffer[65536];
144 int64_t i_index_offset;
146 real_index_t *p_index;
149 static const unsigned char i_subpacket_size_sipr[4] = { 29, 19, 37, 20 };
151 static int Demux( demux_t * );
152 static int Control( demux_t *, int i_query, va_list args );
155 static void DemuxVideo( demux_t *, real_track_t *tk, mtime_t i_dts, unsigned i_flags );
156 static void DemuxAudio( demux_t *, real_track_t *tk, mtime_t i_pts, unsigned i_flags );
158 static int ControlSeekByte( demux_t *, int64_t i_bytes );
159 static int ControlSeekTime( demux_t *, mtime_t i_time );
161 static int HeaderRead( demux_t *p_demux );
162 static int CodecParse( demux_t *p_demux, int i_len, int i_num );
164 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
165 static int RLength( const uint8_t **pp_data, int *pi_data );
166 static uint8_t R8( const uint8_t **pp_data, int *pi_data );
167 static uint16_t R16( const uint8_t **pp_data, int *pi_data );
168 static uint32_t R32( const uint8_t **pp_data, int *pi_data );
169 static void SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize);
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 demux_t *p_demux = (demux_t*)p_this;
179 const uint8_t *p_peek;
180 bool b_real_audio = false;
182 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
186 if( !memcmp( p_peek, ".ra", 3 ) )
188 msg_Err( p_demux, ".ra files unsuported" );
191 /* Real Media Format */
192 else if( memcmp( p_peek, ".RMF", 4 ) )
197 /* Fill p_demux field */
198 p_demux->pf_demux = Demux;
199 p_demux->pf_control = Control;
201 p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
205 p_sys->i_data_offset = 0;
208 p_sys->i_pcr = VLC_TS_INVALID;
210 p_sys->b_seek = false;
211 p_sys->b_real_audio = b_real_audio;
213 /* Parse the headers */
214 /* Real Audio files */
217 CodecParse( p_demux, 32, 0 ); /* At least 32 */
218 return VLC_EGENERIC; /* We don't know how to read
219 correctly the data yet */
222 else if( HeaderRead( p_demux ) )
224 msg_Err( p_demux, "invalid header" );
232 /*****************************************************************************
234 *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
237 demux_t *p_demux = (demux_t*)p_this;
238 demux_sys_t *p_sys = p_demux->p_sys;
240 for( int i = 0; i < p_sys->i_track; i++ )
242 real_track_t *tk = p_sys->track[i];
244 es_format_Clean( &tk->fmt );
247 block_Release( tk->p_frame );
249 for( int j = 0; j < tk->i_subpackets; j++ )
251 if( tk->p_subpackets[ j ] )
252 block_Release( tk->p_subpackets[ j ] );
254 free( tk->p_subpackets );
255 free( tk->p_subpackets_timecode );
256 if( tk->p_sipr_packet )
257 block_Release( tk->p_sipr_packet );
260 if( p_sys->i_track > 0 )
261 free( p_sys->track );
263 free( p_sys->psz_title );
264 free( p_sys->psz_artist );
265 free( p_sys->psz_copyright );
266 free( p_sys->psz_description );
267 free( p_sys->p_index );
273 /*****************************************************************************
275 *****************************************************************************/
276 static int Demux( demux_t *p_demux )
278 demux_sys_t *p_sys = p_demux->p_sys;
281 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
282 p_sys->i_data_packets_count )
284 if( stream_Read( p_demux->s, header, 18 ) < 18 )
287 if( memcmp( header, "DATA", 4 ) )
290 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
291 p_sys->i_data_size = GetDWBE( &header[4] );
292 p_sys->i_data_packets_count = GetDWBE( &header[10] );
293 p_sys->i_data_packets = 0;
294 p_sys->i_data_offset_next = GetDWBE( &header[14] );
296 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
297 p_sys->i_data_packets_count,
298 (unsigned int)p_sys->i_data_offset_next );
301 /* Read Packet Header */
302 if( stream_Read( p_demux->s, header, 12 ) < 12 )
304 //const int i_version = GetWBE( &header[0] );
305 const size_t i_size = GetWBE( &header[2] ) - 12;
306 const int i_id = GetWBE( &header[4] );
307 const int64_t i_pts = VLC_TS_0 + 1000 * GetDWBE( &header[6] );
308 const int i_flags= header[11]; /* flags 0x02 -> keyframe */
310 p_sys->i_data_packets++;
311 if( i_size > sizeof(p_sys->buffer) )
313 msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
317 p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
318 if( p_sys->i_buffer < i_size )
321 real_track_t *tk = NULL;
322 for( int i = 0; i < p_sys->i_track; i++ )
324 if( p_sys->track[i]->i_id == i_id )
325 tk = p_sys->track[i];
330 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
334 if( tk->fmt.i_cat == VIDEO_ES )
336 DemuxVideo( p_demux, tk, i_pts, i_flags );
340 assert( tk->fmt.i_cat == AUDIO_ES );
341 DemuxAudio( p_demux, tk, i_pts, i_flags );
345 mtime_t i_pcr = VLC_TS_INVALID;
346 for( int i = 0; i < p_sys->i_track; i++ )
348 real_track_t *tk = p_sys->track[i];
350 if( i_pcr <= VLC_TS_INVALID || ( tk->i_last_dts > VLC_TS_INVALID && tk->i_last_dts < i_pcr ) )
351 i_pcr = tk->i_last_dts;
353 if( i_pcr > VLC_TS_INVALID && i_pcr != p_sys->i_pcr )
355 p_sys->i_pcr = i_pcr;
356 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
361 /*****************************************************************************
363 *****************************************************************************/
364 static int Control( demux_t *p_demux, int i_query, va_list args )
366 demux_sys_t *p_sys = p_demux->p_sys;
373 case DEMUX_GET_POSITION:
374 pf = (double*) va_arg( args, double* );
376 /* read stream size maybe failed in rtsp streaming,
377 so use duration to determin the position at first */
378 if( p_sys->i_our_duration > 0 )
380 if( p_sys->i_pcr > VLC_TS_INVALID )
381 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
387 i64 = stream_Size( p_demux->s );
389 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
395 pi64 = (int64_t*)va_arg( args, int64_t * );
397 if( p_sys->i_our_duration > 0 )
399 *pi64 = p_sys->i_pcr > VLC_TS_INVALID ? p_sys->i_pcr : 0;
403 /* same as GET_POSTION */
404 i64 = stream_Size( p_demux->s );
405 if( p_sys->i_our_duration > 0 && i64 > 0 )
407 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
414 case DEMUX_SET_POSITION:
415 f = (double) va_arg( args, double );
416 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
418 if( !p_sys->p_index && i64 != 0 )
421 msg_Err( p_demux,"Seek No Index Real File failed!" );
422 return VLC_EGENERIC; // no index!
426 /* it is a rtsp stream , it is specials in access/rtsp/... */
427 msg_Dbg(p_demux, "Seek in real rtsp stream!");
428 p_sys->i_pcr = VLC_TS_0 + INT64_C(1000) * ( p_sys->i_our_duration * f );
429 p_sys->b_seek = true;
430 return stream_Seek( p_demux->s, p_sys->i_pcr - VLC_TS_0 );
432 return ControlSeekByte( p_demux, i64 );
435 if( !p_sys->p_index )
438 i64 = (int64_t) va_arg( args, int64_t );
439 return ControlSeekTime( p_demux, i64 );
441 case DEMUX_GET_LENGTH:
442 pi64 = (int64_t*)va_arg( args, int64_t * );
444 if( p_sys->i_our_duration <= 0 )
450 /* our stored duration is in ms, so... */
451 *pi64 = INT64_C(1000) * p_sys->i_our_duration;
456 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
458 /* the core will crash if we provide NULL strings, so check
459 * every string first */
460 if( p_sys->psz_title )
461 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
462 if( p_sys->psz_artist )
463 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
464 if( p_sys->psz_copyright )
465 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
466 if( p_sys->psz_description )
467 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
478 /*****************************************************************************
480 *****************************************************************************/
481 static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
483 demux_sys_t *p_sys = p_demux->p_sys;
485 if( i_dts > VLC_TS_INVALID )
486 tk->i_last_dts = i_dts;
488 if( p_sys->i_pcr > VLC_TS_INVALID || i_dts <= VLC_TS_INVALID )
491 p_sys->i_pcr = i_dts;
492 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
495 static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
497 demux_sys_t *p_sys = p_demux->p_sys;
499 const uint8_t *p_data = p_sys->buffer;
500 int i_data = p_sys->i_buffer;
504 uint8_t i_hdr = R8( &p_data, &i_data );
505 uint8_t i_type = i_hdr >> 6;
514 R8( &p_data, &i_data );
521 else if( i_type == 3 )
523 i_len = RLength( &p_data, &i_data );
524 i_pos = RLength( &p_data, &i_data );
525 i_frame_num = R8( &p_data, &i_data );
531 assert( i_type == 0 || i_type == 2 );
532 i_seq = R8( &p_data, &i_data );
533 i_len = RLength( &p_data, &i_data );
535 i_pos = RLength( &p_data, &i_data );
536 i_frame_num = R8( &p_data, &i_data );
539 if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
541 tk->i_frame_slice = 0;
542 tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
543 tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
544 tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
545 tk->i_frame_num = i_frame_num;
548 block_Release( tk->p_frame );
550 tk->p_frame = block_Alloc( tk->i_frame_size );
553 tk->i_frame_size = 0;
557 tk->p_frame->i_dts = i_dts;
558 tk->p_frame->i_pts = VLC_TS_INVALID;
560 tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
562 i_dts = VLC_TS_INVALID;
568 i_frame_data = i_len;
572 i_frame_data = i_data;
573 if( i_type == 2 && i_frame_data > i_pos )
574 i_frame_data = i_pos;
576 if( i_frame_data > i_data )
581 if( tk->i_frame_slice > tk->i_frame_slice_count || !tk->p_frame )
585 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
586 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 4], tk->i_frame_pos - (2*4 * tk->i_frame_slice_count + 1) );
588 if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
591 memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
592 RVoid( &p_data, &i_data, i_frame_data );
593 tk->i_frame_pos += i_frame_data;
595 if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
597 /* Fix the buffer once the real number of slice is known */
598 tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
599 tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );
601 memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice ],
602 &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
603 tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );
606 CheckPcr( p_demux, tk, tk->p_frame->i_dts );
607 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
609 tk->i_frame_size = 0;
615 static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
617 demux_sys_t *p_sys = p_demux->p_sys;
618 uint8_t *p_buf = p_sys->buffer;
621 if( (i_flags & 2) || p_sys->b_seek )
624 tk->i_out_subpacket = 0;
625 p_sys->b_seek = false;
628 if( tk->fmt.i_codec == VLC_CODEC_COOK ||
629 tk->fmt.i_codec == VLC_CODEC_ATRAC3 )
631 const int i_num = tk->i_frame_size / tk->i_subpacket_size;
632 const int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
634 for( int i = 0; i < i_num; i++ )
636 int i_index = tk->i_subpacket_h * i +
637 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
638 if( i_index >= tk->i_subpackets )
641 block_t *p_block = block_Alloc( tk->i_subpacket_size );
644 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
647 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
649 p_block->i_pts = VLC_TS_INVALID;
651 p_buf += tk->i_subpacket_size;
653 if( tk->p_subpackets[i_index] != NULL )
655 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
656 block_Release( tk->p_subpackets[i_index] );
659 tk->p_subpackets[i_index] = p_block;
660 if( tk->i_subpacket == 0 )
661 tk->p_subpackets_timecode[0] = i_pts;
667 const int y = tk->i_subpacket / (tk->i_subpacket_h / 2);
668 assert( tk->fmt.i_codec == VLC_CODEC_RA_288 );
670 for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
672 int i_index = (i * 2 * tk->i_frame_size / tk->i_coded_frame_size) + y;
673 if( i_index >= tk->i_subpackets )
676 block_t *p_block = block_Alloc( tk->i_coded_frame_size);
679 if( &p_buf[tk->i_coded_frame_size] > &p_sys->buffer[p_sys->i_buffer] )
682 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
684 p_block->i_pts = i_index == 0 ? i_pts : VLC_TS_INVALID;
686 p_buf += tk->i_coded_frame_size;
688 if( tk->p_subpackets[i_index] != NULL )
690 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
691 block_Release( tk->p_subpackets[i_index] );
694 tk->p_subpackets[i_index] = p_block;
699 while( tk->i_out_subpacket != tk->i_subpackets &&
700 tk->p_subpackets[tk->i_out_subpacket] )
702 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
703 tk->p_subpackets[tk->i_out_subpacket] = NULL;
705 if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
708 p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
710 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
712 tk->i_out_subpacket++;
714 CheckPcr( p_demux, tk, p_block->i_pts );
715 es_out_Send( p_demux->out, tk->p_es, p_block );
718 if( tk->i_subpacket == tk->i_subpackets &&
719 tk->i_out_subpacket != tk->i_subpackets )
721 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
722 "this shouldn't happen" );
725 if( tk->i_subpacket == tk->i_subpackets )
728 tk->i_out_subpacket = 0;
732 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
734 demux_sys_t *p_sys = p_demux->p_sys;
736 if( p_sys->i_buffer < 2 )
739 unsigned i_sub = (p_sys->buffer[1] >> 4)&0x0f;
740 if( p_sys->i_buffer < 2+2*i_sub )
743 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
745 for( unsigned i = 0; i < i_sub; i++ )
747 const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
748 block_t *p_block = block_Alloc( i_sub_size );
752 if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
755 memcpy( p_block->p_buffer, p_sub, i_sub_size );
759 p_block->i_pts = i == 0 ? i_pts : VLC_TS_INVALID;
761 CheckPcr( p_demux, tk, p_block->i_pts );
762 es_out_Send( p_demux->out, tk->p_es, p_block );
765 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
767 demux_sys_t *p_sys = p_demux->p_sys;
769 if( p_sys->i_buffer <= 0 )
772 block_t *p_block = block_Alloc( p_sys->i_buffer );
776 if( tk->fmt.i_codec == VLC_CODEC_A52 )
778 uint8_t *p_src = p_sys->buffer;
779 uint8_t *p_dst = p_block->p_buffer;
782 while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
792 memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
795 p_block->i_pts = i_pts;
797 CheckPcr( p_demux, tk, p_block->i_pts );
798 es_out_Send( p_demux->out, tk->p_es, p_block );
801 // Sipr packet re-ordering code and index table borrowed from
802 // the MPlayer Realmedia demuxer.
803 static const uint8_t sipr_swap_index_table[38][2] = {
804 { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
805 { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
806 { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
807 { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
808 { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
809 { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
810 { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
811 { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
812 { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
813 { 67, 83 }, { 77, 80 }
816 static void SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize)
818 int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
820 for (n = 0; n < 38; n++) {
822 int i = bs * sipr_swap_index_table[n][0];
823 int o = bs * sipr_swap_index_table[n][1];
825 /* swap 4 bit-nibbles of block 'i' with 'o' */
826 for (j = 0; j < bs; j++, i++, o++) {
827 int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
828 y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
830 buf[o >> 1] = (x << (4 * (o & 1))) |
831 (buf[o >> 1] & (0xF << (4 * !(o & 1))));
832 buf[i >> 1] = (y << (4 * (i & 1))) |
833 (buf[i >> 1] & (0xF << (4 * !(i & 1))));
838 static void DemuxAudioSipr( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
840 demux_sys_t *p_sys = p_demux->p_sys;
841 block_t *p_block = tk->p_sipr_packet;
843 if( p_sys->i_buffer < tk->i_frame_size
844 || tk->i_sipr_subpacket_count >= tk->i_subpacket_h )
849 p_block = block_Alloc( tk->i_frame_size * tk->i_subpacket_h );
852 tk->p_sipr_packet = p_block;
854 memcpy( p_block->p_buffer + tk->i_sipr_subpacket_count * tk->i_frame_size,
855 p_sys->buffer, tk->i_frame_size );
856 if (!tk->i_sipr_subpacket_count)
859 p_block->i_pts = i_pts;
862 if( ++tk->i_sipr_subpacket_count < tk->i_subpacket_h )
865 SiprPacketReorder(p_block->p_buffer, tk->i_subpacket_h, tk->i_frame_size);
866 CheckPcr( p_demux, tk, p_block->i_pts );
867 es_out_Send( p_demux->out, tk->p_es, p_block );
868 tk->i_sipr_subpacket_count = 0;
869 tk->p_sipr_packet = NULL;
872 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
874 switch( tk->fmt.i_codec )
877 case VLC_CODEC_ATRAC3:
878 case VLC_CODEC_RA_288:
879 DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
882 DemuxAudioMethod2( p_demux, tk, i_pts );
885 DemuxAudioSipr( p_demux, tk, i_pts );
888 DemuxAudioMethod3( p_demux, tk, i_pts );
893 /*****************************************************************************
894 * Helpers: seek/control
895 *****************************************************************************/
896 static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
898 demux_sys_t *p_sys = p_demux->p_sys;
900 p_sys->b_seek = true;
901 p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
902 for( int i = 0; i < p_sys->i_track; i++ )
903 p_sys->track[i]->i_last_dts = 0;
904 return stream_Seek( p_demux->s, p_index->i_file_offset );
906 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
908 demux_sys_t *p_sys = p_demux->p_sys;
909 real_index_t *p_index = p_sys->p_index;
911 while( p_index->i_file_offset != 0 )
913 if( p_index->i_time_offset * INT64_C(1000) > i_time )
915 if( p_index != p_sys->p_index )
921 if( p_index->i_file_offset == 0 )
923 return ControlGoToIndex( p_demux, p_index );
925 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
927 demux_sys_t *p_sys = p_demux->p_sys;
928 real_index_t *p_index = p_sys->p_index;
930 while( p_index->i_file_offset != 0 )
932 if( p_index->i_file_offset > i_bytes )
934 if( p_index != p_sys->p_index )
940 if( p_index->i_file_offset == 0 )
942 return ControlGoToIndex( p_demux, p_index );
945 /*****************************************************************************
946 * Helpers: header reading
947 *****************************************************************************/
950 * This function will read a pascal string with size stored in 2 bytes from
953 * FIXME what is the right charset ?
955 static char *StreamReadString2( stream_t *s )
959 if( stream_Read( s, p_tmp, 2 ) < 2 )
962 const int i_length = GetWBE( p_tmp );
966 char *psz_string = xcalloc( 1, i_length + 1 );
968 stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
971 EnsureUTF8( psz_string );
976 * This function will read a pascal string with size stored in 1 byte from a
979 * FIXME what is the right charset ?
981 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
983 const uint8_t *p_data = *pp_data;
984 int i_data = *pi_data;
986 char *psz_string = NULL;
991 int i_length = *p_data++; i_data--;
992 if( i_length > i_data )
997 psz_string = strndup( (const char*)p_data, i_length );
999 EnsureUTF8( psz_string );
1012 * This function parses(skip) the .RMF identification chunk.
1014 static int HeaderRMF( demux_t *p_demux )
1016 uint8_t p_buffer[8];
1018 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1019 return VLC_EGENERIC;
1021 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
1022 GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
1026 * This function parses the PROP properties chunk.
1028 static int HeaderPROP( demux_t *p_demux )
1030 demux_sys_t *p_sys = p_demux->p_sys;
1032 uint8_t p_buffer[40];
1035 if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
1036 return VLC_EGENERIC;
1038 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1039 GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
1040 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
1041 GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
1042 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
1043 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
1044 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
1045 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
1046 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
1047 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
1049 /* set the duration for export in control */
1050 p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
1052 p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
1054 i_flags = GetWBE(&p_buffer[38]);
1055 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
1057 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
1058 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
1059 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
1064 * This functions parses the CONT commentairs chunk.
1066 static int HeaderCONT( demux_t *p_demux )
1068 demux_sys_t *p_sys = p_demux->p_sys;
1071 p_sys->psz_title = StreamReadString2( p_demux->s );
1072 if( p_sys->psz_title )
1073 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1076 p_sys->psz_artist = StreamReadString2( p_demux->s );
1077 if( p_sys->psz_artist )
1078 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1081 p_sys->psz_copyright = StreamReadString2( p_demux->s );
1082 if( p_sys->psz_copyright )
1083 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1086 p_sys->psz_description = StreamReadString2( p_demux->s );
1087 if( p_sys->psz_description )
1088 msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
1093 * This function parses the MDPR (Media properties) chunk.
1095 static int HeaderMDPR( demux_t *p_demux )
1097 uint8_t p_buffer[30];
1099 if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1100 return VLC_EGENERIC;
1102 const int i_num = GetWBE( &p_buffer[0] );
1103 msg_Dbg( p_demux, " - id=0x%x", i_num );
1104 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1105 GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1106 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1107 GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1108 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
1109 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
1110 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
1113 const uint8_t *p_peek;
1114 int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1115 int i_peek = i_peek_org;
1117 return VLC_EGENERIC;
1119 char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1122 msg_Dbg( p_demux, " - name=`%s'", psz_name );
1125 char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1128 msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
1131 const int i_skip = i_peek_org - i_peek;
1132 if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1133 return VLC_EGENERIC;
1136 if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1137 return VLC_EGENERIC;
1139 const uint32_t i_size = GetDWBE( p_buffer );
1142 CodecParse( p_demux, i_size, i_num );
1143 unsigned size = stream_Read( p_demux->s, NULL, i_size );
1145 return VLC_EGENERIC;
1150 * This function parses DATA chunk (it contains the actual movie data).
1152 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1154 demux_sys_t *p_sys = p_demux->p_sys;
1155 uint8_t p_buffer[8];
1157 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1158 return VLC_EGENERIC;
1160 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1161 p_sys->i_data_size = i_size;
1162 p_sys->i_data_packets_count = GetDWBE( p_buffer );
1163 p_sys->i_data_packets = 0;
1164 p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1166 msg_Dbg( p_demux, " - packets count=%d next=%u",
1167 p_sys->i_data_packets_count,
1168 (unsigned int)p_sys->i_data_offset_next );
1172 * This function parses the INDX (movie index chunk).
1173 * It is optional but seeking without it is ... hard.
1175 static void HeaderINDX( demux_t *p_demux )
1177 demux_sys_t *p_sys = p_demux->p_sys;
1180 uint32_t i_index_count;
1182 if( p_sys->i_index_offset == 0 )
1185 stream_Seek( p_demux->s, p_sys->i_index_offset );
1187 if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
1190 const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
1191 const uint32_t i_size = GetDWBE( &buffer[4] );
1192 int i_version = GetWBE( &buffer[8] );
1194 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
1195 (char*)&i_id, i_size, i_version );
1197 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
1200 i_index_count = GetDWBE( &buffer[10] );
1202 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
1204 if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
1207 if( GetDWBE( &buffer[16] ) > 0 )
1208 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
1209 GetDWBE( &buffer[16] ) );
1211 /* One extra entry is allocated (that MUST be set to 0) to identify the
1213 * TODO add a clean entry count (easier to build index on the fly) */
1214 p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
1215 if( !p_sys->p_index )
1218 for( unsigned int i = 0; i < i_index_count; i++ )
1220 uint8_t p_entry[14];
1222 if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
1225 if( GetWBE( &p_entry[0] ) != 0 )
1227 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
1228 GetWBE( &p_entry[0] ) );
1232 real_index_t *p_idx = &p_sys->p_index[i];
1234 p_idx->i_time_offset = GetDWBE( &p_entry[2] );
1235 p_idx->i_file_offset = GetDWBE( &p_entry[6] );
1236 p_idx->i_frame_index = GetDWBE( &p_entry[10] );
1240 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
1241 p_idx->i_time_offset,
1242 p_idx->i_file_offset,
1243 p_idx->i_frame_index );
1250 * This function parses the complete RM headers and move the
1251 * stream pointer to the data to be read.
1253 static int HeaderRead( demux_t *p_demux )
1255 demux_sys_t *p_sys = p_demux->p_sys;
1259 const int64_t i_stream_position = stream_Tell( p_demux->s );
1260 uint8_t header[100]; /* FIXME */
1262 /* Read the header */
1263 if( stream_Read( p_demux->s, header, 10 ) < 10 )
1264 return VLC_EGENERIC;
1266 const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1267 header[2], header[3] );
1268 const uint32_t i_size = GetDWBE( &header[4] );
1269 const int i_version = GetWBE( &header[8] );
1271 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1272 (char*)&i_id, i_size, i_version );
1275 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1277 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1278 return VLC_EGENERIC;
1284 case VLC_FOURCC('.','R','M','F'):
1285 i_ret = HeaderRMF( p_demux );
1287 case VLC_FOURCC('P','R','O','P'):
1288 i_ret = HeaderPROP( p_demux );
1290 case VLC_FOURCC('C','O','N','T'):
1291 i_ret = HeaderCONT( p_demux );
1293 case VLC_FOURCC('M','D','P','R'):
1294 i_ret = HeaderMDPR( p_demux );
1296 case VLC_FOURCC('D','A','T','A'):
1297 i_ret = HeaderDATA( p_demux, i_size );
1301 msg_Dbg( p_demux, "unknown chunk" );
1302 i_ret = VLC_SUCCESS;
1308 if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1311 /* Skip unread data */
1312 const int64_t i_stream_current = stream_Tell( p_demux->s );
1313 const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1315 if( i_stream_skip > 0 )
1317 if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1318 return VLC_EGENERIC;
1320 else if( i_stream_skip < 0 )
1322 return VLC_EGENERIC;
1326 /* read index if possible */
1327 if( p_sys->i_index_offset > 0 )
1329 const int64_t i_position = stream_Tell( p_demux->s );
1331 HeaderINDX( p_demux );
1333 if( stream_Seek( p_demux->s, i_position ) )
1334 return VLC_EGENERIC;
1339 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1341 demux_sys_t *p_sys = p_demux->p_sys;
1344 p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1345 if( p_sys->psz_title )
1346 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1349 p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1350 if( p_sys->psz_artist )
1351 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1354 p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1355 if( p_sys->psz_copyright )
1356 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1359 p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1360 if( p_sys->psz_description )
1361 msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
1364 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1366 demux_sys_t *p_sys = p_demux->p_sys;
1369 return VLC_EGENERIC;
1373 es_format_Init( &fmt, VIDEO_ES,
1374 VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1375 fmt.video.i_width = GetWBE( &p_data[12] );
1376 fmt.video.i_height= GetWBE( &p_data[14] );
1377 fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1378 fmt.video.i_frame_rate_base = 1 << 16;
1380 fmt.i_extra = i_data - 26;
1381 fmt.p_extra = malloc( fmt.i_extra );
1385 memcpy( fmt.p_extra, &p_data[26], fmt.i_extra );
1387 //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
1390 switch( GetDWBE( &p_data[30] ) )
1394 fmt.i_codec = VLC_CODEC_RV13;
1400 fmt.i_codec = VLC_CODEC_RV20;
1403 fmt.i_codec = VLC_CODEC_RV30;
1406 fmt.i_codec = VLC_CODEC_RV40;
1409 msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
1410 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1412 real_track_t *tk = malloc( sizeof( *tk ) );
1415 es_format_Clean( &fmt );
1418 tk->i_out_subpacket = 0;
1419 tk->i_subpacket = 0;
1420 tk->i_subpackets = 0;
1421 tk->p_subpackets = NULL;
1422 tk->p_subpackets_timecode = NULL;
1425 tk->i_frame_num = -1;
1426 tk->i_frame_size = 0;
1429 tk->p_sipr_packet = NULL;
1430 tk->p_es = es_out_Add( p_demux->out, &fmt );
1432 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1435 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1437 demux_sys_t *p_sys = p_demux->p_sys;
1441 return VLC_EGENERIC;
1444 int i_coded_frame_size = 0;
1445 int i_subpacket_h = 0;
1446 int i_frame_size = 0;
1447 int i_subpacket_size = 0;
1449 int i_version = GetWBE( &p_data[4] );
1450 int i_extra_codec = 0;
1452 msg_Dbg( p_demux, " - audio version=%d", i_version );
1454 es_format_Init( &fmt, AUDIO_ES, 0 );
1456 RVoid( &p_data, &i_data, 6 ); /* 4 + version */
1457 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1459 RVoid( &p_data, &i_data, 2 + 10 + 4 );
1462 CodecMetaRead( p_demux, &p_data, &i_data );
1464 RVoid( &p_data, &i_data, 1 + 1 );
1466 memcpy( &fmt.i_codec, p_data, 4 );
1467 RVoid( &p_data, &i_data, 4 );
1469 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1470 fmt.audio.i_rate = 8000;
1472 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1473 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1475 else /* RMF version 4/5 or .ra version 4 */
1477 RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1478 i_flavor = R16( &p_data, &i_data );
1479 i_coded_frame_size = R32( &p_data, &i_data );
1480 RVoid( &p_data, &i_data, 4 + 4 + 4 );
1481 i_subpacket_h = R16( &p_data, &i_data );
1482 i_frame_size = R16( &p_data, &i_data );
1483 i_subpacket_size = R16( &p_data, &i_data );
1484 if( !i_frame_size || !i_coded_frame_size )
1486 es_format_Clean( &fmt );
1487 return VLC_EGENERIC;
1490 RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1492 fmt.audio.i_rate = R16( &p_data, &i_data );
1493 RVoid( &p_data, &i_data, 2 );
1494 fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1495 fmt.audio.i_channels = R16( &p_data, &i_data );
1496 fmt.audio.i_blockalign = i_frame_size;
1498 if( i_version == 5 )
1502 memcpy( p_genr, &p_data[0], 4 );
1503 memcpy( &fmt.i_codec, &p_data[4], 4 );
1505 RVoid( &p_data, &i_data, 8 );
1507 else /* version 4 */
1510 RVoid( &p_data, &i_data, 1 + *p_data );
1511 if( i_data >= 1 + 4 )
1512 memcpy( &fmt.i_codec, &p_data[1], 4 );
1514 RVoid( &p_data, &i_data, 1 + *p_data );
1517 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1518 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1520 RVoid( &p_data, &i_data, 3 );
1522 if( p_sys->b_real_audio )
1524 CodecMetaRead( p_demux, &p_data, &i_data );
1528 if( i_version == 5 )
1529 RVoid( &p_data, &i_data, 1 );
1530 i_extra_codec = R32( &p_data, &i_data );
1534 switch( fmt.i_codec )
1536 case VLC_FOURCC('l','p','c','J'):
1537 case VLC_FOURCC('1','4','_','4'):
1538 fmt.i_codec = VLC_CODEC_RA_144;
1539 fmt.audio.i_blockalign = 0x14 ;
1542 case VLC_FOURCC('2','8','_','8'):
1543 if( i_coded_frame_size <= 0 )
1545 es_format_Clean( &fmt );
1546 return VLC_EGENERIC;
1548 fmt.i_codec = VLC_CODEC_RA_288;
1549 fmt.audio.i_blockalign = i_coded_frame_size;
1552 case VLC_FOURCC( 'a','5','2',' ' ):
1553 case VLC_FOURCC( 'd','n','e','t' ):
1554 fmt.i_codec = VLC_CODEC_A52;
1557 case VLC_FOURCC( 'r','a','a','c' ):
1558 case VLC_FOURCC( 'r','a','c','p' ):
1559 fmt.i_codec = VLC_CODEC_MP4A;
1561 if( i_extra_codec > 0 )
1564 RVoid( &p_data, &i_data, 1 );
1566 if( i_extra_codec > 0 )
1568 fmt.p_extra = malloc( i_extra_codec );
1569 if( !fmt.p_extra || i_extra_codec > i_data )
1571 free( fmt.p_extra );
1575 fmt.i_extra = i_extra_codec;
1576 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1580 case VLC_CODEC_SIPR:
1581 fmt.i_codec = VLC_CODEC_SIPR;
1584 msg_Dbg( p_demux, " - unsupported sipr flavorc=%i", i_flavor );
1585 es_format_Clean( &fmt );
1586 return VLC_EGENERIC;
1589 i_subpacket_size = i_subpacket_size_sipr[i_flavor];
1590 // The libavcodec sipr decoder requires stream bitrate
1591 // to be set during initialization so that the correct mode
1593 fmt.i_bitrate = fmt.audio.i_rate;
1594 msg_Dbg( p_demux, " - sipr flavor=%i", i_flavor );
1596 case VLC_CODEC_COOK:
1597 case VLC_CODEC_ATRAC3:
1598 if( i_subpacket_size <= 0 || i_frame_size / i_subpacket_size <= 0 )
1600 es_format_Clean( &fmt );
1601 return VLC_EGENERIC;
1603 if( !memcmp( p_genr, "genr", 4 ) )
1604 fmt.audio.i_blockalign = i_subpacket_size;
1606 fmt.audio.i_blockalign = i_coded_frame_size;
1608 if( i_extra_codec > 0 )
1610 fmt.p_extra = malloc( i_extra_codec );
1611 if( !fmt.p_extra || i_extra_codec > i_data )
1613 free( fmt.p_extra );
1617 fmt.i_extra = i_extra_codec;
1618 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1623 case VLC_FOURCC('r','a','l','f'):
1625 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1626 (char*)&fmt.i_codec );
1627 es_format_Clean( &fmt );
1628 return VLC_EGENERIC;
1631 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1634 real_track_t *tk = malloc( sizeof( *tk ) );
1637 es_format_Clean( &fmt );
1642 tk->i_frame_size = 0;
1645 tk->i_subpacket_h = i_subpacket_h;
1646 tk->i_subpacket_size = i_subpacket_size;
1647 tk->i_coded_frame_size = i_coded_frame_size;
1648 tk->i_frame_size = i_frame_size;
1650 tk->i_out_subpacket = 0;
1651 tk->i_subpacket = 0;
1652 tk->i_subpackets = 0;
1653 tk->p_subpackets = NULL;
1654 tk->p_subpackets_timecode = NULL;
1656 tk->p_sipr_packet = NULL;
1657 tk->i_sipr_subpacket_count = 0;
1659 if( fmt.i_codec == VLC_CODEC_COOK ||
1660 fmt.i_codec == VLC_CODEC_ATRAC3 )
1663 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1665 xcalloc( tk->i_subpackets, sizeof(block_t *) );
1666 tk->p_subpackets_timecode =
1667 xcalloc( tk->i_subpackets , sizeof( int64_t ) );
1669 else if( fmt.i_codec == VLC_CODEC_RA_288 )
1672 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1674 xcalloc( tk->i_subpackets, sizeof(block_t *) );
1675 tk->p_subpackets_timecode =
1676 xcalloc( tk->i_subpackets , sizeof( int64_t ) );
1679 /* Check if the calloc went correctly */
1680 if( tk->i_subpacket > 0 && ( !tk->p_subpackets || !tk->p_subpackets_timecode ) )
1682 free( tk->p_subpackets_timecode );
1683 free( tk->p_subpackets );
1685 msg_Err( p_demux, "Can't alloc subpacket" );
1686 return VLC_EGENERIC;
1690 tk->p_es = es_out_Add( p_demux->out, &fmt );
1692 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1698 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1700 const uint8_t *p_peek;
1702 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1703 if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1704 return VLC_EGENERIC;
1706 if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1708 return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1710 else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1712 return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1717 /*****************************************************************************
1718 * Helpers: memory buffer fct.
1719 *****************************************************************************/
1720 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1722 if( i_size > *pi_data )
1728 #define RX(name, type, size, code ) \
1729 static type name( const uint8_t **pp_data, int *pi_data ) { \
1730 if( *pi_data < (size) ) \
1733 RVoid( pp_data, pi_data, size ); \
1736 RX(R8, uint8_t, 1, **pp_data )
1737 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1738 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1739 static int RLength( const uint8_t **pp_data, int *pi_data )
1741 const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1744 return (v0 << 16) | R16( pp_data, pi_data );