[vlc-devel] [PATCH 3/4] demux/mp4: Add fragmented MP4 support

Frederic YHUEL fyhuel at viotech.net
Wed Jul 4 17:49:08 CEST 2012


On Mon, Jun 25, 2012 at 1:21 PM, Frédéric Yhuel <fyhuel at viotech.net> wrote:
> TODO: add support for multiplexed content
> (only video track is played for now).
>
> Test it:
>
> ***** DASH *****
> - http://www-itec.uni-klu.ac.at/ftp/datasets/mmsys12/ElephantsDream/MPDs/ElephantsDream_15s_isoffmain_DIS_23009_1_v_2_1c2_2011_08_30.mpd
> (you must set "#define SEEK 1" in dash.cpp)
>
> ***** Smooth Streaming *****
> See Smooth Streaming patch message
> ---
>  modules/demux/mp4/libmp4.h |    1 +
>  modules/demux/mp4/mp4.c    |  953 ++++++++++++++++++++++++++++++++++++++++++--
>  2 files changed, 930 insertions(+), 24 deletions(-)
>
> diff --git a/modules/demux/mp4/libmp4.h b/modules/demux/mp4/libmp4.h
> index 6a06e3f..23ed220 100644
> --- a/modules/demux/mp4/libmp4.h
> +++ b/modules/demux/mp4/libmp4.h
> @@ -1345,6 +1345,7 @@ typedef struct
>
>      bool b_drms;
>      bool b_end_of_chunk;
> +    bool b_adaption;
>      void      *p_drms;
>      MP4_Box_t *p_skcr;
>
> diff --git a/modules/demux/mp4/mp4.c b/modules/demux/mp4/mp4.c
> index 0e13001..cc03c87 100644
> --- a/modules/demux/mp4/mp4.c
> +++ b/modules/demux/mp4/mp4.c
> @@ -35,6 +35,8 @@
>  #include <vlc_charset.h>                           /* EnsureUTF8 */
>  #include <vlc_meta.h>                              /* vlc_meta_t, vlc_meta_ */
>  #include <vlc_input.h>
> +#include <assert.h>
> +#include <vlc_modules.h>
>
>  #include "libmp4.h"
>  #include "id3genres.h"                             /* for ATOM_gnre */
> @@ -59,6 +61,7 @@ vlc_module_end ()
>   *****************************************************************************/
>  static int   Demux   ( demux_t * );
>  static int   DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
> +static int   DemuxFrg( demux_t * );
>  static int   Seek    ( demux_t *, mtime_t );
>  static int   Control ( demux_t *, int, va_list );
>
> @@ -76,6 +79,9 @@ struct demux_sys_t
>      mp4_track_t  *track;         /* array of track */
>      float        f_fps;          /* number of frame per seconds */
>
> +    bool         b_smooth;       /* Smooth Streaming => no moov box */
> +    bool         b_fragmented;   /* fMP4 */
> +
>      /* */
>      MP4_Box_t    *p_tref_chap;
>
> @@ -87,6 +93,7 @@ struct demux_sys_t
>   * Declaration of local function
>   *****************************************************************************/
>  static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t  *, bool b_force_enable );
> +static int sms_TrackCreate( demux_t *, mp4_track_t *, MP4_Box_t *);
>  static void MP4_TrackDestroy(  mp4_track_t * );
>
>  static int  MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
> @@ -105,7 +112,12 @@ static const char *MP4_ConvertMacCode( uint16_t );
>  /* Return time in s of a 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]
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    mp4_chunk_t chunk;
> +    if( p_sys->b_fragmented )
> +        chunk = *p_track->cchunk;
> +    else
> +        chunk = p_track->chunk[p_track->i_chunk];
>
>      unsigned int i_index = 0;
>      unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
> @@ -127,8 +139,6 @@ static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
>          }
>      }
>
> -#undef chunk
> -
>      /* now handle elst */
>      if( p_track->p_elst )
>      {
> @@ -153,9 +163,15 @@ static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
>      return INT64_C(1000000) * i_dts / p_track->i_timescale;
>  }
>
> -static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
> +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];
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    mp4_chunk_t *ck;
> +    if( p_sys->b_fragmented )
> +        ck = p_track->cchunk;
> +    else
> +        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;
>
> @@ -179,6 +195,107 @@ static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
>
>  static void LoadChapter( demux_t  *p_demux );
>
> +static int LoadInitFrag( demux_t *p_demux )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    if( p_sys->b_smooth ) /* Smooth Streaming */
> +    {
> +        if( ( p_sys->p_root = MP4_BoxGetSmooBox( p_demux->s ) ) == NULL )
> +        {
> +            goto LoadInitFragError;
> +        }
> +        else
> +        {
> +            MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
> +            if( !p_smoo || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
> +                goto LoadInitFragError;
> +            /* Get number of tracks */
> +            p_sys->i_tracks = 0;
> +            for( int i = 0; i < 3; i++ )
> +            {
> +                MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
> +                if( p_stra && p_stra->data.p_stra->i_track_ID )
> +                    p_sys->i_tracks++;
> +                /* Get timescale and duration of the video track; */
> +                if( i == 0 )
> +                {
> +                    p_sys->i_timescale = p_stra->data.p_stra->i_timescale;
> +                    p_sys->i_duration = p_stra->data.p_stra->i_duration;
> +                }
> +            }
> +        }
> +    }
> +    else if( p_sys->b_fragmented ) /* DASH */
> +    {
> +        /* Load moov box */
> +        if( ( p_sys->p_root = MP4_BoxGetInitFrag( p_demux->s ) ) == NULL )
> +        {
> +            goto LoadInitFragError;
> +        }
> +    }
> +    else
> +    {
> +        /* Load all boxes ( except raw data ) */
> +        if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
> +        {
> +            goto LoadInitFragError;
> +        }
> +    }
> +    return VLC_SUCCESS;
> +
> +LoadInitFragError:
> +    msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
> +    return VLC_EGENERIC;
> +}
> +
> +static int AllocateMemory( demux_t *p_demux )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
> +    if( p_sys->track == NULL )
> +        return VLC_EGENERIC;
> +
> +    if( p_sys->b_fragmented )
> +    {
> +        mp4_track_t *p_track;
> +        for( uint16_t i = 0; i < p_sys->i_tracks; i++ )
> +        {
> +            p_track = &p_sys->track[i];
> +            p_track->cchunk = calloc( 1, sizeof( mp4_chunk_t ) );
> +            if( unlikely( !p_track->cchunk ) )
> +            {
> +                free( p_sys->track );
> +                return VLC_EGENERIC;
> +            }
> +        }
> +    }
> +    return VLC_SUCCESS;
> +}
> +
> +static void CreateTracksFromSmooBox( demux_t *p_demux )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
> +    mp4_track_t *p_track;
> +    int j = 0;
> +    for( int i = 0; i < 3; i++ )
> +    {
> +        MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
> +        if( !p_stra || p_stra->data.p_stra->i_track_ID == 0 )
> +            continue;
> +        else
> +        {
> +            p_track = &p_sys->track[j]; j++;
> +            p_track->b_end_of_chunk = true;
> +            sms_TrackCreate( p_demux, p_track, p_stra );
> +            p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
> +        }
> +    }
> +}
> +
>  /*****************************************************************************
>   * Open: check file and initializes MP4 structures
>   *****************************************************************************/
> @@ -211,6 +328,7 @@ static int Open( vlc_object_t * p_this )
>          case ATOM_free:
>          case ATOM_skip:
>          case ATOM_wide:
> +        case ATOM_uuid:
>          case VLC_FOURCC( 'p', 'n', 'o', 't' ):
>              break;
>          case ATOM_ftyp:
> @@ -223,7 +341,7 @@ static int Open( vlc_object_t * p_this )
>      }
>
>      /* I need to seek */
> -    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
> +    stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
>      if( !b_seekable )
>      {
>          msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
> @@ -237,11 +355,33 @@ static int Open( vlc_object_t * p_this )
>      /* create our structure that will contains all data */
>      p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
>
> -    /* Now load all boxes ( except raw data ) */
> -    if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
> +    /* Is it smooth streaming or DASH ? */
> +    char *parent_name = NULL;
> +    if( p_demux->s->p_source && p_demux->s->p_source->p_module )
> +        parent_name = (char *)module_get_name( p_demux->s->p_source->p_module, false );
> +    if( parent_name && !strcmp( parent_name, "Smooth Streaming" ) )
> +    {
> +        p_sys->b_smooth = true;
> +        p_sys->b_fragmented = true;
> +    }
> +    else if( parent_name && !strcmp( parent_name, "DASH" ) )
>      {
> -        msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
> +        p_sys->b_fragmented = true;
> +    }
> +    if( p_sys->b_fragmented )
> +    {
> +        p_demux->pf_demux = DemuxFrg;
> +    }
> +
> +    if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
>          goto error;
> +
> +    if( p_sys->b_smooth )
> +    {
> +        if( AllocateMemory( p_demux ) != VLC_SUCCESS )
> +            goto error;
> +        CreateTracksFromSmooBox( p_demux );
> +        return VLC_SUCCESS;
>      }
>
>      MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
> @@ -292,13 +432,8 @@ static int Open( vlc_object_t * p_this )
>
>          if( !p_foov )
>          {
> -            /* search also for moof box used by smoothstreaming */
> -            p_foov = MP4_BoxGet( p_sys->p_root, "/moof" );
> -            if( !p_foov )
> -            {
> -                msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
> -                goto error;
> -            }
> +            msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
> +            goto error;
>          }
>          /* we have a free box as a moov, rename it */
>          p_foov->i_type = ATOM_moov;
> @@ -415,9 +550,7 @@ static int Open( vlc_object_t * p_this )
>                          p_sys->i_tracks,
>                          p_sys->i_tracks ? 's':' ' );
>
> -    /* allocate memory */
> -    p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
> -    if( p_sys->track == NULL )
> +    if( AllocateMemory( p_demux ) != VLC_SUCCESS )
>          goto error;
>
>      /* Search the first chap reference (like quicktime) and
> @@ -443,6 +576,7 @@ static int Open( vlc_object_t * p_this )
>      {
>          p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
>          MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
> +        p_sys->track[i].b_end_of_chunk = true;
>
>          if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
>          {
> @@ -640,7 +774,7 @@ static int Demux( demux_t *p_demux )
>                  /* dts */
>                  p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
>                  /* pts */
> -                i_delta = MP4_TrackGetPTSDelta( tk );
> +                i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
>                  if( i_delta != -1 )
>                      p_block->i_pts = p_block->i_dts + i_delta;
>                  else if( tk->fmt.i_cat != VIDEO_ES )
> @@ -706,6 +840,40 @@ static int Seek( demux_t *p_demux, mtime_t i_date )
>      return VLC_SUCCESS;
>  }
>
> +static int fMP4_Seek( demux_t *p_demux, double f )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    int64_t i64 = stream_Size( p_demux->s );
> +    if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
> +    {
> +        return VLC_EGENERIC;
> +    }
> +    else
> +    {
> +        /* update global time */
> +        p_sys->i_time = (uint64_t)(f * (double)p_sys->i_duration);
> +        p_sys->i_pcr  = MP4_GetMoviePTS( p_sys );
> +
> +        for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
> +        {
> +            mp4_track_t *tk = &p_sys->track[i_track];
> +
> +            /* We don't want the current chunk to be flushed */
> +            tk->cchunk->i_sample = tk->cchunk->i_sample_count;
> +
> +            /* reset/update some values */
> +            tk->i_sample = tk->i_sample_first = 0;
> +            tk->i_first_dts = p_sys->i_time;
> +
> +            /* We want to discard the current chunk and get the next one at once */
> +            tk->b_end_of_chunk = true;
> +        }
> +        es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->i_pcr );
> +        return VLC_SUCCESS;
> +    }
> +}
> +
>  /*****************************************************************************
>   * Control:
>   *****************************************************************************/
> @@ -732,7 +900,11 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
>
>          case DEMUX_SET_POSITION:
>              f = (double)va_arg( args, double );
> -            if( p_sys->i_timescale > 0 )
> +            if( p_sys->b_fragmented )
> +            {
> +                return fMP4_Seek( p_demux, f );
> +            }
> +            else if( p_sys->i_timescale > 0 )
>              {
>                  i64 = (int64_t)( f * (double)1000000 *
>                                   (double)p_sys->i_duration /
> @@ -1024,7 +1196,7 @@ static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
>      for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
>      {
>          const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
> -        const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
> +        const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
>          const unsigned int i_size = MP4_TrackSampleSize( tk );
>
>          if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
> @@ -1096,6 +1268,10 @@ static void LoadChapter( demux_t  *p_demux )
>  static int TrackCreateChunksIndex( demux_t *p_demux,
>                                     mp4_track_t *p_demux_track )
>  {
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    if( p_sys->b_fragmented )
> +        return VLC_SUCCESS;
> +
>      MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
>      MP4_Box_t *p_stsc;
>
> @@ -1183,6 +1359,10 @@ static int TrackCreateChunksIndex( demux_t *p_demux,
>  static int TrackCreateSamplesIndex( demux_t *p_demux,
>                                      mp4_track_t *p_demux_track )
>  {
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    if( p_sys->b_fragmented )
> +        return VLC_SUCCESS;
> +
>      MP4_Box_t *p_box;
>      MP4_Box_data_stsz_t *stsz;
>      MP4_Box_data_stts_t *stts;
> @@ -1432,8 +1612,15 @@ static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
>  static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
>                            unsigned int i_chunk, es_out_id_t **pp_es )
>  {
> -    const unsigned i_sample_description_index =
> -        p_track->chunk[i_chunk].i_sample_description_index;
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    unsigned int i_sample_description_index;
> +
> +    if( p_sys->b_fragmented )
> +        i_sample_description_index = 1; /* XXX */
> +    else
> +        i_sample_description_index =
> +                p_track->chunk[i_chunk].i_sample_description_index;
> +
>      MP4_Box_t   *p_sample;
>      MP4_Box_t   *p_esds;
>      MP4_Box_t   *p_frma;
> @@ -1510,12 +1697,15 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
>          p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
>          p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
>
> +        /* TODO: Set frame rate for Smooth Streaming */
> +        if( !p_sys->b_smooth ) {
>          /* Frame rate */
>          TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
>                                &p_track->fmt.video.i_frame_rate_base,
>                                p_track, i_sample_description_index, i_chunk );
>          p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
>                                  (float)p_track->fmt.video.i_frame_rate_base;
> +        }
>
>          /* Rotation */
>          switch( (int)p_track->f_rotation ) {
> @@ -2823,3 +3013,718 @@ static const char *MP4_ConvertMacCode( uint16_t i_code )
>      }
>      return "";
>  }
> +
> +/******************************************************************************
> + *     Here are the functions used for fragmented MP4
> + *****************************************************************************/
> +
> +static int hex_digit( char c )
> +{
> +    if (c >= 'A' && c <= 'F')
> +        return c - 'A' + 10;
> +    else if (c >= 'a' && c <= 'f')
> +        return c - 'a' + 10;
> +    else if (c >= '0' && c<= '9')
> +        return c - '0';
> +    else
> +        return -1;
> +}
> +
> +static uint8_t *decode_string_hex_to_binary( const char *psz_src )
> +{
> +    int i = 0, j = 0, first_digit, second_digit;
> +    int i_len = strlen( psz_src );
> +    uint8_t *p_data = malloc ( i_len / 2 );
> +
> +    if( !p_data )
> +        return NULL;
> +
> +    while( i < i_len )
> +    {
> +        first_digit = hex_digit( psz_src[i++] );
> +        second_digit = hex_digit( psz_src[i++] );
> +        p_data[j++] = ( first_digit << 4 ) | second_digit;
> +    }
> +
> +    return p_data;
> +}
> +
> +/**
> + * Build raw avcC box (without the 8 bytes header)
> + * \return The size of the box.
> + */
> +static int build_raw_avcC( uint8_t **p_extra, const char *CodecPrivateData )
> +{
> +    uint8_t *avcC;
> +    const char *mark = "00000001";
> +    char head[9];
> +    char *pos;
> +    char tmp[512];
> +    char sps_nal_unit[512], pps_nal_unit[512];
> +
> +    strncpy( head, CodecPrivateData, 8 );
> +    head[8] = '\0';
> +    assert( !strcmp( head, mark ) );
> +    strncpy( tmp, CodecPrivateData + 8, 511 );
> +    tmp[511] = '\0';
> +    pos = strstr( tmp, mark );
> +    if( pos == NULL )
> +        return 0;
> +    strncpy( pps_nal_unit, pos + 8, 511 );
> +    pps_nal_unit[511] = '\0';
> +    size_t len = (size_t)(pos - tmp);
> +    assert( len < 512 );
> +    strncpy( sps_nal_unit, tmp, len );
> +    sps_nal_unit[len] = '\0';
> +    uint8_t *sps = decode_string_hex_to_binary( sps_nal_unit );
> +    uint8_t *pps = decode_string_hex_to_binary( pps_nal_unit );
> +
> +    uint32_t length = 8 + (strlen( CodecPrivateData ) - 16) / 2 + 11;
> +    avcC = calloc( length, 1 );
> +    if( unlikely( avcC == NULL ) )
> +        return 0;
> +
> +    /* XXX */
> +    uint8_t AVCProfileIndication = 0x64;
> +    uint8_t profile_compatibility = 0x40;
> +    uint8_t AVCLevelIndication = 0x1f;
> +    uint8_t lengthSizeMinusOne = 0x03;
> +
> +    int sps_len = strlen( sps_nal_unit ) / 2;
> +    int pps_len = strlen( pps_nal_unit ) / 2;
> +
> +    avcC[0] = 1;
> +    avcC[1] = AVCProfileIndication;
> +    avcC[2] = profile_compatibility;
> +    avcC[3] = AVCLevelIndication;
> +    avcC[4] = 0xfc + lengthSizeMinusOne;
> +    avcC[5] = 0xe0 + 1;
> +    avcC[6] = (sps_len & 0xff00)>>8;
> +    avcC[7] = sps_len & 0xff;
> +    memcpy( avcC+8, sps, sps_len );
> +
> +    avcC[8+sps_len] = 1;
> +    avcC[9+sps_len] = (pps_len & 0xff00) >> 8;
> +    avcC[10+sps_len] = pps_len & 0xff;
> +    memcpy( avcC + 11 + sps_len, pps, pps_len );
> +
> +    *p_extra = avcC;
> +    return length;
> +}
> +
> +/**
> + * Build part of esds box (actually just the CodecPrivateData).
> + * \return The size of the box.
> + */
> +static int build_raw_esds( uint8_t **p_extra, const char *CodecPrivateData )
> +{
> +    uint8_t *esds;
> +    uint8_t *codec_data = decode_string_hex_to_binary( CodecPrivateData );
> +    if( !codec_data )
> +        return 0;
> +
> +    size_t codec_data_length = strlen( CodecPrivateData ) / 2;
> +    esds = calloc( 1, codec_data_length );
> +    if( unlikely( esds == NULL ) )
> +        return 0;
> +
> +    memcpy( esds, codec_data, codec_data_length );
> +
> +    *p_extra = esds;
> +    return codec_data_length;
> +}
> +
> +/**
> + * Build a mp4_track_t from a StraBox
> + */
> +static int sms_TrackCreate( demux_t *p_demux, mp4_track_t *p_track, MP4_Box_t *p_stra )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +    MP4_Box_data_stra_t *p_data = p_stra->data.p_stra;
> +    if( !p_data )
> +        return VLC_EGENERIC;
> +
> +    p_track->b_ok       = true;
> +    p_track->b_selected = false;
> +    p_track->i_sample_count = UINT32_MAX;
> +
> +    p_track->i_timescale = p_sys->i_timescale;
> +    p_track->i_width = p_data->MaxWidth;
> +    p_track->i_height = p_data->MaxHeight;
> +    p_track->i_track_ID = p_data->i_track_ID;
> +
> +    es_format_t *fmt = &p_track->fmt;
> +    assert( fmt != NULL );
> +
> +    es_format_Init( fmt, p_data->i_es_cat, 0 );
> +
> +    /* Set language FIXME */
> +    fmt->psz_language = strdup( "en" );
> +
> +    uint8_t **p_extra = (uint8_t **)&fmt->p_extra;
> +    switch( fmt->i_cat )
> +    {
> +        case VIDEO_ES:
> +            if( p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
> +                p_data->FourCC == VLC_FOURCC( 'H', '2', '6', '4' ) )
> +            {
> +                fmt->i_extra = build_raw_avcC( p_extra, p_data->CodecPrivateData );
> +                assert( fmt->i_extra > 0 );
> +                fmt->i_codec = VLC_CODEC_H264;
> +            }
> +            else
> +            {
> +                fmt->i_extra = build_raw_esds( p_extra, p_data->CodecPrivateData );
> +                assert( fmt->i_extra > 0 );
> +                fmt->i_codec = p_data->FourCC;
> +            }
> +
> +            fmt->video.i_width = p_data->MaxWidth;
> +            fmt->video.i_height = p_data->MaxHeight;
> +            fmt->video.i_bits_per_pixel = 0x18;
> +            fmt->video.i_visible_width = p_data->MaxWidth;
> +            fmt->video.i_visible_height = p_data->MaxHeight;
> +            break;
> +
> +        case AUDIO_ES:
> +            fmt->i_extra = build_raw_esds( p_extra, p_data->CodecPrivateData );
> +            assert( fmt->i_extra > 0 );
> +            if( p_data->FourCC == VLC_FOURCC( 'A', 'A', 'C', 'H' ) ||
> +                p_data->FourCC == VLC_FOURCC( 'A', 'A', 'C', 'L' ) )
> +                fmt->i_codec = VLC_CODEC_MP4A;
> +
> +            /* disable WMAP for now (doesn't work) */
> +            else if( p_data->FourCC == VLC_FOURCC( 'W', 'M', 'A', 'P' ) )
> +                fmt->i_codec = 0;
> +            else
> +                fmt->i_codec = p_data->FourCC;
> +
> +            fmt->audio.i_channels = p_data->Channels;
> +            fmt->audio.i_rate = p_data->SamplingRate;
> +            fmt->audio.i_bitspersample = p_data->BitsPerSample;
> +            break;
> +
> +        default:
> +            break;
> +    }
> +
> +    return VLC_SUCCESS;
> +}
> +
> +/**
> + * Return the track identified by tid
> + */
> +static mp4_track_t *fMP4_GetTrack( demux_t *p_demux, uint16_t tid )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    mp4_track_t *ret = NULL;
> +    for( unsigned i = 0; i < p_sys->i_tracks; i++ )
> +    {
> +        ret = &p_sys->track[i];
> +        if( !ret )
> +            return NULL;
> +        if( ret->i_track_ID == tid )
> +            break;
> +        if( i == p_sys->i_tracks - 1 )
> +        {
> +            msg_Err( p_demux, "fMP4_GetTrack: track %"PRIu16" not found!", tid );
> +            return NULL;
> +        }
> +    }
> +    return ret;
> +}
> +
> +static int FreeChunk( mp4_chunk_t *ck )
> +{
> +    free( ck->p_sample_count_dts );
> +    free( ck->p_sample_delta_dts );
> +    free( ck->p_sample_count_pts );
> +    free( ck->p_sample_offset_pts );
> +    free( ck->p_sample_size );
> +    for( uint32_t i = 0; i < ck->i_sample_count; i++ )
> +        free( ck->p_sample_data[i] );
> +    free( ck->p_sample_data );
> +    memset( ck, 0, sizeof( mp4_chunk_t ) );
> +    return VLC_SUCCESS;
> +}
> +
> +static void FlushChunk( demux_t *p_demux, mp4_track_t *tk )
> +{
> +    msg_Dbg( p_demux, "Flushing chunk for track id %u", tk->i_track_ID );
> +    mp4_chunk_t *ck = tk->cchunk;
> +    while( ck->i_sample < ck->i_sample_count )
> +    {
> +        block_t *p_block;
> +        int64_t i_delta;
> +
> +        if( ck->p_sample_size == NULL || ck->p_sample_data == NULL )
> +            return;
> +
> +        uint32_t sample_size = ck->p_sample_size[ck->i_sample];
> +        assert( sample_size > 0 );
> +        p_block = block_Alloc( sample_size );
> +        if( unlikely( !p_block ) )
> +            return;
> +
> +        uint8_t *src = ck->p_sample_data[ck->i_sample];
> +        assert( src );
> +        memcpy( p_block->p_buffer, src, sample_size );
> +        ck->i_sample++;
> +
> +        /* dts */
> +        p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
> +        /* pts */
> +        i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
> +        if( i_delta != -1 )
> +            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 = VLC_TS_INVALID;
> +
> +        es_out_Send( p_demux->out, tk->p_es, p_block );
> +
> +        tk->i_sample++;
> +    }
> +}
> +
> +/**
> + * Re-init decoder.
> + * \Note If we call that function too soon,
> + * before the track has been selected by MP4_TrackSelect
> + * (during the first execution of Demux), then the track gets disabled
> + */
> +static int ReInitDecoder( demux_t *p_demux, mp4_track_t *p_track )
> +{
> +    demux_sys_t *p_sys = p_demux->p_sys;
> +
> +    uint32_t i_sample = 0;
> +    MP4_Box_t *p_stra = NULL, *p_trak = NULL;
> +
> +    if( p_sys->b_smooth )
> +    {
> +        p_stra = MP4_BoxGet( p_sys->p_root, "uuid/uuid[0]" );
> +        if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
> +            return VLC_EGENERIC;
> +    }
> +    else /* DASH */
> +    {
> +        p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[0]" );
> +        if( !p_trak )
> +            return VLC_EGENERIC;
> +    }
> +
> +    i_sample = p_track->i_sample;
> +    es_out_Del( p_demux->out, p_track->p_es );
> +    es_format_Clean( &p_track->fmt );
> +
> +    if( p_sys->b_smooth )
> +        sms_TrackCreate( p_demux, p_track, p_stra );
> +    else /* DASH */
> +        MP4_TrackCreate( p_demux, p_track, p_trak, true );
> +
> +    p_track->i_sample = i_sample;
> +
> +    /* Temporary hack until we support track selection */
> +    p_track->b_selected = true;
> +    p_track->b_ok = true;
> +    p_track->b_enable = true;
> +
> +    p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
> +    p_track->b_adaption = false;
> +
> +    return VLC_SUCCESS;
> +}
> +
> +/**
> + * This function fills a mp4_chunk_t structure from a MP4_Box_t (p_chunk).
> + * The 'i_tk_id' argument returns the ID of the track the chunk belongs to.
> + * \note p_chunk usually contains a 'moof' and a 'mdat', and might contain a 'sidx'.
> + * \return VLC_SUCCESS, VLC_EGENERIC or VLC_ENOMEM.
> + */
> +static int fMP4_GetChunk( demux_t *p_demux, MP4_Box_t *p_chunk, unsigned *i_tk_id )
> +{
> +    MP4_Box_t *p_sidx = MP4_BoxGet( p_chunk, "sidx" );
> +    MP4_Box_t *p_moof = MP4_BoxGet( p_chunk, "moof" );
> +    if( p_moof == NULL)
> +    {
> +        msg_Warn( p_demux, "no moof box found!" );
> +        return VLC_EGENERIC;
> +    }
> +
> +    /* There is only one traf per moof in un-multiplexed fMP4 */
> +    MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
> +    if( p_traf == NULL)
> +    {
> +        msg_Warn( p_demux, "no traf box found!" );
> +        return VLC_EGENERIC;
> +    }
> +
> +    MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
> +    if( p_tfhd == NULL)
> +    {
> +        msg_Warn( p_demux, "no tfhd box found!" );
> +        return VLC_EGENERIC;
> +    }
> +
> +    uint32_t i_track_ID = p_tfhd->data.p_tfhd->i_track_ID;
> +    *i_tk_id = i_track_ID;
> +    assert( i_track_ID > 0 );
> +    msg_Dbg( p_demux, "GetChunk: track ID is %"PRIu32"", i_track_ID );
> +
> +    mp4_track_t *p_track = fMP4_GetTrack( p_demux, i_track_ID );
> +    if( !p_track )
> +        return VLC_EGENERIC;
> +
> +    mp4_chunk_t *ret = p_track->cchunk;
> +
> +    if( p_tfhd->data.p_tfhd->b_empty )
> +    {
> +        /* XXX Don't know what to do in this case actually */
> +        msg_Warn( p_demux, "No samples in this chunk!" );
> +    }
> +
> +    /* Usually we read 100 ms of each track. However, suppose we have two tracks,
> +     * Ta and Tv (audio and video). Suppose also that Ta is the first track to be
> +     * read, i.e. we read 100 ms of Ta, then 100 ms of Tv, then 100 ms of Ta,
> +     * and so on. Finally, suppose that we get the chunks the other way around,
> +     * i.e. first a chunk of Tv, then a chunk of Ta, then a chunk of Tv, and so on.
> +     * In that case, it is very likely that at some point, Ta->cchunk or Tv->cchunk
> +     * is not emptied when fMP4_GetChunks is called. It is therefore necessary to
> +     * flush it, i.e. send to the decoder the samples not yet sent.
> +     * Note that all the samples to be flushed should worth less than 100 ms,
> +     * (though I did not do the formal proof) and thus this flushing mechanism
> +     * should not cause A/V sync issues, or delays or whatever.
> +     */

Actually I did the formal proof of the above claim:
https://docs.google.com/document/pub?id=1hJ5f_U5kRKgm-6sHfeEvDl3jrMe_95PYQarG28UWL7Y

(have a look at the end)

Also, this document might help you to understand this big patch.

Best regards
Frédéric



More information about the vlc-devel mailing list