@@ -375,16 +375,6 @@ static int set_context_streams_from_tracks(AVFormatContext *s) {
375375 return ret ;
376376}
377377
378- static int64_t get_minimum_track_timestamp (IMFContext * c ) {
379- int64_t minimum_timestamp = INT64_MAX ;
380- for (int i = 0 ; i < c -> track_count ; ++ i ) {
381- if (c -> tracks [i ]-> current_timestamp < minimum_timestamp ) {
382- minimum_timestamp = c -> tracks [i ]-> current_timestamp ;
383- }
384- }
385- return minimum_timestamp ;
386- }
387-
388378static int open_cpl_tracks (AVFormatContext * s ) {
389379 IMFContext * c = s -> priv_data ;
390380 int32_t track_index = 0 ;
@@ -452,6 +442,23 @@ static int imf_read_header(AVFormatContext *s) {
452442 return ret ;
453443}
454444
445+ static IMFVirtualTrackPlaybackCtx * get_next_track_with_minimum_timestamp (AVFormatContext * s ) {
446+ IMFContext * c = s -> priv_data ;
447+ IMFVirtualTrackPlaybackCtx * track ;
448+
449+ int64_t minimum_timestamp = INT64_MAX ;
450+ for (int i = 0 ; i < c -> track_count ; ++ i ) {
451+ av_log (s , AV_LOG_DEBUG , "Compare track %p timestamp %ld to minimum %ld (over duration: %ld)\n" , c -> tracks [i ], c -> tracks [i ]-> current_timestamp , minimum_timestamp , c -> tracks [i ]-> duration );
452+ if (c -> tracks [i ]-> current_timestamp < minimum_timestamp ) {
453+ track = c -> tracks [i ];
454+ minimum_timestamp = track -> current_timestamp ;
455+ }
456+ }
457+
458+ av_log (s , AV_LOG_DEBUG , "Found next track to read: %d (timestamp: %ld / %ld)\n" , track -> index , track -> current_timestamp , minimum_timestamp );
459+ return track ;
460+ }
461+
455462static IMFVirtualTrackResourcePlaybackCtx * get_resource_context_for_timestamp (AVFormatContext * s , IMFVirtualTrackPlaybackCtx * track ) {
456463 unsigned long cumulated_duration = 0 ;
457464 unsigned long edit_unit_duration ;
@@ -483,28 +490,10 @@ static IMFVirtualTrackResourcePlaybackCtx *get_resource_context_for_timestamp(AV
483490static int ff_imf_read_packet (AVFormatContext * s , AVPacket * pkt ) {
484491 IMFContext * c = s -> priv_data ;
485492
486- IMFVirtualTrackPlaybackCtx * track ;
487- IMFVirtualTrackPlaybackCtx * track_to_read = NULL ;
488493 IMFVirtualTrackResourcePlaybackCtx * resource_to_read = NULL ;
494+ int ret = 0 ;
489495
490- int64_t minimum_timestamp = get_minimum_track_timestamp (c );
491- int ret = 0 , i ;
492-
493- for (i = 0 ; i < c -> track_count ; i ++ ) {
494- track = c -> tracks [i ];
495- av_log (s , AV_LOG_DEBUG , "Compare track %p timestamp %ld to minimum %ld (over duration: %ld)\n" , track , track -> current_timestamp , minimum_timestamp , track -> duration );
496- if (track -> current_timestamp <= minimum_timestamp ) {
497- track_to_read = track ;
498- minimum_timestamp = track -> current_timestamp ;
499- av_log (s , AV_LOG_DEBUG , "Found next track to read: %d (timestamp: %ld / %ld)\n" , track_to_read -> index , track -> current_timestamp , minimum_timestamp );
500- break ;
501- }
502- }
503-
504- if (!track_to_read ) {
505- av_log (s , AV_LOG_ERROR , "Could not find IMF track to read\n" );
506- return AVERROR_STREAM_NOT_FOUND ;
507- }
496+ IMFVirtualTrackPlaybackCtx * track_to_read = get_next_track_with_minimum_timestamp (s );
508497
509498 if (track_to_read -> current_timestamp == track_to_read -> duration ) {
510499 return AVERROR_EOF ;
0 commit comments