Commit 70ee5fbf authored by Laurent Aimar's avatar Laurent Aimar

* block: added

    - BLOCK_FLAG_CORRUPTED : signal corrupted data (do not use anymore
 BLOCK_FLAG_DISCONTINUITY in that case)
    - BLOCK_FLAG_PREROLL : mark this block to be decoded (no matter what).
 * ffmpeg, libmpeg2: support BLOCK_FLAG_PREROLL (ie disable frame dropping).
 * input: added ES_OUT_SET_NEXT_DISPLAY_TIME to ease the work for preroll
 (untested).
 * mp4: added support for CTTS table (pts, needed for h264+bframe).
 * decoders: for now handle discontinuity and corrupted block the same way.
parent 26b8ba09
...@@ -68,6 +68,10 @@ typedef struct block_sys_t block_sys_t; ...@@ -68,6 +68,10 @@ typedef struct block_sys_t block_sys_t;
#define BLOCK_FLAG_CLOCK 0x0200 #define BLOCK_FLAG_CLOCK 0x0200
/** This block is scrambled */ /** This block is scrambled */
#define BLOCK_FLAG_SCRAMBLED 0x0400 #define BLOCK_FLAG_SCRAMBLED 0x0400
/** This block has to be decoded but not be displayed */
#define BLOCK_FLAG_PREROLL 0x0800
/** This block is corrupted and/or there is data loss */
#define BLOCK_FLAG_CORRUPTED 0x1000
#define BLOCK_FLAG_PRIVATE_MASK 0xffff0000 #define BLOCK_FLAG_PRIVATE_MASK 0xffff0000
#define BLOCK_FLAG_PRIVATE_SHIFT 16 #define BLOCK_FLAG_PRIVATE_SHIFT 16
......
...@@ -59,7 +59,12 @@ enum es_out_query_e ...@@ -59,7 +59,12 @@ enum es_out_query_e
ES_OUT_SET_GROUP, /* arg1= int */ ES_OUT_SET_GROUP, /* arg1= int */
ES_OUT_GET_GROUP, /* arg1= int* */ ES_OUT_GET_GROUP, /* arg1= int* */
/* PCR handling, dts/pts will be automatically computed using thoses PCR */ /* PCR handling, dts/pts will be automatically computed using thoses PCR
* XXX: SET_PCR(_GROUP) is in charge of the pace control. They will wait to slow
* down the demuxer to read at the right speed.
* XXX: if you want PREROLL just call RESET_PCR and ES_OUT_SET_NEXT_DISPLAY_TIME and send
* data to the decoder *without* calling SET_PCR until preroll is finished.
*/
ES_OUT_SET_PCR, /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/ ES_OUT_SET_PCR, /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
ES_OUT_SET_GROUP_PCR, /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/ ES_OUT_SET_GROUP_PCR, /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
ES_OUT_RESET_PCR, /* no arg */ ES_OUT_RESET_PCR, /* no arg */
...@@ -69,7 +74,10 @@ enum es_out_query_e ...@@ -69,7 +74,10 @@ enum es_out_query_e
ES_OUT_GET_TS, /* arg1=int64_t i_ts(microsecond!) (using default group 0), arg2=int64_t* converted i_ts */ ES_OUT_GET_TS, /* arg1=int64_t i_ts(microsecond!) (using default group 0), arg2=int64_t* converted i_ts */
/* Try not to use this one as it is a bit hacky */ /* Try not to use this one as it is a bit hacky */
ES_OUT_SET_FMT /* arg1= es_out_id_t* arg2=es_format_t* */ ES_OUT_SET_FMT, /* arg1= es_out_id_t* arg2=es_format_t* */
/* Allow preroll of data (data with dts/pts < i_pts for one ES will be decoded but not displayed */
ES_OUT_SET_NEXT_DISPLAY_TIME, /* arg1=es_out_id_t* arg2=int64_t i_pts(microsecond) */
}; };
struct es_out_t struct es_out_t
......
...@@ -174,7 +174,7 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -174,7 +174,7 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY ) if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{ {
p_sys->i_state = STATE_NOSYNC; p_sys->i_state = STATE_NOSYNC;
} }
......
...@@ -175,7 +175,7 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -175,7 +175,7 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY ) if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{ {
p_sys->i_state = STATE_NOSYNC; p_sys->i_state = STATE_NOSYNC;
} }
......
...@@ -181,7 +181,7 @@ static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -181,7 +181,7 @@ static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
p_block = *pp_block; p_block = *pp_block;
if( p_block->i_flags&BLOCK_FLAG_DISCONTINUITY ) if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{ {
block_Release( p_block ); block_Release( p_block );
return NULL; return NULL;
......
...@@ -209,7 +209,7 @@ aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block ) ...@@ -209,7 +209,7 @@ aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
if( p_block->i_buffer <= 0 || p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) if( p_block->i_buffer <= 0 || ( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) )
{ {
block_Release( p_block ); block_Release( p_block );
return NULL; return NULL;
......
...@@ -438,7 +438,7 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block ) ...@@ -438,7 +438,7 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
p_block = *pp_block; p_block = *pp_block;
if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{ {
p_sys->i_buffer = 0; p_sys->i_buffer = 0;
p_sys->i_pts = 0; /* To make sure we recover properly */ p_sys->i_pts = 0; /* To make sure we recover properly */
...@@ -450,6 +450,14 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block ) ...@@ -450,6 +450,14 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
if( p_block->i_flags & BLOCK_FLAG_PREROLL )
{
/* Do not care about late frames when prerolling
* TODO avoid decoding of non reference frame
* (ie all B except for H264 where it depends only on nal_ref_idc) */
p_sys->i_late_frames = 0;
}
if( !p_dec->b_pace_control && p_sys->i_late_frames > 0 && if( !p_dec->b_pace_control && p_sys->i_late_frames > 0 &&
mdate() - p_sys->i_late_frames_start > I64C(5000000) ) mdate() - p_sys->i_late_frames_start > I64C(5000000) )
{ {
...@@ -579,8 +587,9 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block ) ...@@ -579,8 +587,9 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
continue; continue;
} }
/* Update frame late count*/ /* Update frame late count (except when doing preroll) */
if( p_sys->i_pts && p_sys->i_pts <= mdate() ) if( p_sys->i_pts && p_sys->i_pts <= mdate() &&
!(p_block->i_flags & BLOCK_FLAG_PREROLL) )
{ {
p_sys->i_late_frames++; p_sys->i_late_frames++;
if( p_sys->i_late_frames == 1 ) if( p_sys->i_late_frames == 1 )
...@@ -630,6 +639,10 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block ) ...@@ -630,6 +639,10 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
{ {
p_pic->date = p_sys->i_pts; p_pic->date = p_sys->i_pts;
static int64_t i_old = 0;
msg_Dbg( p_dec, "pts=%lld diff=%lld", p_pic->date, p_pic->date - i_old );
i_old = p_pic->date;
/* interpolate the next PTS */ /* interpolate the next PTS */
if( p_sys->p_context->frame_rate > 0 ) if( p_sys->p_context->frame_rate > 0 )
{ {
...@@ -657,6 +670,7 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block ) ...@@ -657,6 +670,7 @@ picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
else else
{ {
p_dec->pf_vout_buffer_del( p_dec, p_pic ); p_dec->pf_vout_buffer_del( p_dec, p_pic );
msg_Dbg( p_dec, "pts=0" );
} }
} }
......
...@@ -295,7 +295,7 @@ static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -295,7 +295,7 @@ static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
aout_DateSet( &p_sys->end_date, (*pp_block)->i_pts ); aout_DateSet( &p_sys->end_date, (*pp_block)->i_pts );
} }
if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY ) if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{ {
p_sys->i_state = STATE_NOSYNC; p_sys->i_state = STATE_NOSYNC;
} }
......
...@@ -65,6 +65,8 @@ struct decoder_sys_t ...@@ -65,6 +65,8 @@ struct decoder_sys_t
* the sequence header ? */ * the sequence header ? */
vlc_bool_t b_slice_i; /* intra-slice refresh stream */ vlc_bool_t b_slice_i; /* intra-slice refresh stream */
vlc_bool_t b_preroll;
/* /*
* Output properties * Output properties
*/ */
...@@ -137,6 +139,7 @@ static int OpenDecoder( vlc_object_t *p_this ) ...@@ -137,6 +139,7 @@ static int OpenDecoder( vlc_object_t *p_this )
p_sys->b_garbage_pic = 0; p_sys->b_garbage_pic = 0;
p_sys->b_slice_i = 0; p_sys->b_slice_i = 0;
p_sys->b_skip = 0; p_sys->b_skip = 0;
p_sys->b_preroll = VLC_FALSE;
#if defined( __i386__ ) #if defined( __i386__ )
if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX ) if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
...@@ -213,7 +216,7 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -213,7 +216,7 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) && if( (p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) &&
p_sys->p_synchro && p_sys->p_synchro &&
p_sys->p_info->sequence && p_sys->p_info->sequence &&
p_sys->p_info->sequence->width != (unsigned)-1 ) p_sys->p_info->sequence->width != (unsigned)-1 )
...@@ -244,6 +247,17 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -244,6 +247,17 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
} }
} }
if( p_block->i_flags & BLOCK_FLAG_PREROLL )
{
p_sys->b_preroll = VLC_TRUE;
}
else if( p_sys->b_preroll )
{
p_sys->b_preroll = VLC_FALSE;
/* Reset synchro */
vout_SynchroReset( p_sys->p_synchro );
}
#ifdef PIC_FLAG_PTS #ifdef PIC_FLAG_PTS
if( p_block->i_pts ) if( p_block->i_pts )
{ {
...@@ -432,7 +446,7 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -432,7 +446,7 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
p_sys->p_info->current_picture->nb_fields, i_pts, i_dts, p_sys->p_info->current_picture->nb_fields, i_pts, i_dts,
p_sys->i_current_rate ); p_sys->i_current_rate );
if( !p_dec->b_pace_control && if( !p_dec->b_pace_control && !p_sys->b_preroll &&
!(p_sys->b_slice_i !(p_sys->b_slice_i
&& ((p_sys->p_info->current_picture->flags && ((p_sys->p_info->current_picture->flags
& PIC_MASK_CODING_TYPE) == P_CODING_TYPE)) & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
......
...@@ -196,7 +196,7 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) ...@@ -196,7 +196,7 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY ) if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{ {
p_sys->i_state = STATE_NOSYNC; p_sys->i_state = STATE_NOSYNC;
} }
......
...@@ -386,7 +386,7 @@ static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket, ...@@ -386,7 +386,7 @@ static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
block_t *p_block = *pp_block; block_t *p_block = *pp_block;
void *p_buf; void *p_buf;
if( ( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) != 0 ) if( ( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) != 0 )
{ {
/* Don't send the the first packet after a discontinuity to /* Don't send the the first packet after a discontinuity to
* theora_decode, otherwise we get purple/green display artifacts * theora_decode, otherwise we get purple/green display artifacts
......
...@@ -76,6 +76,9 @@ typedef struct ...@@ -76,6 +76,9 @@ typedef struct
uint32_t *p_sample_count_dts; uint32_t *p_sample_count_dts;
uint32_t *p_sample_delta_dts; /* dts delta */ uint32_t *p_sample_delta_dts; /* dts delta */
uint32_t *p_sample_count_pts;
uint32_t *p_sample_offset_pts; /* pts-dts */
/* TODO if needed add pts /* TODO if needed add pts
but quickly *add* support for edts and seeking */ but quickly *add* support for edts and seeking */
...@@ -162,7 +165,7 @@ static int MP4_TrackNextSample( demux_t *, mp4_track_t * ); ...@@ -162,7 +165,7 @@ static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t ); static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
/* Return time in s of a track */ /* Return time in s of a track */
static inline int64_t MP4_TrackGetPTS( demux_t *p_demux, mp4_track_t *p_track ) static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
{ {
#define chunk p_track->chunk[p_track->i_chunk] #define chunk p_track->chunk[p_track->i_chunk]
...@@ -213,6 +216,24 @@ static inline int64_t MP4_TrackGetPTS( demux_t *p_demux, mp4_track_t *p_track ) ...@@ -213,6 +216,24 @@ static inline int64_t MP4_TrackGetPTS( demux_t *p_demux, mp4_track_t *p_track )
return I64C(1000000) * i_dts / p_track->i_timescale; return I64C(1000000) * i_dts / p_track->i_timescale;
} }
static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
{
mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
unsigned int i_index = 0;
unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
return -1;
for( i_index = 0;; i_index++ )
{
if( i_sample < ck->p_sample_count_pts[i_index] )
return ck->p_sample_offset_pts[i_index] * I64C(1000000) / p_track->i_timescale;
i_sample -= ck->p_sample_count_pts[i_index];
}
}
static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys ) static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
{ {
return I64C(1000000) * p_sys->i_time / p_sys->i_timescale; return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
...@@ -604,17 +625,18 @@ static int Demux( demux_t *p_demux ) ...@@ -604,17 +625,18 @@ static int Demux( demux_t *p_demux )
continue; continue;
} }
while( MP4_TrackGetPTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) ) while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
{ {
#if 0 #if 0
msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track, msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
MP4_TrackGetPTS( p_demux, tk ), MP4_TrackGetDTS( p_demux, tk ),
MP4_GetMoviePTS( p_sys ) ); MP4_GetMoviePTS( p_sys ) );
#endif #endif
if( MP4_TrackSampleSize( tk ) > 0 ) if( MP4_TrackSampleSize( tk ) > 0 )
{ {
block_t *p_block; block_t *p_block;
int64_t i_delta;
/* go,go go ! */ /* go,go go ! */
if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) ) if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
...@@ -669,16 +691,23 @@ static int Demux( demux_t *p_demux ) ...@@ -669,16 +691,23 @@ static int Demux( demux_t *p_demux )
} }
} }
} }
p_block->i_dts = MP4_TrackGetPTS( p_demux, tk ) + 1; /* dts */
p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
/* pts */
i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
if( i_delta >= 0 )
p_block->i_pts = p_block->i_dts + i_delta;
else if( tk->fmt.i_cat != VIDEO_ES )
p_block->i_pts = p_block->i_dts;
else
p_block->i_pts = 0;
p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? if( tk->fmt.i_cat == VIDEO_ES )
0 : p_block->i_dts + 1; msg_Dbg( p_demux, "pts=%lld dts=%lld", p_block->i_pts, p_block->i_dts );
if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) ) if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
{
es_out_Send( p_demux->out, tk->p_es, p_block ); es_out_Send( p_demux->out, tk->p_es, p_block );
} }
}
/* Next sample */ /* Next sample */
if( MP4_TrackNextSample( p_demux, tk ) ) if( MP4_TrackNextSample( p_demux, tk ) )
...@@ -918,8 +947,15 @@ static int TrackCreateChunksIndex( demux_t *p_demux, ...@@ -918,8 +947,15 @@ static int TrackCreateChunksIndex( demux_t *p_demux,
/* first we read chunk offset */ /* first we read chunk offset */
for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ ) for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
{ {
p_demux_track->chunk[i_chunk].i_offset = mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
p_co64->data.p_co64->i_chunk_offset[i_chunk];
ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
ck->i_first_dts = 0;
ck->p_sample_count_dts = NULL;
ck->p_sample_delta_dts = NULL;
ck->p_sample_count_pts = NULL;
ck->p_sample_offset_pts = NULL;
} }
/* now we read index for SampleEntry( soun vide mp4a mp4v ...) /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
...@@ -963,12 +999,9 @@ static int TrackCreateChunksIndex( demux_t *p_demux, ...@@ -963,12 +999,9 @@ static int TrackCreateChunksIndex( demux_t *p_demux,
static int TrackCreateSamplesIndex( demux_t *p_demux, static int TrackCreateSamplesIndex( demux_t *p_demux,
mp4_track_t *p_demux_track ) mp4_track_t *p_demux_track )
{ {
MP4_Box_t *p_stts; /* makes mapping between sample and decoding time, MP4_Box_t *p_box;
ctts make same mapping but for composition time, MP4_Box_data_stsz_t *stsz;
not yet used and probably not usefull */ MP4_Box_data_stts_t *stts;
MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
that uses a compressed form FIXME make them in libmp4
as a unique type */
/* TODO use also stss and stsh table for seeking */ /* TODO use also stss and stsh table for seeking */
/* FIXME use edit table */ /* FIXME use edit table */
int64_t i_sample; int64_t i_sample;
...@@ -979,23 +1012,35 @@ static int TrackCreateSamplesIndex( demux_t *p_demux, ...@@ -979,23 +1012,35 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
int64_t i_last_dts; int64_t i_last_dts;
p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" ); /* Find stsz
p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */ * Gives the sample size for each samples. There is also a stz2 table
* (compressed form) that we need to implement TODO */
if( ( !p_stts )||( !p_stsz ) ) p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
if( !p_box )
{ {
msg_Warn( p_demux, "cannot read sample table" ); /* FIXME and stz2 */
return( VLC_EGENERIC ); msg_Warn( p_demux, "cannot find STSZ box" );
return VLC_EGENERIC;
} }
stsz = p_box->data.p_stsz;
p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count; /* Find stts
* Gives mapping between sample and decoding time
*/
p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
if( !p_box )
{
msg_Warn( p_demux, "cannot find STTS box" );
return VLC_EGENERIC;
}
stts = p_box->data.p_stts;
/* for sample size, there are 2 case */ /* Use stsz table to create a sample number -> sample size table */
if( p_stsz->data.p_stsz->i_sample_size ) p_demux_track->i_sample_count = stsz->i_sample_count;
if( stsz->i_sample_size )
{ {
/* 1: all sample have the same size, so no need to construct a table */ /* 1: all sample have the same size, so no need to construct a table */
p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size; p_demux_track->i_sample_size = stsz->i_sample_size;
p_demux_track->p_sample_size = NULL; p_demux_track->p_sample_size = NULL;
} }
else else
...@@ -1008,58 +1053,117 @@ static int TrackCreateSamplesIndex( demux_t *p_demux, ...@@ -1008,58 +1053,117 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ ) for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
{ {
p_demux_track->p_sample_size[i_sample] = p_demux_track->p_sample_size[i_sample] =
p_stsz->data.p_stsz->i_entry_size[i_sample]; stsz->i_entry_size[i_sample];
} }
} }
/* we have extracted all the information from stsz, now use stts */
/* if we don't want to waste too much memory, we can't expand /* Use stts table to crate a sample number -> dts table.
the box! so each chunk will contain an "extract" of this table * XXX: if we don't want to waste too much memory, we can't expand
for fast research */ * the box! so each chunk will contain an "extract" of this table
* for fast research (problem with raw stream where a sample is sometime
* just channels*bits_per_sample/8 */
i_last_dts = 0; i_last_dts = 0;
i_index = 0; i_index_sample_used = 0; i_index = 0; i_index_sample_used = 0;
/* create and init last data for each chunk */
for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ ) 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; int64_t i_entry, i_sample_count, i;
/* save last dts */ /* save last dts */
p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts; ck->i_first_dts = i_last_dts;
/* count how many entries are needed for this chunk /* count how many entries are needed for this chunk
* for p_sample_delta_dts and p_sample_count_dts */ * for p_sample_delta_dts and p_sample_count_dts */
i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count; i_sample_count = ck->i_sample_count;
i_entry = 0; i_entry = 0;
while( i_sample_count > 0 ) while( i_sample_count > 0 )
{ {
i_sample_count -= i_sample_count -= stts->i_sample_count[i_index+i_entry];
p_stts->data.p_stts->i_sample_count[i_index+i_entry]; /* don't count already used sample in this entry */
if( i_entry == 0 ) if( i_entry == 0 )
i_sample_count += i_index_sample_used;
i_entry++;
}
/* allocate them */
ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
ck->p_sample_delta_dts = 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 = stts->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_dts[i] = i_used;
ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
i_last_dts += i_used * ck->p_sample_delta_dts[i];
if( i_index_sample_used >= stts->i_sample_count[i_index] )
{
i_index++;
i_index_sample_used = 0;
}
}
}
/* 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 */ /* don't count already used sample in this entry */
if( i_entry == 0 )
i_sample_count += i_index_sample_used; i_sample_count += i_index_sample_used;
}
i_entry++; i_entry++;
} }
/* allocate them */ /* allocate them */
p_demux_track->chunk[i_chunk].p_sample_count_dts = ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
calloc( i_entry, sizeof( uint32_t ) ); ck->p_sample_offset_pts = calloc( i_entry, sizeof( uint32_t ) );
p_demux_track->chunk[i_chunk].p_sample_delta_dts =
calloc( i_entry, sizeof( uint32_t ) );
/* now copy */ /* now copy */
i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count; i_sample_count = ck->i_sample_count;
for( i = 0; i < i_entry; i++ ) for( i = 0; i < i_entry; i++ )
{ {
int64_t i_used; int64_t i_used;
int64_t i_rest; int64_t i_rest;