* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
#include <vlc/vlc.h>
-#include <stdio.h>
-#include <stdlib.h> /* malloc(), free() */
#include <vlc_demux.h>
if( (i_read > 0) && (p_peek[0]) ) \
{ \
const int __i_copy__ = strnlen( (char*)p_peek, i_read-1 ); \
- p_str = calloc( sizeof(char), __i_copy__+1 ); \
- if( __i_copy__ > 0 ) memcpy( p_str, p_peek, __i_copy__ ); \
- p_str[__i_copy__] = 0; \
+ p_str = malloc( __i_copy__+1 ); \
+ if( p_str ) \
+ { \
+ memcpy( p_str, p_peek, __i_copy__ ); \
+ p_str[__i_copy__] = 0; \
+ } \
p_peek += __i_copy__ + 1; \
i_read -= __i_copy__ + 1; \
} \
*/
+/* This macro is used when we want to printf the box type
+ * APPLE annotation box is :
+ * either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
+ * either 32-bit ASCII text string
+ */
+#define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
+
static uint32_t Get24bBE( const uint8_t *p )
{
return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
/* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71
where XXXXXXXX is the fourcc */
/* FIXME implement this */
+ (void)p_uuid;
+ (void)i_fourcc;
}
/* some functions for mp4 encoding of variables */
#ifdef MP4_VERBOSE
if( p_box->i_size )
{
- msg_Dbg( p_stream, "found Box: %4.4s size "I64Fd,
- (char*)&p_box->i_type, p_box->i_size );
+ if MP4_BOX_TYPE_ASCII()
+ msg_Dbg( p_stream, "found Box: %4.4s size "I64Fd,
+ (char*)&p_box->i_type, p_box->i_size );
+ else
+ msg_Dbg( p_stream, "found Box: c%3.3s size "I64Fd,
+ (char*)&p_box->i_type+1, p_box->i_size );
}
#endif
static void MP4_FreeBox_Common( MP4_Box_t *p_box )
{
/* Up to now do nothing */
+ (void)p_box;
}
static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
/* Nothing to do */
#ifdef MP4_VERBOSE
- msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
+ if MP4_BOX_TYPE_ASCII()
+ msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
+ else
+ msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
#endif
return 1;
}
if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
{
unsigned int i;
- p_box->data.p_ftyp->i_compatible_brands =
- calloc( p_box->data.p_ftyp->i_compatible_brands_count, sizeof(uint32_t));
+ uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
+ calloc( p_box->data.p_ftyp->i_compatible_brands_count,
+ sizeof(uint32_t));
+
+ if( tab == NULL )
+ p_box->data.p_ftyp->i_compatible_brands_count = 0;
for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
{
- MP4_GETFOURCC( p_box->data.p_ftyp->i_compatible_brands[i] );
+ MP4_GETFOURCC( tab[i] );
}
}
else
static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
{
int32_t i_reserved;
+ int code = 0;
MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
if( i_read > 0 )
{
- p_box->data.p_hdlr->psz_name = calloc( sizeof( char ), i_read + 1 );
+ uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
+ if( psz == NULL )
+ goto error;
/* Yes, I love .mp4 :( */
if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
MP4_GET1BYTE( i_len );
i_copy = __MIN( i_read, i_len );
- memcpy( p_box->data.p_hdlr->psz_name, p_peek, i_copy );
+ memcpy( psz, p_peek, i_copy );
p_box->data.p_hdlr->psz_name[i_copy] = '\0';
}
else
{
- memcpy( p_box->data.p_hdlr->psz_name, p_peek, i_read );
+ memcpy( psz, p_peek, i_read );
p_box->data.p_hdlr->psz_name[i_read] = '\0';
}
}
#ifdef MP4_VERBOSE
- msg_Dbg( p_stream, "read box: \"hdlr\" handler type %4.4s name %s",
- (char*)&p_box->data.p_hdlr->i_handler_type,
- p_box->data.p_hdlr->psz_name );
+ msg_Dbg( p_stream, "read box: \"hdlr\" handler type %4.4s name %s",
+ (char*)&p_box->data.p_hdlr->i_handler_type,
+ p_box->data.p_hdlr->psz_name );
#endif
- MP4_READBOX_EXIT( 1 );
+ code = 1;
+
+error:
+ MP4_READBOX_EXIT( code );
}
static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
static void MP4_FreeBox_url( MP4_Box_t *p_box )
{
- FREENULL( p_box->data.p_url->psz_location )
+ FREENULL( p_box->data.p_url->psz_location );
}
static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
p_box->data.p_stts->i_sample_count =
- calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
+ calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
p_box->data.p_stts->i_sample_delta =
- calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
+ calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
{
MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
p_box->data.p_ctts->i_sample_count =
- calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
+ calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
p_box->data.p_ctts->i_sample_offset =
- calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
+ calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
{
unsigned int i_len;
MP4_GET1BYTE( i_len );
- es_descriptor.psz_URL = calloc( sizeof(char), i_len + 1 );
- memcpy( es_descriptor.psz_URL, p_peek, i_len );
- es_descriptor.psz_URL[i_len] = 0;
+ es_descriptor.psz_URL = malloc( i_len + 1 );
+ if( es_descriptor.psz_URL )
+ {
+ memcpy( es_descriptor.psz_URL, p_peek, i_len );
+ es_descriptor.psz_URL[i_len] = 0;
+ }
p_peek += i_len;
i_read -= i_len;
}
es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
- memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
- p_peek, i_len );
+ if( es_descriptor.p_decConfigDescr->p_decoder_specific_info )
+ memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
+ p_peek, i_len );
MP4_READBOX_EXIT( 1 );
p_avcC->i_avcC = i_read;
if( p_avcC->i_avcC > 0 )
{
- p_avcC->p_avcC = malloc( p_avcC->i_avcC );
- memcpy( p_avcC->p_avcC, p_peek, i_read );
+ uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
+ if( p )
+ memcpy( p, p_peek, i_read );
}
MP4_GET1BYTE( p_avcC->i_version );
{
/* SoundDescriptionV2 */
double f_sample_rate;
- int64_t dummy;
+ int64_t dummy;
uint32_t i_channel;
MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
MP4_GET8BYTES( dummy );
- memcpy( &f_sample_rate, &dummy, 8 );
+ memcpy( &f_sample_rate, &dummy, 8 );
msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
p_box->data.p_sample_soun->i_sampleratehi = (int)f_sample_rate % 65536;
if( p_box->i_type == FOURCC_alac )
{
- if( p_box->data.p_sample_soun->p_qt_description )
- free( p_box->data.p_sample_soun->p_qt_description );
+ free( p_box->data.p_sample_soun->p_qt_description );
p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
p_box->data.p_sample_soun->i_qt_description = i_read;
MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
p_box->data.p_stsz->i_entry_size =
- calloc( sizeof( uint32_t ), p_box->data.p_stsz->i_sample_count );
+ calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
if( !p_box->data.p_stsz->i_sample_size )
{
MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
p_box->data.p_stsc->i_first_chunk =
- calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
+ calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
p_box->data.p_stsc->i_samples_per_chunk =
- calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
+ calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
p_box->data.p_stsc->i_sample_description_index =
- calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
+ calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
{
MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
p_box->data.p_co64->i_chunk_offset =
- calloc( sizeof( uint64_t ), p_box->data.p_co64->i_entry_count );
+ calloc( p_box->data.p_co64->i_entry_count, sizeof(uint64_t) );
for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
{
MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
p_box->data.p_stss->i_sample_number =
- calloc( sizeof( uint32_t ), p_box->data.p_stss->i_entry_count );
+ calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
{
static void MP4_FreeBox_stss( MP4_Box_t *p_box )
{
- FREENULL( p_box->data.p_stss->i_sample_number )
+ FREENULL( p_box->data.p_stss->i_sample_number );
}
static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
p_box->data.p_stsh->i_shadowed_sample_number =
- calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
+ calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
p_box->data.p_stsh->i_sync_sample_number =
- calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
+ calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
{
- FREENULL( p_box->data.p_stsh->i_shadowed_sample_number )
- FREENULL( p_box->data.p_stsh->i_sync_sample_number )
+ FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
+ FREENULL( p_box->data.p_stsh->i_sync_sample_number );
}
MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
p_box->data.p_stdp->i_priority =
- calloc( sizeof( uint16_t ), i_read / 2 );
+ calloc( i_read / 2, sizeof(uint16_t) );
for( i = 0; i < i_read / 2 ; i++ )
{
static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
{
- FREENULL( p_box->data.p_stdp->i_priority )
+ FREENULL( p_box->data.p_stdp->i_priority );
}
static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
{
+ int code = 0;
unsigned int i;
+ uint32_t count;
MP4_READBOX_ENTER( MP4_Box_data_padb_t );
MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
+ count = (p_box->data.p_padb->i_sample_count + 1) / 2;
- p_box->data.p_padb->i_reserved1 =
- calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
- p_box->data.p_padb->i_pad2 =
- calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
- p_box->data.p_padb->i_reserved2 =
- calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
- p_box->data.p_padb->i_pad1 =
- calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
-
+ p_box->data.p_padb->i_reserved1 = calloc( count, sizeof(uint16_t) );
+ p_box->data.p_padb->i_pad2 = calloc( count, sizeof(uint16_t) );
+ p_box->data.p_padb->i_reserved2 = calloc( count, sizeof(uint16_t) );
+ p_box->data.p_padb->i_pad1 = calloc( count, sizeof(uint16_t) );
for( i = 0; i < i_read / 2 ; i++ )
{
+ if( i >= count )
+ goto error;
p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
i_read / 2 );
#endif
- MP4_READBOX_EXIT( 1 );
+ code = 1;
+error:
+ MP4_READBOX_EXIT( code );
}
static void MP4_FreeBox_padb( MP4_Box_t *p_box )
MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
p_box->data.p_elst->i_segment_duration =
- calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
+ calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
p_box->data.p_elst->i_media_time =
- calloc( sizeof( int64_t ), p_box->data.p_elst->i_entry_count );
+ calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
p_box->data.p_elst->i_media_rate_integer =
- calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
+ calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
p_box->data.p_elst->i_media_rate_fraction=
- calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
+ calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
}
#ifdef MP4_VERBOSE
- msg_Dbg( p_stream, "read box: \"elst\" entry-count "I64Fd, p_box->data.p_elst->i_entry_count );
+ msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
+ (unsigned long)p_box->data.p_elst->i_entry_count );
#endif
MP4_READBOX_EXIT( 1 );
}
MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
MP4_GET4BYTES( i_len );
+ i_len++;
+
if( i_len > 0 )
{
uint32_t i;
- p_box->data.p_rdrf->psz_ref = malloc( i_len + 1);
+ p_box->data.p_rdrf->psz_ref = malloc( i_len );
+ i_len--;
+
for( i = 0; i < i_len; i++ )
{
MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
#ifdef MP4_VERBOSE
msg_Dbg( p_stream,
- "read box: \"rdrf\" type:%4.4s ref %s",
- (char*)&p_box->data.p_rdrf->i_ref_type,
- p_box->data.p_rdrf->psz_ref );
-
+ "read box: \"rdrf\" type:%4.4s ref %s",
+ (char*)&p_box->data.p_rdrf->i_ref_type,
+ p_box->data.p_rdrf->psz_ref );
#endif
MP4_READBOX_EXIT( 1 );
}
static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
{
- FREENULL( p_box->data.p_rdrf->psz_ref )
+ FREENULL( p_box->data.p_rdrf->psz_ref );
}
if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
{
- int i_ret;
- if( config_GetInt( p_stream, "france" ) )
- {
- i_ret = -7;
- }
- else
- {
- i_ret= drms_init( p_drms_box->data.p_sample_soun->p_drms,
+ int i_ret = drms_init( p_drms_box->data.p_sample_soun->p_drms,
p_box->i_type, p_peek, i_read );
- }
if( i_ret )
{
const char *psz_error;
case -4: psz_error = "could not get SCI data"; break;
case -5: psz_error = "no user key found in SCI data"; break;
case -6: psz_error = "invalid user key"; break;
- case -7: psz_error = "you live in France"; break;
default: psz_error = "unknown error"; break;
}
-
- msg_Err( p_stream, "drms_init(%4.4s) failed (%s)",
- (char *)&p_box->i_type, psz_error );
+ if MP4_BOX_TYPE_ASCII()
+ msg_Err( p_stream, "drms_init(%4.4s) failed (%s)",
+ (char *)&p_box->i_type, psz_error );
+ else
+ msg_Err( p_stream, "drms_init(c%3.3s) failed (%s)",
+ (char *)&p_box->i_type+1, psz_error );
drms_free( p_drms_box->data.p_sample_soun->p_drms );
p_drms_box->data.p_sample_soun->p_drms = NULL;
static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
{
- uint16_t i_length, i_dummy;
+ uint16_t i16;
+ size_t i_length;
MP4_READBOX_ENTER( MP4_Box_data_0xa9xxx_t );
p_box->data.p_0xa9xxx->psz_text = NULL;
- MP4_GET2BYTES( i_length );
+ MP4_GET2BYTES( i16 );
+ i_length = i16 + 1;
if( i_length > 0 )
{
- MP4_GET2BYTES( i_dummy );
- if( i_length > i_read ) i_length = i_read;
+ MP4_GET2BYTES( i16 );
+ if( i_length >= i_read ) i_length = i_read + 1;
- p_box->data.p_0xa9xxx->psz_text = malloc( i_length + 1 );
+ p_box->data.p_0xa9xxx->psz_text = malloc( i_length );
+ i_length--;
memcpy( p_box->data.p_0xa9xxx->psz_text,
p_peek, i_length );
p_box->data.p_0xa9xxx->psz_text[i_length] = '\0';
MP4_GET4BYTES( i_version );
MP4_GET4BYTES( i_reserved );
// version should be 0, flags should be 1 for text, 0 for data
- if( i_version == 0x00000001 )
+ if( ( i_version == 0x00000001 ) && (i_data_len >= 12 ) )
{
// the rest is the text
i_data_len -= 12;
return MP4_ReadBox_sample_tx3g( p_stream, p_box );
default:
msg_Warn( p_stream,
- "unknown handler type in stsd (uncompletetly loaded)" );
+ "unknown handler type in stsd (incompletely loaded)" );
return 1;
}
}
unknown:
- msg_Warn( p_stream,
- "unknown box type %4.4s (uncompletetly loaded)",
- (char*)&p_box->i_type );
+ if MP4_BOX_TYPE_ASCII()
+ msg_Warn( p_stream,
+ "unknown box type %4.4s (incompletely loaded)",
+ (char*)&p_box->i_type );
+ else
+ msg_Warn( p_stream,
+ "unknown box type c%3.3s (incompletely loaded)",
+ (char*)&p_box->i_type+1 );
return 1;
}
/**** "Higher level" Functions ****/
/**** ------------------------------------------------------------------- ****/
-static struct
+static const struct
{
uint32_t i_type;
int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
{
- free( p_box );
+ MP4_BoxFree( p_stream, p_box );
return NULL;
}
if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
{
/* Should not happen */
- msg_Warn( s,
- "cannot free box %4.4s, type unknown",
- (char*)&p_box->i_type );
+ if MP4_BOX_TYPE_ASCII()
+ msg_Warn( s,
+ "cannot free box %4.4s, type unknown",
+ (char*)&p_box->i_type );
+ else
+ msg_Warn( s,
+ "cannot free box c%3.3s, type unknown",
+ (char*)&p_box->i_type+1 );
}
else
{
if( !i_level )
{
- msg_Dbg( s, "dumping root Box \"%4.4s\"",
- (char*)&p_box->i_type );
+ if MP4_BOX_TYPE_ASCII()
+ msg_Dbg( s, "dumping root Box \"%4.4s\"",
+ (char*)&p_box->i_type );
+ else
+ msg_Dbg( s, "dumping root Box \"c%3.3s\"",
+ (char*)&p_box->i_type+1 );
}
else
{
{
str[i*5] = '|';
}
- sprintf( str + i_level * 5, "+ %4.4s size %d",
- (char*)&p_box->i_type,
- (uint32_t)p_box->i_size );
-
+ if MP4_BOX_TYPE_ASCII()
+ sprintf( str + i_level * 5, "+ %4.4s size %d",
+ (char*)&p_box->i_type, (uint32_t)p_box->i_size );
+ else
+ sprintf( str + i_level * 5, "+ c%3.3s size %d",
+ (char*)&p_box->i_type+1, (uint32_t)p_box->i_size );
msg_Dbg( s, "%s", str );
}
p_child = p_box->p_first;
return;
}
- vasprintf( &psz_path, psz_fmt, args );
+ if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
+ psz_path = NULL;
if( !psz_path || !psz_path[0] )
{