[vlc-commits] [Git][videolan/vlc][master] 8 commits: demux: mp4: don't assume the converted value is a vlc_tick_t

Felix Paul Kühne (@fkuehne) gitlab at videolan.org
Wed May 21 11:49:44 UTC 2025



Felix Paul Kühne pushed to branch master at VideoLAN / VLC


Commits:
83dd19c3 by Steve Lhomme at 2025-05-21T11:34:09+00:00
demux: mp4: don't assume the converted value is a vlc_tick_t

- - - - -
9ef458d7 by Steve Lhomme at 2025-05-21T11:34:09+00:00
demux: mp4: convert the decoder delay directly in track ticks

No need for a double conversion.

- - - - -
a9a8b3d0 by Steve Lhomme at 2025-05-21T11:34:09+00:00
demux: mp4: handle moof time outside of FragSeekLoadFragment()

- - - - -
58bbde65 by Steve Lhomme at 2025-05-21T11:34:09+00:00
demux: mp4: always set the p_sample_offset_pts to i_ctsdelta

The assignment is the same in all if/else cases.

- - - - -
656fc6e0 by Steve Lhomme at 2025-05-21T11:34:09+00:00
demux: mp4: only set p_sys->i_nztime once for moov

- - - - -
33384c7a by Steve Lhomme at 2025-05-21T11:34:09+00:00
demux: mp4: remove redundant call

The same call is done below.

- - - - -
8e895ac6 by Steve Lhomme at 2025-05-21T11:34:09+00:00
mux: mp4: pass the track timescale to the track init

- - - - -
3e4bec57 by Steve Lhomme at 2025-05-21T11:34:09+00:00
libmp4: always read the tfra data into 64 bits variables

We need to handle the worst case scenario anyway.
It should not take that much more memory and that avoids
casting pointers.

- - - - -


4 changed files:

- modules/demux/mp4/libmp4.c
- modules/demux/mp4/libmp4.h
- modules/demux/mp4/mp4.c
- modules/mux/mp4/libmp4mux.c


Changes:

=====================================
modules/demux/mp4/libmp4.c
=====================================
@@ -4428,9 +4428,9 @@ static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
     p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
     p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
 
-    size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
-    p_tfra->p_time = calloc( i_number_of_entries, size );
-    p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
+    size_t size;
+    p_tfra->p_time = calloc( i_number_of_entries, sizeof(*p_tfra->p_time) );
+    p_tfra->p_moof_offset = calloc( i_number_of_entries, sizeof(*p_tfra->p_moof_offset) );
 
     size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
     if ( size == 3 ) size++;
@@ -4458,8 +4458,8 @@ static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
         {
             if ( i_read < i_fields_length + 16 )
                 break;
-            MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
-            MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
+            MP4_GET8BYTES( p_tfra->p_time[i] );
+            MP4_GET8BYTES( p_tfra->p_moof_offset[i] );
         }
         else
         {
@@ -4483,22 +4483,11 @@ static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
 #ifdef MP4_ULTRA_VERBOSE
     for( i = 0; i < i_number_of_entries; i++ )
     {
-        if( p_tfra->i_version == 0 )
-        {
-            msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
-                               "moof_offset[%"PRIu32"]: %"PRIu32"",
-                     p_tfra->i_track_ID,
-                     i, p_tfra->p_time[i],
-                     i, p_tfra->p_moof_offset[i] );
-        }
-        else
-        {
-            msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
-                               "moof_offset[%"PRIu32"]: %"PRIu64"",
-                     p_tfra->i_track_ID,
-                     i, ((uint64_t *)(p_tfra->p_time))[i],
-                     i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
-        }
+        msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
+                            "moof_offset[%"PRIu32"]: %"PRIu64"",
+                    p_tfra->i_track_ID,
+                    i, p_tfra->p_time[i],
+                    i, p_tfra->p_moof_offset[i] );
     }
 #endif
 #ifdef MP4_VERBOSE


=====================================
modules/demux/mp4/libmp4.h
=====================================
@@ -1560,8 +1560,8 @@ typedef struct
     uint8_t i_length_size_of_trun_num;
     uint8_t i_length_size_of_sample_num;
 
-    uint32_t *p_time;
-    uint32_t *p_moof_offset;
+    uint64_t *p_time;
+    uint64_t *p_moof_offset;
     uint8_t *p_traf_number;
     uint8_t *p_trun_number;
     uint8_t *p_sample_number;


=====================================
modules/demux/mp4/mp4.c
=====================================
@@ -234,7 +234,7 @@ static int64_t MP4_rescale( int64_t i_value, uint32_t i_timescale, uint32_t i_ne
     if( i_timescale == i_newscale )
         return i_value;
 
-    if( i_value <= VLC_TICK_MAX / i_newscale )
+    if( i_value <= INT64_MAX / i_newscale )
         return i_value * i_newscale / i_timescale;
 
     /* overflow */
@@ -1975,7 +1975,7 @@ static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof )
     return 0;
 }
 
-static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox, stime_t i_moox_time )
+static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     MP4_Box_t *p_moox;
@@ -2009,15 +2009,6 @@ static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox, stime_t i_mo
     p_sys->context.p_fragment_atom = p_moox;
     p_sys->context.i_current_box_type = i_moox;
 
-    if( i_moox == ATOM_moof )
-    {
-        FragPrepareChunk( p_demux, p_moox, NULL, i_moox_time, true );
-        p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
-
-        p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
-        p_sys->i_pcr = VLC_TICK_INVALID;
-    }
-
     msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos );
     return VLC_SUCCESS;
 }
@@ -2165,11 +2156,23 @@ static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurat
     }
 
     /* Context is killed on success */
-    if( FragSeekLoadFragment( p_demux, i_segment_type, i_segment_time ) != VLC_SUCCESS )
+    if( FragSeekLoadFragment( p_demux, i_segment_type ) != VLC_SUCCESS )
     {
         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
         return VLC_EGENERIC;
     }
+    if( i_segment_type == ATOM_moof )
+    {
+        MP4_Box_t *p_moox = p_sys->context.p_fragment_atom;
+        FragPrepareChunk( p_demux, p_moox, NULL, i_segment_time, true );
+        p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
+
+        p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
+    }
+    else
+    {
+        p_sys->i_nztime = i_sync_time;
+    }
 
     p_sys->i_pcr  = VLC_TICK_INVALID;
 
@@ -2178,7 +2181,6 @@ static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurat
         if( i_segment_type == ATOM_moov )
         {
             MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time );
-            p_sys->i_nztime = i_sync_time;
             p_sys->i_pcr  = VLC_TICK_INVALID;
         }
         else
@@ -3035,12 +3037,12 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
                 int64_t i_ctsdelta = ctts->pi_sample_offset[i_index] + i_cts_shift;
                 if( i_ctsdelta < 0 ) /* should not */
                     i_ctsdelta = 0;
+                ck->p_sample_offset_pts[i] = i_ctsdelta;
                 if ( i_current_index_samples_left )
                 {
                     if ( i_current_index_samples_left > i_sample_count )
                     {
                         ck->p_sample_count_pts[i] = i_sample_count;
-                        ck->p_sample_offset_pts[i] = i_ctsdelta;
                         i_current_index_samples_left -= i_sample_count;
                         i_sample_count = 0;
                         assert( i == ck->i_entries_pts - 1 );
@@ -3049,7 +3051,6 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
                     else
                     {
                         ck->p_sample_count_pts[i] = i_current_index_samples_left;
-                        ck->p_sample_offset_pts[i] = i_ctsdelta;
                         i_sample_count -= i_current_index_samples_left;
                         i_current_index_samples_left = 0;
                         i_index++;
@@ -3060,7 +3061,6 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
                     if ( ctts->pi_sample_count[i_index] > i_sample_count )
                     {
                         ck->p_sample_count_pts[i] = i_sample_count;
-                        ck->p_sample_offset_pts[i] = i_ctsdelta;
                         i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
                         i_sample_count = 0;
                         assert( i == ck->i_entries_pts - 1 );
@@ -3069,13 +3069,10 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
                     else
                     {
                         ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
-                        ck->p_sample_offset_pts[i] = i_ctsdelta;
                         i_sample_count -= ctts->pi_sample_count[i_index];
                         i_index++;
                     }
                 }
-
-
             }
         }
     }
@@ -3321,22 +3318,22 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
                     }
                     else if( p_sys->qt.i_delay_samples > 0 )
                     {
-                        p_track->i_decoder_delay =  MP4_rescale_qtime(
-                                    vlc_tick_from_samples( p_sys->qt.i_delay_samples, i_rate ),
+                        p_track->i_decoder_delay =  MP4_rescale(
+                                    p_sys->qt.i_delay_samples, i_rate,
                                     p_track->i_timescale );
                     }
                     /* AAC historical Apple decoder delay 2112 > 2048 */
                     else if( p_fmt->i_codec == VLC_CODEC_MP4A )
                     {
-                        p_track->i_decoder_delay =  MP4_rescale_qtime(
-                                    vlc_tick_from_samples( 2112, i_rate ),
+                        p_track->i_decoder_delay =  MP4_rescale(
+                                    2112, i_rate,
                                     p_track->i_timescale );
                     }
                     /* Opus has an expected 80ms discard on seek */
                     else if( p_fmt->i_codec == VLC_CODEC_OPUS )
                     {
-                        p_track->i_decoder_delay =  MP4_rescale_qtime(
-                                    vlc_tick_from_samples( 80 * 48, 48000 ),
+                        p_track->i_decoder_delay =  MP4_rescale(
+                                    80 * 48, 48000,
                                     p_track->i_timescale );
                     }
                     /* MPEG have One frame delay */
@@ -3345,15 +3342,15 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
                         /* https://lame.sourceforge.io/tech-FAQ.txt
                          * https://www.compuphase.com/mp3/mp3loops.htm
                            https://www.iis.fraunhofer.de/content/dam/iis/de/doc/ame/conference/AES-116-Convention_guideline-to-audio-codec-delay_AES116.pdf */
-                        p_track->i_decoder_delay =  MP4_rescale_qtime(
-                                    vlc_tick_from_samples( 576, i_rate ),
+                        p_track->i_decoder_delay =  MP4_rescale(
+                                    576, i_rate,
                                     p_track->i_timescale );
                     }
                     else if( p_fmt->i_codec == VLC_CODEC_MPGA ||
                              p_fmt->i_codec == VLC_CODEC_MP2 )
                     {
-                        p_track->i_decoder_delay =  MP4_rescale_qtime(
-                                    vlc_tick_from_samples( 240, i_rate ),
+                        p_track->i_decoder_delay =  MP4_rescale(
+                                    240, i_rate,
                                     p_track->i_timescale );
                     }
                 }
@@ -5444,18 +5441,8 @@ static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_t
                 stime_t i_track_target_time = MP4_rescale_qtime( i_target_time, p_track->i_timescale );
                 for ( uint32_t i = 0; i<p_data->i_number_of_entries; i += ( p_data->i_version == 1 ) ? 2 : 1 )
                 {
-                    stime_t i_time;
-                    uint64_t i_offset;
-                    if ( p_data->i_version == 1 )
-                    {
-                        i_time = *((uint64_t *)(p_data->p_time + i));
-                        i_offset = *((uint64_t *)(p_data->p_moof_offset + i));
-                    }
-                    else
-                    {
-                        i_time = p_data->p_time[i];
-                        i_offset = p_data->p_moof_offset[i];
-                    }
+                    stime_t i_time = p_data->p_time[i];
+                    uint64_t i_offset = p_data->p_moof_offset[i];
 
                     if ( i_time >= i_track_target_time )
                     {


=====================================
modules/mux/mp4/libmp4mux.c
=====================================
@@ -148,13 +148,12 @@ mp4mux_trackinfo_t * mp4mux_track_Add(mp4mux_handle_t *h, unsigned id,
     if(unlikely(id == 0))
         return NULL;
     mp4mux_trackinfo_t *t = malloc(sizeof(*t));
-    if(!t || !mp4mux_trackinfo_Init(t, 0, 0))
+    if(!t || !mp4mux_trackinfo_Init(t, 0, timescale))
     {
         free(t);
         return NULL;
     }
     t->i_track_id = id;
-    t->i_timescale = timescale;
     es_format_Init(&t->fmt, fmt->i_cat, fmt->i_codec);
     es_format_Copy(&t->fmt, fmt);
     vlc_array_append(&h->tracks, t);



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/56195130f5972e5cdd1ec721b4a3f66496d5cb3c...3e4bec57cfc24826aca3a80730353017156c14e8

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/56195130f5972e5cdd1ec721b4a3f66496d5cb3c...3e4bec57cfc24826aca3a80730353017156c14e8
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list