From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id DECE44B216 for ; Fri, 31 May 2024 08:54:10 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 81FA268D4EF; Fri, 31 May 2024 11:54:07 +0300 (EEST) Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2007F68D389 for ; Fri, 31 May 2024 11:54:01 +0300 (EEST) Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2e6f51f9de4so24512781fa.3 for ; Fri, 31 May 2024 01:54:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20230601.gappssmtp.com; s=20230601; t=1717145639; x=1717750439; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=7DM8QIbaW9w8jSrYKy60+5U+3QHgDbXf94N18fN6atw=; b=oO4+Xt5RuoyEZccWLnI/BKlQy069ItJ6jPuDROpIE+NiHRR5siqkVpsjS4KndOnodh ChGxEcoKj4wS7MTU+haBnhklVx3r/djNaCf/fDSQfuP0M+7oUhWKSWV6wybLE8aXnKfU rinrfAY7Exs6PU9GNUPFI/yDdhfGYFsPVFWAS5a7hnb8LVz5sthkgb60Zl83IrN2vMvV sRR+JuwFLdywepZltpFFMpkdbr8hWgPcfTJV3iFpj6RQtrpHMXDQbMwwrkdQZUM4vhMs 3X2qlvZgzOKfR95psOeDEIZi53ZqxIXAcC8G+jopTUHH0oI/nkxkhZD/vPfsawhfx+0t IoKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717145639; x=1717750439; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7DM8QIbaW9w8jSrYKy60+5U+3QHgDbXf94N18fN6atw=; b=eo0DQJFv8Duq1oWmPzTgwfm4q3OGiRVQdKDkH61+VAdZx8IMlBnHW6QgUq7UJs5zL3 yJUtf5EOyuYLULXALQYpv7p6HCMI/XseS5IIhDiyY7NuLWKjVJzImTsysgInJIE4apm/ VH4PKh29QxWHEUIPN+DAH7ixSksDD7uuwhO6nlnWnnmuFI3IMdQjL/enc1mj88MGI4Gr dOMe1cxiRx8DrWtz/NLeEspn9ViyQvVVSks9Oa5G+KdSzfH3EwJJONfZTU1vQe3odfui jkXq/OrjkvtV069GCqrxLcL1KuRsiNwRx1zlVPvmuKPFzXCBPQX75xb5UJETGjKr6Vgl yBEQ== X-Gm-Message-State: AOJu0YyUIL2yH0XFX/KtN8qc/hjWRDMcPGDNffWdxYFH4vVaeVo2ucoO hc+MzL2hocB4F7s7OgH6nO5I6wGw4TLNiwJ/w8IdroHfAUBg6AOWegJ/L5C4Au8FNwekd0PjmpX jSQ== X-Google-Smtp-Source: AGHT+IHlWPdOXBs6yM0usedTggm+aYF8EWOlY7OLHNqFKXAwyqErGY+l8RzGzs1HmzY01jhQY2KxIQ== X-Received: by 2002:a2e:824b:0:b0:2e9:61c7:cad3 with SMTP id 38308e7fff4ca-2ea95153f8emr10360121fa.25.1717145639277; Fri, 31 May 2024 01:53:59 -0700 (PDT) Received: from localhost (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2ea91ce877esm2341301fa.106.2024.05.31.01.53.58 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 31 May 2024 01:53:58 -0700 (PDT) From: =?UTF-8?q?Martin=20Storsj=C3=B6?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 May 2024 11:53:58 +0300 Message-Id: <20240531085358.45478-1-martin@martin.st> X-Mailer: git-send-email 2.39.3 (Apple Git-146) MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] movenc: Add an option for hiding fragments at the end X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: This allows ending up with a normal, non-fragmented file when the file is finished, while keeping the file readable if writing is aborted abruptly at any point. (Normally when writing a mov/mp4 file, the unfinished file is completely useless unless it is finished properly.) This results in a file where the mdat atom contains (and hides) all the moof atoms that were part of the fragmented file structure initially. --- This is, incidentally, how Apple devices do (or at least did, at some point) their writing of files when recording, at least with some of their userspace APIs. --- doc/muxers.texi | 7 +++++ libavformat/movenc.c | 59 ++++++++++++++++++++++++++++++++--- libavformat/movenc.h | 4 ++- libavformat/version.h | 4 +-- tests/fate/lavf-container.mak | 3 +- tests/ref/lavf/mov_hide_frag | 3 ++ 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 tests/ref/lavf/mov_hide_frag diff --git a/doc/muxers.texi b/doc/muxers.texi index 6340c8e54d..e313b5e631 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -569,6 +569,13 @@ experimental, may be renamed or changed, do not use from scripts. @item write_gama write deprecated gama atom + +@item hide_fragments +After writing a fragmented file, convert it to a regular, non-fragmented +file at the end. This keeps the file readable while it is being +written, and makes it recoverable if the process of writing the file +gets aborted uncleanly, while still producing an easily seekable +and widely compatible non-fragmented file in the end. @end table @item movie_timescale @var{scale} diff --git a/libavformat/movenc.c b/libavformat/movenc.c index d1517870fc..6f044a62a7 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -110,6 +110,7 @@ static const AVOption options[] = { { "use_metadata_tags", "Use mdta atom for metadata.", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_USE_MDTA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" }, { "write_colr", "Write colr atom even if the color info is unspecified (Experimental, may be renamed or changed, do not use from scripts)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_COLR}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" }, { "write_gama", "Write deprecated gama atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_GAMA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" }, + { "hide_fragments", "Hide fragments at the end", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_HIDE_FRAGMENTS}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" }, { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, { "mov_gamma", "gamma value for gama atom", offsetof(MOVMuxContext, gamma), AV_OPT_TYPE_FLOAT, {.dbl = 0.0 }, 0.0, 10, AV_OPT_FLAG_ENCODING_PARAM}, { "movie_timescale", "set movie timescale", offsetof(MOVMuxContext, movie_timescale), AV_OPT_TYPE_INT, {.i64 = MOV_TIMESCALE}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, @@ -5993,10 +5994,30 @@ static int mov_write_squashed_packets(AVFormatContext *s) return 0; } -static int mov_finish_fragment(MOVTrack *track) +static int mov_finish_fragment(MOVMuxContext *mov, MOVTrack *track, + int64_t ref_pos) { + int i; if (!track->entry) return 0; + if (mov->flags & FF_MOV_FLAG_HIDE_FRAGMENTS) { + for (i = 0; i < track->entry; i++) + track->cluster[i].pos += ref_pos + track->data_offset; + if (track->cluster_written == 0 && !(mov->flags & FF_MOV_FLAG_EMPTY_MOOV)) { + // First flush. If this was a case of not using empty moov, reset chunking. + for (i = 0; i < track->entry; i++) { + track->cluster[i].chunkNum = 0; + track->cluster[i].samples_in_chunk = track->cluster[i].entries; + } + } + if (av_reallocp_array(&track->cluster_written, + track->entry_written + track->entry, + sizeof(*track->cluster))) + return AVERROR(ENOMEM); + memcpy(&track->cluster_written[track->entry_written], + track->cluster, track->entry * sizeof(*track->cluster)); + track->entry_written += track->entry; + } track->entry = 0; track->entries_flushed = 0; track->end_reliable = 0; @@ -6007,7 +6028,7 @@ static int mov_flush_fragment(AVFormatContext *s, int force) { MOVMuxContext *mov = s->priv_data; int i, first_track = -1; - int64_t mdat_size = 0; + int64_t mdat_size = 0, mdat_start = 0; int ret; int has_video = 0, starts_with_key = 0, first_video_track = 1; @@ -6113,7 +6134,7 @@ static int mov_flush_fragment(AVFormatContext *s, int force) mov->moov_written = 1; mov->mdat_size = 0; for (i = 0; i < mov->nb_tracks; i++) - mov_finish_fragment(&mov->tracks[i]); + mov_finish_fragment(mov, &mov->tracks[i], 0); avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT); return 0; } @@ -6182,9 +6203,10 @@ static int mov_flush_fragment(AVFormatContext *s, int force) avio_wb32(s->pb, mdat_size + 8); ffio_wfourcc(s->pb, "mdat"); + mdat_start = avio_tell(s->pb); } - mov_finish_fragment(&mov->tracks[i]); + mov_finish_fragment(mov, &mov->tracks[i], mdat_start); if (!mov->frag_interleave) { if (!track->mdat_buf) continue; @@ -7169,6 +7191,7 @@ static void mov_free(AVFormatContext *s) else if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd) av_freep(&track->par); av_freep(&track->cluster); + av_freep(&track->cluster_written); av_freep(&track->frag_info); av_packet_free(&track->cover_image); @@ -7887,6 +7910,11 @@ static int mov_write_header(AVFormatContext *s) FF_MOV_FLAG_FRAG_EVERY_FRAME)) && !mov->max_fragment_duration && !mov->max_fragment_size) mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME; + if (mov->flags & FF_MOV_FLAG_HIDE_FRAGMENTS) { + avio_wb32(pb, 8); // placeholder for extended size field (64 bit) + ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free"); + mov->mdat_pos = avio_tell(pb); + } } else if (mov->mode != MODE_AVIF) { if (mov->flags & FF_MOV_FLAG_FASTSTART) mov->reserved_header_pos = avio_tell(pb); @@ -8090,13 +8118,34 @@ static int mov_write_trailer(AVFormatContext *s) } } - if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) { + if (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || + mov->flags & FF_MOV_FLAG_HIDE_FRAGMENTS) { + if (mov->flags & FF_MOV_FLAG_HIDE_FRAGMENTS) { + mov_flush_fragment(s, 1); + mov->mdat_size = avio_tell(pb) - mov->mdat_pos - 8; + for (i = 0; i < mov->nb_tracks; i++) { + MOVTrack *track = &mov->tracks[i]; + track->data_offset = 0; + av_free(track->cluster); + track->cluster = track->cluster_written; + track->entry = track->entry_written; + track->cluster_written = NULL; + track->entry_written = 0; + track->chunkCount = 0; // Force build_chunks to rebuild the list of chunks + } + // Clear the empty_moov flag, as we do want the moov to include + // all the samples at this point. + mov->flags &= ~FF_MOV_FLAG_EMPTY_MOOV; + } + moov_pos = avio_tell(pb); /* Write size of mdat tag */ if (mov->mdat_size + 8 <= UINT32_MAX) { avio_seek(pb, mov->mdat_pos, SEEK_SET); avio_wb32(pb, mov->mdat_size + 8); + if (mov->flags & FF_MOV_FLAG_HIDE_FRAGMENTS) + ffio_wfourcc(pb, "mdat"); // overwrite the original moov into a mdat } else { /* overwrite 'wide' placeholder atom */ avio_seek(pb, mov->mdat_pos - 8, SEEK_SET); diff --git a/libavformat/movenc.h b/libavformat/movenc.h index 08d580594d..59daf8946b 100644 --- a/libavformat/movenc.h +++ b/libavformat/movenc.h @@ -85,7 +85,7 @@ typedef struct MOVFragmentInfo { typedef struct MOVTrack { int mode; - int entry; + int entry, entry_written; unsigned timescale; uint64_t time; int64_t track_duration; @@ -114,6 +114,7 @@ typedef struct MOVTrack { int vos_len; uint8_t *vos_data; MOVIentry *cluster; + MOVIentry *cluster_written; unsigned cluster_capacity; int audio_vbr; int height; ///< active picture (w/o VBI) height for D-10/IMX @@ -282,6 +283,7 @@ typedef struct MOVMuxContext { #define FF_MOV_FLAG_SKIP_SIDX (1 << 21) #define FF_MOV_FLAG_CMAF (1 << 22) #define FF_MOV_FLAG_PREFER_ICC (1 << 23) +#define FF_MOV_FLAG_HIDE_FRAGMENTS (1 << 24) int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt); diff --git a/libavformat/version.h b/libavformat/version.h index 4687cd857c..af7d0a1024 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,8 +31,8 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 3 -#define LIBAVFORMAT_VERSION_MICRO 104 +#define LIBAVFORMAT_VERSION_MINOR 4 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ diff --git a/tests/fate/lavf-container.mak b/tests/fate/lavf-container.mak index d84117c50f..035c394c09 100644 --- a/tests/fate/lavf-container.mak +++ b/tests/fate/lavf-container.mak @@ -5,7 +5,7 @@ FATE_LAVF_CONTAINER-$(call ENCDEC, FLV, FLV) + FATE_LAVF_CONTAINER-$(call ENCDEC, RAWVIDEO, FILMSTRIP) += flm FATE_LAVF_CONTAINER-$(call ENCDEC2, MPEG2VIDEO, PCM_S16LE, GXF) += gxf gxf_pal gxf_ntsc FATE_LAVF_CONTAINER-$(call ENCDEC2, MPEG4, MP2, MATROSKA) += mkv mkv_attachment -FATE_LAVF_CONTAINER-$(call ENCDEC2, MPEG4, PCM_ALAW, MOV) += mov mov_rtphint ismv +FATE_LAVF_CONTAINER-$(call ENCDEC2, MPEG4, PCM_ALAW, MOV) += mov mov_rtphint mov_hide_frag ismv FATE_LAVF_CONTAINER-$(call ENCDEC, MPEG4, MOV) += mp4 FATE_LAVF_CONTAINER-$(call ENCDEC2, MPEG1VIDEO, MP2, MPEG1SYSTEM MPEGPS) += mpg FATE_LAVF_CONTAINER-$(call ENCDEC , FFV1, MXF) += mxf_ffv1 @@ -51,6 +51,7 @@ fate-lavf-mkv: CMD = lavf_container "" "-c:a mp2 -c:v mpeg4 -ar 44100 -threads 1 fate-lavf-mkv_attachment: CMD = lavf_container_attach "-c:a mp2 -c:v mpeg4 -threads 1 -f matroska" fate-lavf-mov: CMD = lavf_container_timecode "-movflags +faststart -c:a pcm_alaw -c:v mpeg4 -threads 1" fate-lavf-mov_rtphint: CMD = lavf_container "" "-movflags +rtphint -c:a pcm_alaw -c:v mpeg4 -threads 1 -f mov" +fate-lavf-mov_hide_frag: CMD = lavf_container "" "-movflags +frag_keyframe+hide_fragments -c:a pcm_alaw -c:v mpeg4 -threads 1 -f mov" fate-lavf-mp4: CMD = lavf_container_timecode "-c:v mpeg4 -an -threads 1" fate-lavf-mpg: CMD = lavf_container_timecode "-ar 44100 -threads 1" fate-lavf-mxf: CMD = lavf_container_timecode "-af aresample=48000:tsf=s16p -bf 2 -threads 1" diff --git a/tests/ref/lavf/mov_hide_frag b/tests/ref/lavf/mov_hide_frag new file mode 100644 index 0000000000..af3af5b171 --- /dev/null +++ b/tests/ref/lavf/mov_hide_frag @@ -0,0 +1,3 @@ +4871796f41234350f1b050317d0288a3 *tests/data/lavf/lavf.mov_hide_frag +358508 tests/data/lavf/lavf.mov_hide_frag +tests/data/lavf/lavf.mov_hide_frag CRC=0xbb2b949b -- 2.39.3 (Apple Git-146) _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".