Skip to content

Commit c2424b1

Browse files
committed
movenc: Present durations in mvhd/tkhd/mdhd as they are after edits
If the edit lists remove parts of the output timeline, or add a delay to it, this should be included in the mvhd/tkhd/mdhd durations, which should correspond to the edit lists. For tracks starting with pts < 0, the edit list trims out the segment before pts=0. For tracks starting with pts > 0, a delay element is added in the edit list, delaying the start of the track data. In both cases, the practical effect is that the post-edit output is as if the track had started with pts = 0. Thus calculate the range from pts=0 to end_pts, for the purposes of mvhd/tkhd/mdhd, unless edit lists explicitly are disabled. mov_write_edts_tag needs to operate on the actual pts duration of the track samples, not the duration that already takes the edit list effect into account. Signed-off-by: Martin Storsjö <[email protected]>
1 parent d490275 commit c2424b1

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

libavformat/movenc.c

+35-6
Original file line numberDiff line numberDiff line change
@@ -2904,21 +2904,50 @@ static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext
29042904
return update_size(pb, pos);
29052905
}
29062906

2907-
static int64_t calc_pts_duration(MOVMuxContext *mov, MOVTrack *track)
2907+
static void get_pts_range(MOVMuxContext *mov, MOVTrack *track,
2908+
int64_t *start, int64_t *end)
29082909
{
29092910
if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd) {
29102911
// tmcd tracks gets track_duration set in mov_write_moov_tag from
29112912
// another track's duration, while the end_pts may be left at zero.
29122913
// Calculate the pts duration for that track instead.
2913-
return av_rescale(calc_pts_duration(mov, &mov->tracks[track->src_track]),
2914-
track->timescale, mov->tracks[track->src_track].timescale);
2914+
get_pts_range(mov, &mov->tracks[track->src_track], start, end);
2915+
*start = av_rescale(*start, track->timescale,
2916+
mov->tracks[track->src_track].timescale);
2917+
*end = av_rescale(*end, track->timescale,
2918+
mov->tracks[track->src_track].timescale);
2919+
return;
29152920
}
29162921
if (track->end_pts != AV_NOPTS_VALUE &&
29172922
track->start_dts != AV_NOPTS_VALUE &&
29182923
track->start_cts != AV_NOPTS_VALUE) {
2919-
return track->end_pts - (track->start_dts + track->start_cts);
2924+
*start = track->start_dts + track->start_cts;
2925+
*end = track->end_pts;
2926+
return;
29202927
}
2921-
return track->track_duration;
2928+
*start = 0;
2929+
*end = track->track_duration;
2930+
}
2931+
2932+
static int64_t calc_samples_pts_duration(MOVMuxContext *mov, MOVTrack *track)
2933+
{
2934+
int64_t start, end;
2935+
get_pts_range(mov, track, &start, &end);
2936+
return end - start;
2937+
}
2938+
2939+
// Calculate the actual duration of the track, after edits.
2940+
// If it starts with a pts < 0, that is removed by the edit list.
2941+
// If it starts with a pts > 0, the edit list adds a delay before that.
2942+
// Thus, with edit lists enabled, the post-edit output of the file is
2943+
// starting with pts=0.
2944+
static int64_t calc_pts_duration(MOVMuxContext *mov, MOVTrack *track)
2945+
{
2946+
int64_t start, end;
2947+
get_pts_range(mov, track, &start, &end);
2948+
if (mov->use_editlist != 0)
2949+
start = 0;
2950+
return end - start;
29222951
}
29232952

29242953
static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov,
@@ -3145,7 +3174,7 @@ static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
31453174
static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov,
31463175
MOVTrack *track)
31473176
{
3148-
int64_t duration = av_rescale_rnd(calc_pts_duration(mov, track),
3177+
int64_t duration = av_rescale_rnd(calc_samples_pts_duration(mov, track),
31493178
MOV_TIMESCALE, track->timescale,
31503179
AV_ROUND_UP);
31513180
int version = duration < INT32_MAX ? 0 : 1;

tests/ref/fate/copy-trac3074

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
620e3ab4ee6241bec55ea2ec4ef42908 *tests/data/fate/copy-trac3074.mp4
1+
da6122873fb83ce4340cf5d0ab8d475e *tests/data/fate/copy-trac3074.mp4
22
334012 tests/data/fate/copy-trac3074.mp4
33
#tb 0: 1/48000
44
#media_type 0: audio

tests/ref/fate/gaplessenc-itunes-to-ipod-aac

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ duration_ts=103326
55
[/STREAM]
66
[FORMAT]
77
start_time=0.000000
8-
duration=2.367000
8+
duration=2.344000
99
[/FORMAT]
1010
packet|pts=-1024|dts=-1024|duration=1024|flags=KDside_data|
1111

tests/ref/fate/gaplessenc-pcm-to-mov-aac

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ duration_ts=529200
55
[/STREAM]
66
[FORMAT]
77
start_time=0.000000
8-
duration=12.024000
8+
duration=12.000000
99
[/FORMAT]
1010
packet|pts=-1024|dts=-1024|duration=1024|flags=KDside_data|
1111

tests/ref/fate/movenc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ write_data len 36, time nopts, type header atom ftyp
77
write_data len 2761, time nopts, type header atom -
88
write_data len 908, time 966667, type sync atom moof
99
write_data len 110, time nopts, type trailer atom -
10-
5b6e4211a45cd5dac99c8b90752a03d7 3815 non-empty-moov-elst
10+
caf0876986b5f033efc0958c338289cc 3815 non-empty-moov-elst
1111
write_data len 36, time nopts, type header atom ftyp
1212
write_data len 2669, time nopts, type header atom -
1313
write_data len 908, time 1000000, type sync atom moof

0 commit comments

Comments
 (0)