Commit c63f0500 authored by Laurent Aimar's avatar Laurent Aimar

Big clean up/rewrite of the real demuxer.

It fixes:
 - a bunch of array overflows
 - a bunch of non checked malloc.
 - pcr value
 - video frame gathering.
 - seeking.
parent 073a4b90
...@@ -85,27 +85,32 @@ typedef struct ...@@ -85,27 +85,32 @@ typedef struct
es_out_id_t *p_es; es_out_id_t *p_es;
int i_frame; int i_frame_size;
int i_frame_num;
int i_frame_pos;
int i_frame_slice;
int i_frame_slice_count;
block_t *p_frame; block_t *p_frame;
int i_subpacket_h; int i_subpacket_h;
int i_subpacket_size; int i_subpacket_size;
int i_coded_frame_size; int i_coded_frame_size;
int i_frame_size;
int i_subpacket; int i_subpacket;
int i_subpackets; int i_subpackets;
block_t **p_subpackets; block_t **p_subpackets;
int64_t *p_subpackets_timecode; mtime_t *p_subpackets_timecode;
int i_out_subpacket; int i_out_subpacket;
mtime_t i_last_dts;
} real_track_t; } real_track_t;
typedef struct typedef struct
{ {
uint32_t file_offset; uint32_t i_file_offset;
uint32_t time_offset; uint32_t i_time_offset;
uint32_t frame_index; uint32_t i_frame_index;
} rm_index_t; } rm_index_t;
struct demux_sys_t struct demux_sys_t
...@@ -116,10 +121,9 @@ struct demux_sys_t ...@@ -116,10 +121,9 @@ struct demux_sys_t
uint32_t i_data_packets; uint32_t i_data_packets;
int64_t i_data_offset_next; int64_t i_data_offset_next;
bool b_is_real_audio; bool b_real_audio;
int i_our_duration; int64_t i_our_duration;
int i_mux_rate;
char* psz_title; char* psz_title;
char* psz_artist; char* psz_artist;
...@@ -129,13 +133,14 @@ struct demux_sys_t ...@@ -129,13 +133,14 @@ struct demux_sys_t
int i_track; int i_track;
real_track_t **track; real_track_t **track;
int i_buffer;
uint8_t buffer[65536]; uint8_t buffer[65536];
int64_t i_pcr; int64_t i_pcr;
vlc_meta_t *p_meta; vlc_meta_t *p_meta;
int64_t i_index_offset; int64_t i_index_offset;
int b_seek; bool b_seek;
rm_index_t *p_index; rm_index_t *p_index;
}; };
...@@ -143,8 +148,16 @@ static int Demux( demux_t *p_demux ); ...@@ -143,8 +148,16 @@ static int Demux( demux_t *p_demux );
static int Control( demux_t *p_demux, int i_query, va_list args ); static int Control( demux_t *p_demux, int i_query, va_list args );
static int HeaderRead( demux_t *p_demux ); static int HeaderRead( demux_t *p_demux );
static const uint8_t * MetaRead( demux_t *p_demux, const uint8_t *p_peek ); static int CodecParse( demux_t *p_demux, int i_len, int i_num );
static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
static char *StreamReadString2( stream_t *s );
static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data );
static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
static int RLength( const uint8_t **pp_data, int *pi_data );
static uint8_t R8( const uint8_t **pp_data, int *pi_data );
static uint16_t R16( const uint8_t **pp_data, int *pi_data );
static uint32_t R32( const uint8_t **pp_data, int *pi_data );
/***************************************************************************** /*****************************************************************************
* Open * Open
...@@ -155,42 +168,44 @@ static int Open( vlc_object_t *p_this ) ...@@ -155,42 +168,44 @@ static int Open( vlc_object_t *p_this )
demux_sys_t *p_sys; demux_sys_t *p_sys;
const uint8_t *p_peek; const uint8_t *p_peek;
bool b_is_real_audio = false; bool b_real_audio = false;
if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC; if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
return VLC_EGENERIC;
/* Real Audio */ /* Real Audio */
if( !memcmp( p_peek, ".ra", 3 ) ) if( !memcmp( p_peek, ".ra", 3 ) )
{ {
msg_Err( p_demux, ".ra files unsuported" ); msg_Err( p_demux, ".ra files unsuported" );
b_is_real_audio = true; b_real_audio = true;
} }
/* Real Media Format */ /* Real Media Format */
else if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC; else if( memcmp( p_peek, ".RMF", 4 ) )
{
return VLC_EGENERIC;
}
/* Fill p_demux field */ /* Fill p_demux field */
p_demux->pf_demux = Demux; p_demux->pf_demux = Demux;
p_demux->pf_control = Control; p_demux->pf_control = Control;
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
if( p_sys == NULL ) if( !p_sys )
return VLC_ENOMEM; return VLC_ENOMEM;
memset( p_sys, 0, sizeof( demux_sys_t ) );
p_sys->i_data_offset = 0; p_sys->i_data_offset = 0;
p_sys->i_track = 0; p_sys->i_track = 0;
p_sys->track = NULL; p_sys->track = NULL;
p_sys->i_pcr = 1; p_sys->i_pcr = 0;
p_sys->b_seek = 0; p_sys->b_seek = false;
p_sys->b_is_real_audio = b_is_real_audio; p_sys->b_real_audio = b_real_audio;
/* Parse the headers */ /* Parse the headers */
/* Real Audio files */ /* Real Audio files */
if( b_is_real_audio ) if( b_real_audio )
{ {
ReadCodecSpecificData( p_demux, 32, 0 ); /* At least 32 */ CodecParse( p_demux, 32, 0 ); /* At least 32 */
return VLC_EGENERIC; /* We don't know how to read return VLC_EGENERIC; /* We don't know how to read
correctly the data yet */ correctly the data yet */
} }
...@@ -204,15 +219,12 @@ static int Open( vlc_object_t *p_this ) ...@@ -204,15 +219,12 @@ static int Open( vlc_object_t *p_this )
real_track_t *tk = p_sys->track[i]; real_track_t *tk = p_sys->track[i];
if( tk->p_es ) if( tk->p_es )
{
es_out_Del( p_demux->out, tk->p_es ); es_out_Del( p_demux->out, tk->p_es );
}
free( tk ); free( tk );
} }
if( p_sys->i_track > 0 ) if( p_sys->i_track > 0 )
{
free( p_sys->track ); free( p_sys->track );
}
free( p_sys ); free( p_sys );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -227,17 +239,17 @@ static void Close( vlc_object_t *p_this ) ...@@ -227,17 +239,17 @@ static void Close( vlc_object_t *p_this )
{ {
demux_t *p_demux = (demux_t*)p_this; demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys; demux_sys_t *p_sys = p_demux->p_sys;
int i;
for( i = 0; i < p_sys->i_track; i++ ) for( int i = 0; i < p_sys->i_track; i++ )
{ {
real_track_t *tk = p_sys->track[i]; real_track_t *tk = p_sys->track[i];
int j = tk->i_subpackets;
if( tk->p_frame ) block_Release( tk->p_frame );
es_format_Clean( &tk->fmt ); es_format_Clean( &tk->fmt );
while( j-- ) if( tk->p_frame )
block_Release( tk->p_frame );
for( int j = 0; j < tk->i_subpackets; j++ )
{ {
if( tk->p_subpackets[ j ] ) if( tk->p_subpackets[ j ] )
block_Release( tk->p_subpackets[ j ] ); block_Release( tk->p_subpackets[ j ] );
...@@ -247,9 +259,10 @@ static void Close( vlc_object_t *p_this ) ...@@ -247,9 +259,10 @@ static void Close( vlc_object_t *p_this )
free( tk->p_subpackets ); free( tk->p_subpackets );
free( tk->p_subpackets_timecode ); free( tk->p_subpackets_timecode );
} }
free( tk ); free( tk );
} }
if( p_sys->i_track > 0 )
free( p_sys->track );
free( p_sys->psz_title ); free( p_sys->psz_title );
free( p_sys->psz_artist ); free( p_sys->psz_artist );
...@@ -257,7 +270,6 @@ static void Close( vlc_object_t *p_this ) ...@@ -257,7 +270,6 @@ static void Close( vlc_object_t *p_this )
free( p_sys->psz_description ); free( p_sys->psz_description );
free( p_sys->p_index ); free( p_sys->p_index );
if( p_sys->i_track > 0 ) free( p_sys->track );
free( p_sys ); free( p_sys );
} }
...@@ -265,386 +277,236 @@ static void Close( vlc_object_t *p_this ) ...@@ -265,386 +277,236 @@ static void Close( vlc_object_t *p_this )
/***************************************************************************** /*****************************************************************************
* Demux: * Demux:
*****************************************************************************/ *****************************************************************************/
static int Demux( demux_t *p_demux ) static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
{ {
demux_sys_t *p_sys = p_demux->p_sys; demux_sys_t *p_sys = p_demux->p_sys;
uint8_t header[18];
int i_id, i_flags, i;
unsigned int i_size;
int64_t i_pts;
real_track_t *tk = NULL;
bool b_selected;
if( p_sys->i_data_packets >= p_sys->i_data_packets_count && if( i_dts > 0 )
p_sys->i_data_packets_count ) tk->i_last_dts = i_dts;
{
if( stream_Read( p_demux->s, header, 18 ) < 18 )
{
return 0;
}
if( strncmp( (char *)header, "DATA", 4 ) )
{
return 0;
}
p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
p_sys->i_data_size = GetDWBE( &header[4] );
p_sys->i_data_packets_count = GetDWBE( &header[10] );
p_sys->i_data_packets = 0;
p_sys->i_data_offset_next = GetDWBE( &header[14] );
msg_Dbg( p_demux, "entering new DATA packets=%d next=%u", if( p_sys->i_pcr > 0 || i_dts <= 0 )
p_sys->i_data_packets_count, return;
(uint32_t)p_sys->i_data_offset_next );
}
if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
// int i_version = GetWBE( &header[0] );
i_size = GetWBE( &header[2] ) - 12;
i_id = GetWBE( &header[4] );
i_pts = 1000 * GetDWBE( &header[6] );
i_pts += 1000; /* Avoid 0 pts */
i_flags= header[11]; /* flags 0x02 -> keyframe */
msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
p_sys->i_data_packets++;
if( i_size == 0 )
{
msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
return 1;
}
if( i_size > sizeof(p_sys->buffer) )
{
msg_Err( p_demux, "Got a size to read bigger than our buffer. (Invalid format?)" );
return 1;
}
stream_Read( p_demux->s, p_sys->buffer, i_size ); p_sys->i_pcr = i_dts;
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
}
for( i = 0; i < p_sys->i_track; i++ ) static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
{ {
if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i]; demux_sys_t *p_sys = p_demux->p_sys;
}
if( tk == NULL ) const uint8_t *p_data = p_sys->buffer;
{ int i_data = p_sys->i_buffer;
msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
return 1;
}
es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
if( tk->fmt.i_cat == VIDEO_ES && b_selected ) while( i_data > 1 )
{ {
uint8_t *p = p_sys->buffer; uint8_t i_hdr = R8( &p_data, &i_data );
uint8_t i_type = i_hdr >> 6;
while( p < &p_sys->buffer[i_size - 2] ) uint8_t i_seq;
{ int i_len;
uint8_t h = *p++; int i_pos;
int i_len = 0; int i_frame_num;
int i_copy;
int i_subseq = 0;
int i_seqnum = 0;
int i_offset = 0;
if( (h&0xc0) == 0x40 ) if( i_type == 1 )
{
/* Short header */
p++;
i_len = &p_sys->buffer[i_size] - p;
}
else
{ {
if( (h&0x40) == 0 ) R8( &p_data, &i_data );
{ i_len = i_data;
i_subseq = (*p++)&0x7f; i_pos = 0;
i_frame_num = -1;
i_seq = 1;
i_hdr &= ~0x3f;
} }
i_len = (p[0] << 8)|p[1]; p += 2; else if( i_type == 3 )
if( (i_len&0xc000) == 0 )
{ {
i_len <<= 16; i_len = RLength( &p_data, &i_data );
i_len |= (p[0] << 8)|p[1]; p += 2; i_pos = RLength( &p_data, &i_data );
i_len &= 0x3fffffff; i_frame_num = R8( &p_data, &i_data );
i_seq = 1;
i_hdr &= ~0x3f;
} }
else else
{ {
i_len &= 0x3fff; assert( i_type == 0 || i_type == 2 );
} i_seq = R8( &p_data, &i_data );
i_len = RLength( &p_data, &i_data );
i_offset = (p[0] << 8)|p[1]; p += 2; i_pos = RLength( &p_data, &i_data );
if( (i_offset&0xc000) == 0 ) i_frame_num = R8( &p_data, &i_data );
{
i_offset <<= 16;
i_offset |= (p[0] << 8)|p[1]; p += 2;
i_offset &= 0x3fffffff;
}
else
{
i_offset &= 0x3fff;
}
i_seqnum = *p++;
} }
i_copy = i_len - i_offset; if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
if( i_copy > &p_sys->buffer[i_size] - p )
{ {
i_copy = &p_sys->buffer[i_size] - p; tk->i_frame_slice = 0;
} tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
else if( i_copy < 0 ) tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
{ tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
break; tk->i_frame_num = i_frame_num;
}
msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d", if( tk->p_frame )
i_len, i_offset, i_copy, i_subseq, i_seqnum ); block_Release( tk->p_frame );
if( (h&0xc0) == 0x80 ) tk->p_frame = block_New( p_demux, tk->i_frame_size );
if( !tk->p_frame )
{ {
/* last fragment -> fixes */ tk->i_frame_slice = 0;
i_copy = i_offset; tk->i_frame_slice_count = 0;
i_offset = i_len - i_copy; return;
msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
i_copy, i_offset );
} }
if( tk->p_frame && tk->p_frame->i_dts = i_dts;
( tk->p_frame->i_dts != i_pts || tk->p_frame->i_pts = 0;
tk->i_frame != i_len ) ) if( i_flags & 0x02 )
{ tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
msg_Dbg( p_demux, "sending size=%zu", tk->p_frame->i_buffer );
es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
tk->i_frame = 0; i_dts = 0;
tk->p_frame = NULL;
} }
if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame ) int i_frame_data;
if( i_type == 3 )
{ {
/* no fragment */ i_frame_data = i_len;
i_len = i_copy;
i_offset = 0;
} }
else
if( tk->p_frame == NULL )
{
msg_Dbg( p_demux, "new frame size=%d", i_len );
tk->i_frame = i_len;
if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
{ {
return -1; i_frame_data = i_data;
if( i_type == 2 && i_frame_data > i_pos )
i_frame_data = i_pos;
} }
memset( &tk->p_frame->p_buffer[8], 0, i_len );
tk->p_frame->i_dts = i_pts;
tk->p_frame->i_pts = i_pts;
((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */ /* */
((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */ tk->i_frame_slice++;
} if( tk->i_frame_slice > tk->i_frame_slice_count )
break;
if( i_offset < tk->i_frame)
{
int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
i_ck, i_offset, i_copy );
((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
}
p += i_copy; /* */
SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
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) );
if( (h&0xc0) != 0x80 ) if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
{
break; break;
}
#if 0 memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
if( tk->p_frame ) RVoid( &p_data, &i_data, i_frame_data );
{ tk->i_frame_pos += i_frame_data;
/* append data */
int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
if( (h&0xc0) == 0x80 ) if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
{ {
/* last fragment */ /* Fix the buffer once the real number of slice is known */
i_copy = i_offset; tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
i_offset = i_len - i_offset; tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );
((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset; memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice ],
memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy ); &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
p += i_copy; tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );
if( p_sys->i_pcr < tk->p_frame->i_dts ) /* Send it */
{ CheckPcr( p_demux, tk, tk->p_frame->i_dts );
p_sys->i_pcr = tk->p_frame->i_dts;
es_out_Control( p_demux->out, ES_OUT_SET_PCR,
(int64_t)p_sys->i_pcr );
}
es_out_Send( p_demux->out, tk->p_es, tk->p_frame ); es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
tk->i_frame = 0; tk->i_frame_size = 0;
tk->p_frame = NULL; tk->p_frame = NULL;
continue;
}
((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
break;
}
if( (h&0xc0) != 0x00 )
{
block_t *p_frame;
/* not fragmented */
if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
{
return -1;
}
p_frame->i_dts = i_pts;
p_frame->i_pts = i_pts;
((uint32_t*)p_frame->p_buffer)[0] = i_copy;
((uint32_t*)p_frame->p_buffer)[1] = 1;
((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
memcpy( &p_frame->p_buffer[8], p, i_copy );
p += i_copy;
if( p_sys->i_pcr < p_frame->i_dts )
{
p_sys->i_pcr = p_frame->i_dts;
es_out_Control( p_demux->out, ES_OUT_SET_PCR,
(int64_t)p_sys->i_pcr );
} }
es_out_Send( p_demux->out, tk->p_es, p_frame );
} }
else }
{
/* First fragment */
tk->i_frame = i_len;
if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
{
return -1;
}
memset( &tk->p_frame->p_buffer[8], 0, i_len );
tk->p_frame->i_dts = i_pts;
tk->p_frame->i_pts = i_pts;
((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */ {
((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset; demux_sys_t *p_sys = p_demux->p_sys;
memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
break;
}
#endif
}
}
else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
{
if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
{
uint8_t *p_buf = p_sys->buffer; uint8_t *p_buf = p_sys->buffer;
int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size); int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
int i_index, i;
/* Sanity check */ /* Sanity check */
if( i_flags & 2 || ( p_sys->b_seek ) ) if( (i_flags & 2) || p_sys->b_seek )
{ {
y = tk->i_subpacket = 0; y = tk->i_subpacket = 0;
tk->i_out_subpacket = 0; tk->i_out_subpacket = 0;
p_sys->b_seek = 0; p_sys->b_seek = false;
} }
if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) || if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' )) tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ) )
{ {
int num = tk->i_frame_size / tk->i_subpacket_size; const int i_num = tk->i_frame_size / tk->i_subpacket_size;
for( i = 0; i < num; i++ ) for( int i = 0; i < i_num; i++ )
{ {
block_t *p_block = block_New( p_demux, tk->i_subpacket_size ); block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
if( !p_block )
return;
if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
return;
memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size ); memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
p_block->i_dts =
p_block->i_pts = 0;
p_buf += tk->i_subpacket_size; p_buf += tk->i_subpacket_size;
i_index = tk->i_subpacket_h * i + int i_index = tk->i_subpacket_h * i +
((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1); ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
if ( tk->p_subpackets[i_index] != NULL ) if( tk->p_subpackets[i_index] != NULL )
{ {
msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index ); msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
free( tk->p_subpackets[i_index] ); block_Release( tk->p_subpackets[i_index] );
} }
tk->p_subpackets[i_index] = p_block; tk->p_subpackets[i_index] = p_block;
if( tk->i_subpacket == 0 )
tk->p_subpackets_timecode[0] = i_pts;
tk->i_subpacket++; tk->i_subpacket++;
p_block->i_dts = p_block->i_pts = 0;
} }
tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
} }
else
{
assert( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) );
if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) || for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
for( i = 0; i < tk->i_subpacket_h / 2; i++ )
{ {
block_t *p_block = block_New( p_demux, tk->i_coded_frame_size); block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
if( !p_block )
return;
if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
return;
int i_index = (i * 2 * tk->i_frame_size) /
tk->i_coded_frame_size + y;
memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size ); memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
p_block->i_dts =
p_block->i_pts = i_index == 0 ? i_pts : 0;
p_buf += tk->i_coded_frame_size; p_buf += tk->i_coded_frame_size;
i_index = (i * 2 * tk->i_frame_size) / if( tk->p_subpackets[i_index] != NULL )
tk->i_coded_frame_size + y; {
msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
block_Release( tk->p_subpackets[i_index] );
}
p_block->i_dts = p_block->i_pts = i_pts;
tk->p_subpackets[i_index] = p_block; tk->p_subpackets[i_index] = p_block;
tk->i_subpacket++; tk->i_subpacket++;
} }
}
while( tk->i_out_subpacket != tk->i_subpackets && while( tk->i_out_subpacket != tk->i_subpackets &&
tk->p_subpackets[tk->i_out_subpacket] ) tk->p_subpackets[tk->i_out_subpacket] )
{ {
/* Set the PCR */
#if 0
if (tk->i_out_subpacket == 0)
{
p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
es_out_Control( p_demux->out, ES_OUT_SET_PCR,
(int64_t)p_sys->i_pcr );
}
block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
tk->p_subpackets[tk->i_out_subpacket] = 0;
if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
#endif
block_t *p_block = tk->p_subpackets[tk->i_out_subpacket]; block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
tk->p_subpackets[tk->i_out_subpacket] = 0; tk->p_subpackets[tk->i_out_subpacket] = 0;
if ( tk->p_subpackets_timecode[tk->i_out_subpacket] ) if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
{ {
p_block->i_dts = p_block->i_pts = p_block->i_dts =
tk->p_subpackets_timecode[tk->i_out_subpacket]; p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
p_sys->i_pcr = p_block->i_dts; tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
es_out_Control( p_demux->out, ES_OUT_SET_PCR,
(int64_t)p_sys->i_pcr );
} }
es_out_Send( p_demux->out, tk->p_es, p_block );
tk->i_out_subpacket++; tk->i_out_subpacket++;
CheckPcr( p_demux, tk, p_block->i_pts );
es_out_Send( p_demux->out, tk->p_es, p_block );
} }
if( tk->i_subpacket == tk->i_subpackets && if( tk->i_subpacket == tk->i_subpackets &&
...@@ -659,80 +521,240 @@ static int Demux( demux_t *p_demux ) ...@@ -659,80 +521,240 @@ static int Demux( demux_t *p_demux )
tk->i_subpacket = 0; tk->i_subpacket = 0;
tk->i_out_subpacket = 0; tk->i_out_subpacket = 0;
} }
} }
else static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
{ {
/* Set PCR */ demux_sys_t *p_sys = p_demux->p_sys;
if( p_sys->i_pcr < i_pts )
{ if( p_sys->i_buffer < 2 )
p_sys->i_pcr = i_pts; return;
es_out_Control( p_demux->out, ES_OUT_SET_PCR,
(int64_t)p_sys->i_pcr );
}
if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
{
int i_sub = (p_sys->buffer[1] >> 4)&0x0f; int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
if( p_sys->i_buffer < 2+2*i_sub )
return;
uint8_t *p_sub = &p_sys->buffer[2+2*i_sub]; uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
int i; for( int i = 0; i < i_sub; i++ )
for( i = 0; i < i_sub; i++ )
{ {
int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]); const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
block_t *p_block = block_New( p_demux, i_sub_size ); block_t *p_block = block_New( p_demux, i_sub_size );
if( p_block ) if( !p_block )
{ break;
if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
break;
memcpy( p_block->p_buffer, p_sub, i_sub_size ); memcpy( p_block->p_buffer, p_sub, i_sub_size );
p_sub += i_sub_size; p_sub += i_sub_size;
p_block->i_dts = p_block->i_dts =
p_block->i_pts = ( i == 0 ? i_pts : 0 ); p_block->i_pts = ( i == 0 ? i_pts : 0 );
CheckPcr( p_demux, tk, p_block->i_pts );
es_out_Send( p_demux->out, tk->p_es, p_block ); es_out_Send( p_demux->out, tk->p_es, p_block );
} }
} }
} static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
else {
{ demux_sys_t *p_sys = p_demux->p_sys;
block_t *p_block = block_New( p_demux, i_size );
if( p_sys->i_buffer <= 0 )
return;
block_t *p_block = block_New( p_demux, p_sys->i_buffer );
if( !p_block )
return;
if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) ) if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
{ {
uint8_t *src = p_sys->buffer; uint8_t *p_src = p_sys->buffer;
uint8_t *dst = p_block->p_buffer; uint8_t *p_dst = p_block->p_buffer;
/* byte swap data */ /* byte swap data */
while( dst < &p_block->p_buffer[i_size- 1]) while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
{ {
*dst++ = src[1]; *p_dst++ = p_src[1];
*dst++ = src[0]; *p_dst++ = p_src[0];
src += 2; p_src += 2;
} }
} }
else else
{ {
memcpy( p_block->p_buffer, p_sys->buffer, i_size ); memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
} }
p_block->i_dts = p_block->i_pts = i_pts; p_block->i_dts =
p_block->i_pts = i_pts;
CheckPcr( p_demux, tk, p_block->i_pts );
es_out_Send( p_demux->out, tk->p_es, p_block ); es_out_Send( p_demux->out, tk->p_es, p_block );
}
static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
{
switch( tk->fmt.i_codec )
{
case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
case VLC_FOURCC( 'a', 't', 'r', 'c' ):
case VLC_FOURCC( 's', 'i', 'p', 'r' ):
case VLC_FOURCC( '2', '8', '_', '8' ):
DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
break;
case VLC_FOURCC( 'm','p','4','a' ):
DemuxAudioMethod2( p_demux, tk, i_pts );
break;
default:
DemuxAudioMethod3( p_demux, tk, i_pts );
break;
} }
}
static int Demux( demux_t *p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
uint8_t header[18];
if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
p_sys->i_data_packets_count )
{
if( stream_Read( p_demux->s, header, 18 ) < 18 )
return 0;
if( memcmp( header, "DATA", 4 ) )
return 0;
p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
p_sys->i_data_size = GetDWBE( &header[4] );
p_sys->i_data_packets_count = GetDWBE( &header[10] );
p_sys->i_data_packets = 0;
p_sys->i_data_offset_next = GetDWBE( &header[14] );
msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
p_sys->i_data_packets_count,
(unsigned int)p_sys->i_data_offset_next );
} }
/* Read Packet Header */
if( stream_Read( p_demux->s, header, 12 ) < 12 )
return 0;
//const int i_version = GetWBE( &header[0] );
const int i_size = GetWBE( &header[2] ) - 12;
const int i_id = GetWBE( &header[4] );
const int64_t i_pts = 1 + 1000 * GetDWBE( &header[6] );
const int i_flags= header[11]; /* flags 0x02 -> keyframe */
p_sys->i_data_packets++;
if( i_size <= 0 )
{
msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
return 1;
} }
assert( i_size <= sizeof(p_sys->buffer) );
p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
if( p_sys->i_buffer < i_size )
return 0;
real_track_t *tk = NULL;
for( int i = 0; i < p_sys->i_track; i++ )
{
if( p_sys->track[i]->i_id == i_id )
tk = p_sys->track[i];
}
if( !tk )
{
msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
return 1;
}
if( tk->fmt.i_cat == VIDEO_ES )
{
DemuxVideo( p_demux, tk, i_pts, i_flags );
}
else
{
assert( tk->fmt.i_cat == AUDIO_ES );
DemuxAudio( p_demux, tk, i_pts, i_flags );
}
/* Update PCR */
mtime_t i_pcr = 0;
for( int i = 0; i < p_sys->i_track; i++ )
{
real_track_t *tk = p_sys->track[i];
if( i_pcr <= 0 || ( tk->i_last_dts > 0 && tk->i_last_dts > i_pcr ) )
i_pcr = tk->i_last_dts;
}
if( i_pcr > 0 && i_pcr != p_sys->i_pcr )
{
p_sys->i_pcr = i_pcr;
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
}
return 1; return 1;
} }
/***************************************************************************** /*****************************************************************************
* Control: * Control:
*****************************************************************************/ *****************************************************************************/
static int ControlGoToIndex( demux_t *p_demux, rm_index_t *p_index )
{
demux_sys_t *p_sys = p_demux->p_sys;
p_sys->b_seek = true;
p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
for( int i = 0; i < p_sys->i_track; i++ )
p_sys->track[i]->i_last_dts = 0;
return stream_Seek( p_demux->s, p_index->i_file_offset );
}
static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
{
demux_sys_t *p_sys = p_demux->p_sys;
rm_index_t *p_index = p_sys->p_index;
while( p_index->i_file_offset != 0 )
{
if( p_index->i_time_offset * INT64_C(1000) > i_time )
{
if( p_index != p_sys->p_index )
p_index--;
break;
}
p_index++;
}
if( p_index->i_file_offset == 0 )
return VLC_EGENERIC;
return ControlGoToIndex( p_demux, p_index );
}
static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
{
demux_sys_t *p_sys = p_demux->p_sys;
rm_index_t *p_index = p_sys->p_index;
while( p_index->i_file_offset != 0 )
{
if( p_index->i_file_offset > i_bytes )
{
if( p_index != p_sys->p_index )
p_index--;
break;
}
p_index++;
}
if( p_index->i_file_offset == 0 )
return VLC_EGENERIC;
return ControlGoToIndex( p_demux, p_index );
}
static int Control( demux_t *p_demux, int i_query, va_list args ) static int Control( demux_t *p_demux, int i_query, va_list args )
{ {
demux_sys_t *p_sys = p_demux->p_sys; demux_sys_t *p_sys = p_demux->p_sys;
double f, *pf; double f, *pf;
int64_t i64; int64_t i64;
rm_index_t * p_index;
int64_t *pi64; int64_t *pi64;
switch( i_query ) switch( i_query )
...@@ -740,23 +762,18 @@ static int Control( demux_t *p_demux, int i_query, va_list args ) ...@@ -740,23 +762,18 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
case DEMUX_GET_POSITION: case DEMUX_GET_POSITION:
pf = (double*) va_arg( args, double* ); pf = (double*) va_arg( args, double* );
/* read stream size maybe failed in rtsp streaming,
so use duration to determin the position at first */
if( p_sys->i_our_duration > 0 ) if( p_sys->i_our_duration > 0 )
{ {
*pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration; *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
/* read stream size maybe failed in rtsp streaming, *pf = 0.0;
so use duration to determin the position at first */
i64 = stream_Size( p_demux->s ); i64 = stream_Size( p_demux->s );
if( i64 > 0 ) if( i64 > 0 )
{
*pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64; *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
}
else
{
*pf = 0.0;
}
return VLC_SUCCESS; return VLC_SUCCESS;
case DEMUX_GET_TIME: case DEMUX_GET_TIME:
...@@ -783,83 +800,40 @@ static int Control( demux_t *p_demux, int i_query, va_list args ) ...@@ -783,83 +800,40 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
f = (double) va_arg( args, double ); f = (double) va_arg( args, double );
i64 = (int64_t) ( stream_Size( p_demux->s ) * f ); i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
if ( p_sys->i_index_offset == 0 && i64 != 0 ) if( !p_sys->p_index && i64 != 0 )
{ {
msg_Err(p_demux,"Seek No Index Real File failed!" ); /* TODO seek */
msg_Err( p_demux,"Seek No Index Real File failed!" );
return VLC_EGENERIC; // no index! return VLC_EGENERIC; // no index!
} }
if ( i64 == 0 ) else if( i64 == 0 )
{ {
/* it is a rtsp stream , it is specials in access/rtsp/... */ /* it is a rtsp stream , it is specials in access/rtsp/... */
msg_Dbg(p_demux, "Seek in real rtsp stream!"); msg_Dbg(p_demux, "Seek in real rtsp stream!");
p_sys->i_pcr = (int64_t)1000 * ( p_sys->i_our_duration * f ); p_sys->i_pcr = INT64_C(1000) * ( p_sys->i_our_duration * f );
p_sys->b_seek = true;
p_sys->b_seek = 1; return stream_Seek( p_demux->s, p_sys->i_pcr );
return stream_Seek( p_demux->s, p_sys->i_pcr );
}
if ( p_sys->i_index_offset > 0 )
{
p_index = p_sys->p_index;
while( p_index->file_offset !=0 )
{
if ( p_index->file_offset > i64 )
{
msg_Dbg( p_demux, "Seek Real find! %d %d %d",
p_index->time_offset, p_index->file_offset,
(uint32_t) i64 );
if ( p_index != p_sys->p_index ) p_index --;
i64 = p_index->file_offset;
break;
}
p_index++;
}
p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
return stream_Seek( p_demux->s, i64 );
}
case DEMUX_SET_TIME:
i64 = (int64_t) va_arg( args, int64_t ) / 1000;
p_index = p_sys->p_index;
while( p_index->file_offset !=0 )
{
if ( p_index->time_offset > i64 )
{
if ( p_index != p_sys->p_index )
p_index --;
i64 = p_index->file_offset;
break;
}
p_index++;
} }
return ControlSeekByte( p_demux, i64 );
p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset; case DEMUX_SET_TIME:
if( !p_sys->p_index )
return VLC_EGENERIC;
return stream_Seek( p_demux->s, i64 ); i64 = (int64_t) va_arg( args, int64_t );
return ControlSeekTime( p_demux, i64 );
case DEMUX_GET_LENGTH: case DEMUX_GET_LENGTH:
pi64 = (int64_t*)va_arg( args, int64_t * ); pi64 = (int64_t*)va_arg( args, int64_t * );
/* the commented following lines are fen's implementation, which doesn't seem to
* work for one reason or another -- FK */
/*if( p_sys->i_mux_rate > 0 )
{
*pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
return VLC_SUCCESS;
}*/
if( p_sys->i_our_duration > 0 )
{
/* our stored duration is in ms, so... */
*pi64 = (int64_t)1000 * p_sys->i_our_duration;
return VLC_SUCCESS;
}
*pi64 = 0; *pi64 = 0;
if( p_sys->i_our_duration <= 0 )
return VLC_EGENERIC; return VLC_EGENERIC;
/* our stored duration is in ms, so... */
*pi64 = INT64_C(1000) * p_sys->i_our_duration;
return VLC_SUCCESS;
case DEMUX_GET_META: case DEMUX_GET_META:
{ {
vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* ); vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
...@@ -932,7 +906,7 @@ static void ReadRealIndex( demux_t *p_demux ) ...@@ -932,7 +906,7 @@ static void ReadRealIndex( demux_t *p_demux )
if( p_sys->p_index == NULL ) if( p_sys->p_index == NULL )
return; return;
for( i=0; i<i_index_count; i++ ) for( i = 0; i < i_index_count; i++ )
{ {
if( stream_Read( p_demux->s, buffer, 14 ) < 14 ) if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
return ; return ;
...@@ -944,14 +918,14 @@ static void ReadRealIndex( demux_t *p_demux ) ...@@ -944,14 +918,14 @@ static void ReadRealIndex( demux_t *p_demux )
return; return;
} }
p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] ); p_sys->p_index[i].i_time_offset = GetDWBE( &buffer[2] );
p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] ); p_sys->p_index[i].i_file_offset = GetDWBE( &buffer[6] );
p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] ); p_sys->p_index[i].i_frame_index = GetDWBE( &buffer[10] );
msg_Dbg( p_demux, msg_Dbg( p_demux,
"Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32, "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
p_sys->p_index[i].time_offset, p_sys->p_index[i].i_time_offset,
p_sys->p_index[i].file_offset, p_sys->p_index[i].i_file_offset,
p_sys->p_index[i].frame_index ); p_sys->p_index[i].i_frame_index );
} }
memset( p_sys->p_index + i_index_count, 0, sizeof( rm_index_t ) ); memset( p_sys->p_index + i_index_count, 0, sizeof( rm_index_t ) );
} }
...@@ -959,394 +933,429 @@ static void ReadRealIndex( demux_t *p_demux ) ...@@ -959,394 +933,429 @@ static void ReadRealIndex( demux_t *p_demux )
/***************************************************************************** /*****************************************************************************
* HeaderRead: * HeaderRead:
*****************************************************************************/ *****************************************************************************/
static int HeaderRead( demux_t *p_demux ) static int HeaderRMF( demux_t *p_demux )
{ {
demux_sys_t *p_sys = p_demux->p_sys; uint8_t p_buffer[8];
uint8_t header[100]; /* FIXME */
uint32_t i_id;
uint32_t i_size;
int64_t i_skip;
int i_version;
p_sys->p_meta = vlc_meta_New();
for( ;; )
{
/* Read the header */
if( stream_Read( p_demux->s, header, 10 ) < 10 )
{
return VLC_EGENERIC;
}
i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
i_size = GetDWBE( &header[4] );
i_version = GetWBE( &header[8] );
msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
(char*)&i_id, i_size, i_version );
if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') ) if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
{
msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
return VLC_EGENERIC; return VLC_EGENERIC;
}
i_skip = i_size - 10;
if( i_id == VLC_FOURCC('.','R','M','F') )
{
if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
msg_Dbg( p_demux, " - file version=0x%x num headers=%d", msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
GetDWBE( &header[0] ), GetDWBE( &header[4] ) ); GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
return VLC_SUCCESS;
}
static int HeaderPROP( demux_t *p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
i_skip -= 8; uint8_t p_buffer[40];
}
else if( i_id == VLC_FOURCC('P','R','O','P') )
{
int i_flags; int i_flags;
if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC; if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
return VLC_EGENERIC;
msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d", msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
GetDWBE(&header[0]), GetDWBE(&header[4]) ); GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d", msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
GetDWBE(&header[8]), GetDWBE(&header[12]) ); GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) ); msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) ); msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) ); msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) ); msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) ); msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) ); msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
/* set the duration for export in control */ /* set the duration for export in control */
p_sys->i_our_duration = (int)GetDWBE(&header[20]); p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
p_sys->i_index_offset = GetDWBE(&header[28]); p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
i_flags = GetWBE(&header[38]); i_flags = GetWBE(&p_buffer[38]);
msg_Dbg( p_demux, " - flags=0x%x %s%s%s", msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
i_flags, i_flags,
i_flags&0x0001 ? "PN_SAVE_ENABLED " : "", i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "", i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" ); i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
i_skip -= 40;
}
else if( i_id == VLC_FOURCC('C','O','N','T') )
{
int i_len;
char *psz;
/* FIXME FIXME: should convert from whatever the character return VLC_SUCCESS;
* encoding of the input meta data is to UTF-8. */ }
static int HeaderCONT( demux_t *p_demux )
stream_Read( p_demux->s, header, 2 ); {
if( ( i_len = GetWBE( header ) ) > 0 ) demux_sys_t *p_sys = p_demux->p_sys;
{
psz = malloc( i_len + 1 );
stream_Read( p_demux->s, psz, i_len );
psz[i_len] = '\0';
EnsureUTF8( psz );
msg_Dbg( p_demux, " - title=`%s'", psz );
p_sys->psz_title = psz;
i_skip -= i_len;
}
i_skip -= 2;
stream_Read( p_demux->s, header, 2 ); /* */
if( ( i_len = GetWBE( header ) ) > 0 ) p_sys->psz_title = StreamReadString2( p_demux->s );
{ if( p_sys->psz_title )
psz = malloc( i_len + 1 ); msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
stream_Read( p_demux->s, psz, i_len );
psz[i_len] = '\0';
EnsureUTF8( psz ); /* */
msg_Dbg( p_demux, " - author=`%s'", psz ); p_sys->psz_artist = StreamReadString2( p_demux->s );
p_sys->psz_artist = psz; if( p_sys->psz_artist )
i_skip -= i_len; msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
}
i_skip -= 2;
stream_Read( p_demux->s, header, 2 ); /* */
if( ( i_len = GetWBE( header ) ) > 0 ) p_sys->psz_copyright = StreamReadString2( p_demux->s );
{ if( p_sys->psz_copyright )
psz = malloc( i_len + 1 ); msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
stream_Read( p_demux->s, psz, i_len );
psz[i_len] = '\0';
EnsureUTF8( psz ); /* */
msg_Dbg( p_demux, " - copyright=`%s'", psz ); p_sys->psz_description = StreamReadString2( p_demux->s );
p_sys->psz_copyright = psz; if( p_sys->psz_description )
i_skip -= i_len; msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
}
i_skip -= 2;
stream_Read( p_demux->s, header, 2 ); return VLC_SUCCESS;
if( ( i_len = GetWBE( header ) ) > 0 ) }
{ static int HeaderMDPR( demux_t *p_demux )
psz = malloc( i_len + 1 ); {
stream_Read( p_demux->s, psz, i_len ); uint8_t p_buffer[30];
psz[i_len] = '\0';
EnsureUTF8( psz );
msg_Dbg( p_demux, " - comment=`%s'", psz );
p_sys->psz_description = psz;
i_skip -= i_len;
}
i_skip -= 2;
}
else if( i_id == VLC_FOURCC('M','D','P','R') )
{
/* Media properties header */ /* Media properties header */
int i_num; if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
int i_len; return VLC_EGENERIC;
char *psz;
if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC; const int i_num = GetWBE( &p_buffer[0] );
i_num = GetWBE( header );
msg_Dbg( p_demux, " - id=0x%x", i_num ); msg_Dbg( p_demux, " - id=0x%x", i_num );
msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d", msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
GetDWBE(&header[2]), GetDWBE(&header[6]) ); GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d", msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
GetDWBE(&header[10]), GetDWBE(&header[14]) ); GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) ); msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) ); msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) ); msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
i_skip -= 30; /* */
const uint8_t *p_peek;
int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
int i_peek = i_peek_org;
if( i_peek <= 0 )
return VLC_EGENERIC;
stream_Read( p_demux->s, header, 1 ); char *psz_name = MemoryReadString1( &p_peek, &i_peek );
if( ( i_len = header[0] ) > 0 ) if( psz_name )
{ {
psz = malloc( i_len + 1 ); msg_Dbg( p_demux, " - name=`%s'", psz_name );
stream_Read( p_demux->s, psz, i_len ); free( psz_name );
psz[i_len] = '\0';
msg_Dbg( p_demux, " - name=`%s'", psz );
free( psz );
i_skip -= i_len;
} }
i_skip--; char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
if( psz_mime )
stream_Read( p_demux->s, header, 1 );
if( ( i_len = header[0] ) > 0 )
{ {
psz = malloc( i_len + 1 ); msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
stream_Read( p_demux->s, psz, i_len ); free( psz_mime );
psz[i_len] = '\0';
msg_Dbg( p_demux, " - mime=`%s'", psz );
free( psz );
i_skip -= i_len;
} }
i_skip--; const int i_skip = i_peek_org - i_peek;
if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
return VLC_EGENERIC;
stream_Read( p_demux->s, header, 4 ); /* */
if( ( i_len = GetDWBE( header ) ) > 0 ) if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
{ return VLC_EGENERIC;
ReadCodecSpecificData( p_demux, i_len, i_num );
stream_Read( p_demux->s, NULL, i_len );
i_skip -= i_len; const uint32_t i_size = GetDWBE( p_buffer );
} if( i_size > 0 )
i_skip -= 4;
}
else if( i_id == VLC_FOURCC('D','A','T','A') )
{ {
stream_Read( p_demux->s, header, 8 ); CodecParse( p_demux, i_size, i_num );
if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
{
demux_sys_t *p_sys = p_demux->p_sys;
uint8_t p_buffer[8];
if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
return VLC_EGENERIC;
p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10; p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
p_sys->i_data_size = i_size; p_sys->i_data_size = i_size;
p_sys->i_data_packets_count = GetDWBE( header ); p_sys->i_data_packets_count = GetDWBE( p_buffer );
p_sys->i_data_packets = 0; p_sys->i_data_packets = 0;
p_sys->i_data_offset_next = GetDWBE( &header[4] ); p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
msg_Dbg( p_demux, " - packets count=%d next=%u", msg_Dbg( p_demux, " - packets count=%d next=%u",
p_sys->i_data_packets_count, p_sys->i_data_packets_count,
(uint32_t)p_sys->i_data_offset_next ); (unsigned int)p_sys->i_data_offset_next );
return VLC_SUCCESS;
}
/* we have finished the header */ static int HeaderRead( demux_t *p_demux )
break; {
demux_sys_t *p_sys = p_demux->p_sys;
p_sys->p_meta = vlc_meta_New();
for( ;; )
{
const int64_t i_stream_position = stream_Tell( p_demux->s );
uint8_t header[100]; /* FIXME */
/* Read the header */
if( stream_Read( p_demux->s, header, 10 ) < 10 )
return VLC_EGENERIC;
const uint32_t i_id = VLC_FOURCC( header[0], header[1],
header[2], header[3] );
const uint32_t i_size = GetDWBE( &header[4] );
const int i_version = GetWBE( &header[8] );
msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
(char*)&i_id, i_size, i_version );
/* */
if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
{
msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
return VLC_EGENERIC;
} }
else
int i_ret;
switch( i_id )
{ {
case VLC_FOURCC('.','R','M','F'):
i_ret = HeaderRMF( p_demux );
break;
case VLC_FOURCC('P','R','O','P'):
i_ret = HeaderPROP( p_demux );
break;
case VLC_FOURCC('C','O','N','T'):
i_ret = HeaderCONT( p_demux );
break;
case VLC_FOURCC('M','D','P','R'):
i_ret = HeaderMDPR( p_demux );
break;
case VLC_FOURCC('D','A','T','A'):
i_ret = HeaderDATA( p_demux, i_size );
break;
default:
/* unknow header */ /* unknow header */
msg_Dbg( p_demux, "unknown chunk" ); msg_Dbg( p_demux, "unknown chunk" );
i_ret = VLC_SUCCESS;
break;
} }
if( i_ret )
return i_ret;
if( i_skip < 0 ) return VLC_EGENERIC; if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
stream_Read( p_demux->s, NULL, i_skip ); break;
}
/* TODO read index if possible */ /* Skip unread data */
const int64_t i_stream_current = stream_Tell( p_demux->s );
const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
if ( p_sys->i_index_offset > 0 ) if( i_stream_skip > 0 )
{ {
int64_t pos; if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
pos = stream_Tell( p_demux->s ); return VLC_EGENERIC;
ReadRealIndex( p_demux ); }
stream_Seek( p_demux->s, pos ); else if( i_stream_skip < 0 )
{
return VLC_EGENERIC;
} }
}
/* read index if possible */
if( p_sys->i_index_offset > 0 )
{
const int64_t i_position = stream_Tell( p_demux->s );
ReadRealIndex( p_demux );
if( stream_Seek( p_demux->s, i_position ) )
return VLC_EGENERIC;
}
return VLC_SUCCESS; return VLC_SUCCESS;
} }
static const uint8_t * MetaRead( demux_t *p_demux, const uint8_t *p_peek ) /**
* This function will read a pascal string with size stored in 1 byte from a
* memory buffer.
*
* FIXME what is the right charset ?
*/
static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
{ {
demux_sys_t *p_sys = p_demux->p_sys; const uint8_t *p_data = *pp_data;
int i_data = *pi_data;
int i_len; char *psz_string = NULL;
char *psz;
/* Title */ if( i_data < 1 )
i_len = *p_peek ; p_peek++; goto exit;
if( i_len > 0 )
{
psz = malloc( i_len + 1 );
memcpy( psz, p_peek, i_len );
psz[i_len] = '\0';
EnsureUTF8( psz ); int i_length = *p_data++; i_data--;
msg_Dbg( p_demux, " - title=`%s'", psz ); if( i_length > i_data )
p_sys->psz_title = psz; i_length = i_data;
}
p_peek += i_len;
/* Authors */ if( i_length > 0 )
i_len = *p_peek ; p_peek++;
if( i_len > 0 )
{ {
psz = malloc( i_len + 1 ); psz_string = strndup( (const char*)p_data, i_length );
memcpy( psz, p_peek, i_len ); if( psz_string )
psz[i_len] = '\0'; EnsureUTF8( psz_string );
EnsureUTF8( psz ); p_data += i_length;
msg_Dbg( p_demux, " - artist=`%s'", psz ); i_data -= i_length;
p_sys->psz_artist = psz;
} }
p_peek += i_len;
/* Copyright */ exit:
i_len = *p_peek ; p_peek++; *pp_data = p_data;
if( i_len > 0 ) *pi_data = i_data;
{ return psz_string;
psz = malloc( i_len + 1 ); }
memcpy( psz, p_peek, i_len ); /**
psz[i_len] = '\0'; * This function will read a pascal string with size stored in 2 bytes from
* a stream_t.
*
* FIXME what is the right charset ?
*/
static char *StreamReadString2( stream_t *s )
{
uint8_t p_tmp[2];
EnsureUTF8( psz ); if( stream_Read( s, p_tmp, 2 ) < 2 )
msg_Dbg( p_demux, " - Copyright=`%s'", psz ); return NULL;
p_sys->psz_copyright = psz;
}
p_peek += i_len;
/* Comment */ const int i_length = GetWBE( p_tmp );
i_len = *p_peek ; p_peek++; if( i_length <= 0 )
if( i_len > 0 ) return NULL;
{
psz = malloc( i_len + 1 );
memcpy( psz, p_peek, i_len );
psz[i_len] = '\0';
EnsureUTF8( psz ); char *psz_string = calloc( 1, i_length + 1 );
msg_Dbg( p_demux, " - Comment=`%s'", psz );
p_sys->psz_description = psz; stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
}
p_peek += i_len;
return p_peek; if( psz_string )
EnsureUTF8( psz_string );
return psz_string;
} }
static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
{
demux_sys_t *p_sys = p_demux->p_sys;
/* Title */
p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
if( p_sys->psz_title )
msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
/* Authors */
p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
if( p_sys->psz_artist )
msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
/* Copyright */
p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
if( p_sys->psz_copyright )
msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
/* Comment */
p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
if( p_sys->psz_description )
msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
}
static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
{ {
demux_sys_t *p_sys = p_demux->p_sys; demux_sys_t *p_sys = p_demux->p_sys;
es_format_t fmt;
real_track_t *tk;
const uint8_t *p_peek;
msg_Dbg( p_demux, " - specific data len=%d", i_len ); if( i_data < 34 )
if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC; return VLC_EGENERIC;
if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) ) /* */
{ es_format_t fmt;
es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9], es_format_Init( &fmt, VIDEO_ES,
p_peek[10], p_peek[11] ) ); VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
fmt.video.i_width = GetWBE( &p_peek[12] ); fmt.video.i_width = GetWBE( &p_data[12] );
fmt.video.i_height= GetWBE( &p_peek[14] ); fmt.video.i_height= GetWBE( &p_data[14] );
fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
fmt.video.i_frame_rate_base = 1 << 16;
fmt.i_extra = 8; fmt.i_extra = 8;
fmt.p_extra = malloc( 8 ); fmt.p_extra = malloc( 8 );
((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] ); if( !fmt.p_extra )
((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] ); return VLC_ENOMEM;
msg_Dbg( p_demux, " - video 0x%08x 0x%08x", memcpy( fmt.p_extra, &p_data[26], 8 );
((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
if( GetDWBE( &p_peek[30] ) == 0x10003000 || //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
GetDWBE( &p_peek[30] ) == 0x10003001 )
/* */
switch( GetDWBE( &p_data[30] ) )
{ {
case 0x10003000:
case 0x10003001:
fmt.i_codec = VLC_FOURCC( 'R','V','1','3' ); fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
} break;
else if( GetDWBE( &p_peek[30] ) == 0x20001000 || case 0x20001000:
GetDWBE( &p_peek[30] ) == 0x20100001 || case 0x20100001:
GetDWBE( &p_peek[30] ) == 0x20200002 ) case 0x20200002:
{ case 0x20201002:
fmt.i_codec = VLC_FOURCC( 'R','V','2','0' ); fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
} break;
else if( GetDWBE( &p_peek[30] ) == 0x30202002 ) case 0x30202002:
{
fmt.i_codec = VLC_FOURCC( 'R','V','3','0' ); fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
} break;
else if( GetDWBE( &p_peek[30] ) == 0x40000000 ) case 0x40000000:
{
fmt.i_codec = VLC_FOURCC( 'R','V','4','0' ); fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
break;
} }
msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
(char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
msg_Dbg( p_demux, " - video %4.4s %dx%d", real_track_t *tk = malloc( sizeof( *tk ) );
(char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height ); if( !tk )
{
tk = malloc( sizeof( real_track_t ) ); es_format_Clean( &fmt );
return VLC_ENOMEM;
}
tk->i_out_subpacket = 0; tk->i_out_subpacket = 0;
tk->i_subpacket = 0; tk->i_subpacket = 0;
tk->i_subpackets = 0; tk->i_subpackets = 0;
tk->p_subpackets = NULL; tk->p_subpackets = NULL;
tk->p_subpackets_timecode = NULL; tk->p_subpackets_timecode = NULL;
tk->i_id = i_num; tk->i_id = i_tk_id;
tk->fmt = fmt; tk->fmt = fmt;
tk->i_frame = 0; tk->i_frame_num = -1;
tk->i_frame_size = 0;
tk->p_frame = NULL; tk->p_frame = NULL;
tk->i_last_dts = 0;
tk->p_es = es_out_Add( p_demux->out, &fmt ); tk->p_es = es_out_Add( p_demux->out, &fmt );
TAB_APPEND( p_sys->i_track, p_sys->track, tk ); TAB_APPEND( p_sys->i_track, p_sys->track, tk );
} return VLC_SUCCESS;
else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) ) }
{ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
int i_header_size = 0; {
demux_sys_t *p_sys = p_demux->p_sys;
es_format_t fmt;
if( i_data < 6 )
return VLC_EGENERIC;
int i_flavor = 0; int i_flavor = 0;
int i_coded_frame_size = 0; int i_coded_frame_size = 0;
int i_subpacket_h = 0; int i_subpacket_h = 0;
int i_frame_size = 0; int i_frame_size = 0;
int i_subpacket_size = 0; int i_subpacket_size = 0;
char p_genr[4]; char p_genr[4];
int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */ int i_version = GetWBE( &p_data[4] );
int i_extra_codec = 0;
msg_Dbg( p_demux, " - audio version=%d", i_version ); msg_Dbg( p_demux, " - audio version=%d", i_version );
p_peek += 6; /* 4 + version */
es_format_Init( &fmt, AUDIO_ES, 0 ); es_format_Init( &fmt, AUDIO_ES, 0 );
RVoid( &p_data, &i_data, 6 ); /* 4 + version */
if( i_version == 3 ) /* RMF version 3 or .ra version 3 */ if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
{ {
i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */ RVoid( &p_data, &i_data, 2 + 10 + 4 );
p_peek += 10; /* Unknown */
msg_Dbg( p_demux, "Data Size: %i", GetDWBE( p_peek ) );
p_peek += 4; /* Data Size */
/* Meta Datas */ /* Meta Datas */
p_peek = MetaRead( p_demux, p_peek ); CodecMetaRead( p_demux, &p_data, &i_data );
p_peek ++; /* Unknown */ RVoid( &p_data, &i_data, 1 + 1 );
p_peek ++; /* FourCC length = 4 */ if( i_data >= 4 )
memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4; /* FourCC*/ memcpy( &fmt.i_codec, p_data, 4 );
RVoid( &p_data, &i_data, 4 );
fmt.audio.i_channels = 1; /* This is always the case in rm3 */ fmt.audio.i_channels = 1; /* This is always the case in rm3 */
fmt.audio.i_rate = 8000; fmt.audio.i_rate = 8000;
...@@ -1356,58 +1365,57 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1356,58 +1365,57 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
} }
else /* RMF version 4/5 or .ra version 4 */ else /* RMF version 4/5 or .ra version 4 */
{ {
p_peek += 2; /* 00 00 */ RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
p_peek += 4; /* .ra4 or .ra5 */ i_flavor = R16( &p_data, &i_data );
p_peek += 4; /* data size */ i_coded_frame_size = R32( &p_data, &i_data );
p_peek += 2; /* version (4 or 5) */ RVoid( &p_data, &i_data, 4 + 4 + 4 );
i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */ i_subpacket_h = R16( &p_data, &i_data );
i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */ i_frame_size = R16( &p_data, &i_data );
i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/ i_subpacket_size = R16( &p_data, &i_data );
p_peek += 4; /* ?? */ if( (!i_subpacket_size && !p_sys->b_real_audio ) ||
p_peek += 4; /* ?? */ !i_frame_size || !i_coded_frame_size )
p_peek += 4; /* ?? */
i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
if( (!i_subpacket_size && !p_sys->b_is_real_audio )
|| !i_frame_size || !i_coded_frame_size )
return VLC_EGENERIC; return VLC_EGENERIC;
p_peek += 2; /* ?? */ RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */ fmt.audio.i_rate = R16( &p_data, &i_data );
RVoid( &p_data, &i_data, 2 );
fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */ fmt.audio.i_bitspersample = R16( &p_data, &i_data );
p_peek += 2; /* ?? */ fmt.audio.i_channels = R16( &p_data, &i_data );
fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
fmt.audio.i_blockalign = i_frame_size; fmt.audio.i_blockalign = i_frame_size;
if( i_version == 5 ) if( i_version == 5 )
{ {
memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */ if( i_data >= 8 )
memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4; {
memcpy( p_genr, &p_data[0], 4 );
memcpy( &fmt.i_codec, &p_data[4], 4 );
}
RVoid( &p_data, &i_data, 8 );
} }
else /* version 4 */ else /* version 4 */
{ {
p_peek += 1 + p_peek[0]; /* Inteleaver ID string and lenth */ if( i_data > 0 )
memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* FourCC */ RVoid( &p_data, &i_data, 1 + *p_data );
p_peek += 1 + p_peek[0]; if( i_data >= 1 + 4 )
memcpy( &fmt.i_codec, &p_data[1], 4 );
if( i_data > 0 )
RVoid( &p_data, &i_data, 1 + *p_data );
} }
msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz", msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
(char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate ); (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
p_peek += 3; /* ?? */ RVoid( &p_data, &i_data, 3 );
if( p_sys->b_is_real_audio ) if( p_sys->b_real_audio )
{ {
p_peek = MetaRead( p_demux, p_peek ); CodecMetaRead( p_demux, &p_data, &i_data );
} }
else else
{ {
if( i_version == 5 ) p_peek++; if( i_version == 5 )
/* Extra Data then: DWord + byte[] */ RVoid( &p_data, &i_data, 1 );
fmt.i_extra = GetDWBE( p_peek ); p_peek += 4; i_extra_codec = R32( &p_data, &i_data );
} }
} }
...@@ -1420,7 +1428,6 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1420,7 +1428,6 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
break; break;
case VLC_FOURCC('2','8','_','8'): case VLC_FOURCC('2','8','_','8'):
fmt.i_extra = 0;
fmt.audio.i_blockalign = i_coded_frame_size; fmt.audio.i_blockalign = i_coded_frame_size;
break; break;
...@@ -1432,16 +1439,19 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1432,16 +1439,19 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
case VLC_FOURCC( 'r','a','c','p' ): case VLC_FOURCC( 'r','a','c','p' ):
fmt.i_codec = VLC_FOURCC( 'm','p','4','a' ); fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; } if( i_extra_codec > 0 )
if( fmt.i_extra > 0 )
{
fmt.p_extra = malloc( fmt.i_extra );
if( fmt.p_extra == NULL )
{ {
msg_Err( p_demux, "Error in the extra data" ); i_extra_codec--;
return VLC_EGENERIC; RVoid( &p_data, &i_data, 1 );
} }
memcpy( fmt.p_extra, p_peek, fmt.i_extra ); if( i_extra_codec > 0 )
{
fmt.p_extra = malloc( fmt.i_extra );
if( !fmt.p_extra || fmt.i_extra > i_data )
return VLC_ENOMEM;
fmt.i_extra = i_extra_codec;
memcpy( fmt.p_extra, p_data, fmt.i_extra );
} }
break; break;
...@@ -1454,16 +1464,16 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1454,16 +1464,16 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
else else
fmt.audio.i_blockalign = i_coded_frame_size; fmt.audio.i_blockalign = i_coded_frame_size;
if( fmt.i_extra > 0 ) if( i_extra_codec > 0 )
{ {
fmt.p_extra = malloc( fmt.i_extra ); fmt.p_extra = malloc( fmt.i_extra );
if( fmt.p_extra == NULL ) if( !fmt.p_extra || fmt.i_extra > i_data )
{ return VLC_ENOMEM;
msg_Err( p_demux, "Error in the extra data" );
return VLC_EGENERIC; fmt.i_extra = i_extra_codec;
} memcpy( fmt.p_extra, p_data, fmt.i_extra );
memcpy( fmt.p_extra, p_peek, fmt.i_extra );
} }
break; break;
case VLC_FOURCC('r','a','l','f'): case VLC_FOURCC('r','a','l','f'):
...@@ -1476,17 +1486,18 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1476,17 +1486,18 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
(char*)&fmt.i_codec ); (char*)&fmt.i_codec );
break; break;
} }
if( fmt.i_codec != 0 )
{
msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra ); msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
tk = malloc( sizeof( real_track_t ) ); /* */
real_track_t *tk = malloc( sizeof( *tk ) );
if( !tk ) if( !tk )
{
es_format_Clean( &fmt );
return VLC_ENOMEM; return VLC_ENOMEM;
tk->i_id = i_num; }
tk->i_id = i_tk_id;
tk->fmt = fmt; tk->fmt = fmt;
tk->i_frame = 0; tk->i_frame_size = 0;
tk->p_frame = NULL; tk->p_frame = NULL;
tk->i_subpacket_h = i_subpacket_h; tk->i_subpacket_h = i_subpacket_h;
...@@ -1499,8 +1510,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1499,8 +1510,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
tk->i_subpackets = 0; tk->i_subpackets = 0;
tk->p_subpackets = NULL; tk->p_subpackets = NULL;
tk->p_subpackets_timecode = NULL; tk->p_subpackets_timecode = NULL;
if( fmt.i_codec == VLC_FOURCC('c','o','o','k') if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
|| fmt.i_codec == VLC_FOURCC('a','t','r','c') ) fmt.i_codec == VLC_FOURCC('a','t','r','c') )
{ {
tk->i_subpackets = tk->i_subpackets =
i_subpacket_h * i_frame_size / tk->i_subpacket_size; i_subpacket_h * i_frame_size / tk->i_subpacket_size;
...@@ -1529,12 +1540,59 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num ) ...@@ -1529,12 +1540,59 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
return VLC_EGENERIC; return VLC_EGENERIC;
} }
tk->i_last_dts = 0;
tk->p_es = es_out_Add( p_demux->out, &fmt ); tk->p_es = es_out_Add( p_demux->out, &fmt );
TAB_APPEND( p_sys->i_track, p_sys->track, tk ); TAB_APPEND( p_sys->i_track, p_sys->track, tk );
return VLC_SUCCESS;
}
static int CodecParse( demux_t *p_demux, int i_len, int i_num )
{
const uint8_t *p_peek;
msg_Dbg( p_demux, " - specific data len=%d", i_len );
if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
return VLC_EGENERIC;
if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
{
return CodecVideoParse( p_demux, i_num, p_peek, i_len );
} }
else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
{
return CodecAudioParse( p_demux, i_num, p_peek, i_len );
} }
return VLC_SUCCESS; return VLC_SUCCESS;
} }
/* Small memory buffer helpers */
static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
{
if( i_size > *pi_data )
i_size = *pi_data;
*pp_data += i_size;
*pi_data -= i_size;
}
#define RX(name, type, size, code ) \
static type name( const uint8_t **pp_data, int *pi_data ) { \
if( *pi_data < (size) ) \
return 0; \
type v = code; \
RVoid( pp_data, pi_data, size ); \
return v; \
}
RX(R8, uint8_t, 1, **pp_data )
RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
static int RLength( const uint8_t **pp_data, int *pi_data )
{
const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
if( v0 >= 0x4000 )
return v0 - 0x4000;
return (v0 << 16) | R16( pp_data, pi_data );
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment