+/* Chapter support */
+static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
+{
+ MP4_Box_data_chpl_t *p_chpl;
+ uint32_t i_dummy;
+ int i;
+ MP4_READBOX_ENTER( MP4_Box_data_chpl_t );
+
+ p_chpl = p_box->data.p_chpl;
+
+ MP4_GETVERSIONFLAGS( p_chpl );
+
+ MP4_GET4BYTES( i_dummy );
+
+ MP4_GET1BYTE( p_chpl->i_chapter );
+
+ for( i = 0; i < p_chpl->i_chapter; i++ )
+ {
+ uint64_t i_start;
+ uint8_t i_len;
+ int i_copy;
+ MP4_GET8BYTES( i_start );
+ MP4_GET1BYTE( i_len );
+
+ p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
+ i_copy = __MIN( i_len, i_read );
+ if( i_copy > 0 )
+ memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
+ p_chpl->chapter[i].psz_name[i_copy] = '\0';
+ p_chpl->chapter[i].i_start = i_start;
+
+ p_peek += i_copy;
+ i_read -= i_copy;
+ }
+ /* Bubble sort by increasing start date */
+ do
+ {
+ for( i = 0; i < p_chpl->i_chapter - 1; i++ )
+ {
+ if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
+ {
+ char *psz = p_chpl->chapter[i+1].psz_name;
+ int64_t i64 = p_chpl->chapter[i+1].i_start;
+
+ p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
+ p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
+
+ p_chpl->chapter[i].psz_name = psz;
+ p_chpl->chapter[i].i_start = i64;
+
+ i = -1;
+ break;
+ }
+ }
+ } while( i == -1 );
+
+#ifdef MP4_VERBOSE
+ msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
+ p_chpl->i_chapter );
+#endif
+ MP4_READBOX_EXIT( 1 );
+}
+static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
+{
+ MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
+ int i;
+ for( i = 0; i < p_chpl->i_chapter; i++ )
+ free( p_chpl->chapter[i].psz_name );
+}
+
+static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
+{
+ unsigned int i;
+ MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
+
+ p_box->data.p_tref_generic->i_track_ID = NULL;
+ p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
+ if( p_box->data.p_tref_generic->i_entry_count > 0 )
+ p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
+ if( p_box->data.p_tref_generic->i_track_ID == NULL )
+ MP4_READBOX_EXIT( 0 );
+
+ for( i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
+ {
+ MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
+ }
+#ifdef MP4_VERBOSE
+ msg_Dbg( p_stream, "read box: \"chap\" %d references",
+ p_box->data.p_tref_generic->i_entry_count );
+#endif
+
+ MP4_READBOX_EXIT( 1 );
+}
+static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
+{
+ FREENULL( p_box->data.p_tref_generic->i_track_ID );
+}
+
+static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
+{
+ uint8_t meta_data[8];
+ int i_actually_read;
+
+ // skip over box header
+ i_actually_read = stream_Read( p_stream, meta_data, 8 );
+ if( i_actually_read < 8 )
+ return 0;
+
+ /* meta content starts with a 4 byte version/flags value (should be 0) */
+ i_actually_read = stream_Read( p_stream, meta_data, 4 );
+ if( i_actually_read < 4 )
+ return 0;
+
+ /* then it behaves like a container */
+ return MP4_ReadBoxContainerRaw( p_stream, p_box );
+}
+