diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 393442cf5b..31b940a4b2 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -60,6 +60,7 @@ static const AVOption options[] = { { "frag_discont", "Signal that the next fragment is discontinuous from earlier ones", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_DISCONT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" }, { "delay_moov", "Delay writing the initial moov until the first fragment is cut, or until the first fragment flush", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DELAY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" }, { "global_sidx", "Write a global sidx index at the start of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_GLOBAL_SIDX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" }, + { "skip_trailer", "Skip writing the mfra/tfra/mfro trailer for fragmented files", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SKIP_TRAILER}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" }, FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags), { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM}, @@ -2732,6 +2733,20 @@ static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks, return 0; } +static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max) +{ + int i; + for (i = 0; i < mov->nb_streams; i++) { + MOVTrack *track = &mov->tracks[i]; + if ((tracks >= 0 && i != tracks) || !track->entry) + continue; + if (track->nb_frag_info > max) { + memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info)); + track->nb_frag_info = max; + } + } +} + static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track) { int64_t pos = avio_tell(pb); @@ -2917,8 +2932,16 @@ static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)) mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size); - if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0) - return ret; + if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX || + !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) || + mov->ism_lookahead) { + if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0) + return ret; + if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) && + mov->flags & FF_MOV_FLAG_SKIP_TRAILER) { + mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1); + } + } return mov_write_moof_tag_internal(pb, mov, tracks, moof_size); } @@ -4389,7 +4412,7 @@ static int mov_write_trailer(AVFormatContext *s) avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER); mov_write_mfra_tag(pb, mov); } - } else { + } else if (!(mov->flags & FF_MOV_FLAG_SKIP_TRAILER)) { avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER); mov_write_mfra_tag(pb, mov); } diff --git a/libavformat/movenc.h b/libavformat/movenc.h index 6c922fb5df..f4ed188c38 100644 --- a/libavformat/movenc.h +++ b/libavformat/movenc.h @@ -194,6 +194,7 @@ typedef struct MOVMuxContext { #define FF_MOV_FLAG_FRAG_DISCONT (1 << 12) #define FF_MOV_FLAG_DELAY_MOOV (1 << 13) #define FF_MOV_FLAG_GLOBAL_SIDX (1 << 14) +#define FF_MOV_FLAG_SKIP_TRAILER (1 << 15) int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);