+ /* Find ctts
+ * Gives the delta between decoding time (dts) and composition table (pts)
+ */
+ p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
+ if( p_box )
+ {
+ MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
+
+ msg_Warn( p_demux, "CTTS table" );
+
+ /* Create pts-dts table per chunk */
+ i_index = 0; i_index_sample_used = 0;
+ for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
+ {
+ mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
+ int64_t i_entry, i_sample_count, i;
+
+ /* count how many entries are needed for this chunk
+ * for p_sample_delta_dts and p_sample_count_dts */
+ i_sample_count = ck->i_sample_count;
+
+ i_entry = 0;
+ while( i_sample_count > 0 )
+ {
+ i_sample_count -= ctts->i_sample_count[i_index+i_entry];
+
+ /* don't count already used sample in this entry */
+ if( i_entry == 0 )
+ i_sample_count += i_index_sample_used;
+
+ i_entry++;
+ }
+
+ /* allocate them */
+ ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
+ ck->p_sample_offset_pts = calloc( i_entry, sizeof( uint32_t ) );
+
+ /* now copy */
+ i_sample_count = ck->i_sample_count;
+ for( i = 0; i < i_entry; i++ )
+ {
+ int64_t i_used;
+ int64_t i_rest;
+
+ i_rest = ctts->i_sample_count[i_index] -
+ i_index_sample_used;
+
+ i_used = __MIN( i_rest, i_sample_count );
+
+ i_index_sample_used += i_used;
+ i_sample_count -= i_used;
+
+ ck->p_sample_count_pts[i] = i_used;
+ ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
+
+ if( i_index_sample_used >= ctts->i_sample_count[i_index] )
+ {
+ i_index++;
+ i_index_sample_used = 0;
+ }
+ }
+ }
+ }
+