From 2698fc4b53e482adfe09781d90aa936a567524aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <g...@haerdin.se>
Date: Mon, 16 Dec 2024 14:15:12 +0100
Subject: [PATCH 6/6] lavf/movenc: Write version 0 (32-bit) traf if possible

This results in smaller files.
Update fate-movenc accordingly.
---
 libavformat/movenc.c  | 22 +++++++++--
 tests/ref/fate/movenc | 90 +++++++++++++++++++++----------------------
 2 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 76dce9e6e5..e314789831 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -5754,19 +5754,33 @@ static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
-    int i;
+    int i, version = 0;
+
+    // figure out if we can get away with writing version 0 traf
+    for (i = 0; i < track->nb_frag_info; i++) {
+        if (track->frag_info[i].time >= UINT32_MAX ||
+            track->frag_info[i].offset + track->data_offset >= UINT32_MAX) {
+            version = 1;
+            break;
+        }
+    }
 
     avio_wb32(pb, 0); /* size placeholder */
     ffio_wfourcc(pb, "tfra");
-    avio_w8(pb, 1); /* version */
+    avio_w8(pb, version); /* version */
     avio_wb24(pb, 0);
 
     avio_wb32(pb, track->track_id);
     avio_wb32(pb, 0); /* length of traf/trun/sample num */
     avio_wb32(pb, track->nb_frag_info);
     for (i = 0; i < track->nb_frag_info; i++) {
-        avio_wb64(pb, track->frag_info[i].time);
-        avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
+        if (version) {
+            avio_wb64(pb, track->frag_info[i].time);
+            avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
+        } else {
+            avio_wb32(pb, track->frag_info[i].time);
+            avio_wb32(pb, track->frag_info[i].offset + track->data_offset);
+        }
         avio_w8(pb, 1); /* traf number */
         avio_w8(pb, 1); /* trun number */
         avio_w8(pb, 1); /* sample number */
diff --git a/tests/ref/fate/movenc b/tests/ref/fate/movenc
index 5c12aeb29f..00f28ff19d 100644
--- a/tests/ref/fate/movenc
+++ b/tests/ref/fate/movenc
@@ -1,113 +1,113 @@
 write_data len 36, time nopts, type header atom ftyp
 write_data len 2429, time nopts, type header atom -
 write_data len 788, time 1000000, type sync atom moof
-write_data len 110, time nopts, type trailer atom -
-6f06e338c71468d56580df40ff265066 3363 non-empty-moov
+write_data len 94, time nopts, type trailer atom -
+57f2641b843f925b10d22258bb8d50d2 3347 non-empty-moov
 write_data len 36, time nopts, type header atom ftyp
 write_data len 2761, time nopts, type header atom -
 write_data len 908, time 966667, type sync atom moof
-write_data len 110, time nopts, type trailer atom -
-9d260d424e9de4626163fd25ccce5bab 3815 non-empty-moov-elst
+write_data len 94, time nopts, type trailer atom -
+44488b769213b195d98e20abebf9fd02 3799 non-empty-moov-elst
 write_data len 36, time nopts, type header atom ftyp
 write_data len 2669, time nopts, type header atom -
 write_data len 908, time 1000000, type sync atom moof
-write_data len 110, time nopts, type trailer atom -
-e106084014ed245ed7c4a30f1d11a3ac 3723 non-empty-moov-no-elst
+write_data len 94, time nopts, type trailer atom -
+fdcee49342d605bd133be9765d60cbfe 3707 non-empty-moov-no-elst
 write_data len 24, time nopts, type header atom ftyp
 write_data len 1171, time nopts, type header atom -
 write_data len 728, time 0, type sync atom moof
 write_data len 828, time nopts, type unknown atom -
 write_data len 728, time 999999, type sync atom moof
 write_data len 812, time nopts, type unknown atom -
-write_data len 148, time nopts, type trailer atom -
-d2df24d323f4a8896441cd91203ac5f8 4439 ismv
+write_data len 116, time nopts, type trailer atom -
+930ffc74124ac367932fdd3a2582d09e 4407 ismv
 write_data len 36, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 write_data len 796, time 0, type sync atom moof
 write_data len 788, time 1000000, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-08f4b3ad3a3ea224b2ee731476b9056b 2891 empty-moov
+write_data len 116, time nopts, type trailer atom -
+834a4d0b6269951b6577ccde6cef63eb 2859 empty-moov
 write_data len 36, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 write_data len 1068, time 0, type sync atom moof
 write_data len 908, time 1000000, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-d7a2dcb43eb0f95f92669f55fc7adeba 3283 empty-moov-no-elst
+write_data len 116, time nopts, type trailer atom -
+3e2f75d3b03abab012df9faae2fe1321 3251 empty-moov-no-elst
 write_data len 36, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 write_data len 900, time -33333, type sync atom moof
 write_data len 908, time 966667, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-ea70ca697306976879be408431c27aee 3115 empty-moov-no-elst-no-adjust
+write_data len 116, time nopts, type trailer atom -
+3e66b5d27cae7cbb762b4f87cdd568e6 3083 empty-moov-no-elst-no-adjust
 write_data len 1159, time nopts, type header atom ftyp
 write_data len 796, time 0, type sync atom moof
 write_data len 788, time 1000000, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-08f4b3ad3a3ea224b2ee731476b9056b 2891 delay-moov
+write_data len 116, time nopts, type trailer atom -
+834a4d0b6269951b6577ccde6cef63eb 2859 delay-moov
 write_data len 1231, time nopts, type header atom ftyp
 write_data len 916, time -33333, type sync atom moof
 write_data len 908, time 966667, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-314cc3b6296f4ee583b328a34be50b2f 3203 delay-moov-elst
+write_data len 116, time nopts, type trailer atom -
+e80cc7aaadc7b2d946f9082414cfbbc6 3171 delay-moov-elst
 write_data len 1195, time nopts, type header atom ftyp
 write_data len 836, time 0, type sync atom moof
-write_data len 67, time nopts, type trailer atom -
-95d6f59a7354b0cfe7ce49927baada4e 2098 delay-moov-empty-track
+write_data len 59, time nopts, type trailer atom -
+740d78c9ec2d7a3bc0a97ed97cda8482 2090 delay-moov-empty-track
 write_data len 1195, time nopts, type header atom ftyp
 write_data len 360, time 0, type sync atom moof
 write_data len 360, time 1000000, type sync atom moof
-write_data len 86, time nopts, type trailer atom -
-8805d72a27b340ea229c16edde78f974 2001 delay-moov-empty-track-flush
+write_data len 70, time nopts, type trailer atom -
+37857f8b7f85b5ccddbba8dd8db108f4 1985 delay-moov-empty-track-flush
 write_data len 36, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 351ae2c8b6d35d98b4848c309cce6704 1159 empty-moov-header
 write_data len 796, time 0, type sync atom moof
 write_data len 788, time 1000000, type sync atom moof
 289ee982188d66988a374a462b0b5376 1584 empty-moov-content
-write_data len 148, time nopts, type trailer atom -
+write_data len 116, time nopts, type trailer atom -
 write_data len 1159, time nopts, type header atom ftyp
 351ae2c8b6d35d98b4848c309cce6704 1159 delay-moov-header
 write_data len 796, time 0, type sync atom moof
 write_data len 788, time 1000000, type sync atom moof
 289ee982188d66988a374a462b0b5376 1584 delay-moov-content
-write_data len 148, time nopts, type trailer atom -
+write_data len 116, time nopts, type trailer atom -
 write_data len 28, time nopts, type header atom -
 write_data len 1123, time nopts, type header atom -
 write_data len 884, time 0, type sync atom sidx
 write_data len 876, time 1000000, type sync atom sidx
 c0307f99a2a362205b7e3d65b1066f86 876 empty-moov-second-frag
-write_data len 148, time nopts, type trailer atom -
+write_data len 116, time nopts, type trailer atom -
 write_data len 28, time nopts, type header atom -
 write_data len 1123, time nopts, type header atom -
 write_data len 876, time 1000000, type sync atom sidx
 c0307f99a2a362205b7e3d65b1066f86 876 empty-moov-second-frag-discont
-write_data len 110, time nopts, type trailer atom -
+write_data len 94, time nopts, type trailer atom -
 write_data len 1223, time nopts, type header atom -
 write_data len 876, time 1000000, type sync atom sidx
 c0307f99a2a362205b7e3d65b1066f86 876 delay-moov-second-frag-discont
-write_data len 110, time nopts, type trailer atom -
+write_data len 94, time nopts, type trailer atom -
 write_data len 1223, time nopts, type header atom ftyp
 b3811928793ed0749927eb2f7958421c 1223 delay-moov-elst-init
 write_data len 988, time -33333, type sync atom sidx
 write_data len 996, time 966667, type sync atom sidx
 0df125407c7e81978ce722e0ae4f6f84 996 delay-moov-elst-second-frag
-write_data len 148, time nopts, type trailer atom -
+write_data len 116, time nopts, type trailer atom -
 write_data len 1223, time nopts, type header atom ftyp
 b3811928793ed0749927eb2f7958421c 1223 delay-moov-elst-init-discont
 write_data len 996, time 966667, type sync atom sidx
 0df125407c7e81978ce722e0ae4f6f84 996 delay-moov-elst-second-frag-discont
-write_data len 110, time nopts, type trailer atom -
+write_data len 94, time nopts, type trailer atom -
 write_data len 1223, time nopts, type header atom ftyp
 041ac8efc35a0d023c26d05eedb20403 1223 delay-moov-elst-signal-init
 write_data len 1004, time -33333, type sync atom sidx
 write_data len 996, time 966667, type sync atom sidx
 69c9025ffb10302c7b5c2ed9fde86c44 996 delay-moov-elst-signal-second-frag
-write_data len 148, time nopts, type trailer atom -
+write_data len 116, time nopts, type trailer atom -
 write_data len 1223, time nopts, type header atom ftyp
 041ac8efc35a0d023c26d05eedb20403 1223 delay-moov-elst-signal-init-discont
 write_data len 996, time 966667, type sync atom sidx
 69c9025ffb10302c7b5c2ed9fde86c44 996 delay-moov-elst-signal-second-frag-discont
-write_data len 110, time nopts, type trailer atom -
+write_data len 94, time nopts, type trailer atom -
 write_data len 1247, time nopts, type header atom ftyp
 80511a51d1ac9cde62337eed7176ae03 1247 delay-moov-elst-signal-init-discont-largets
 write_data len 996, time 279621233333, type sync atom sidx
@@ -116,13 +116,13 @@ write_data len 110, time nopts, type trailer atom -
 write_data len 1223, time nopts, type header atom ftyp
 write_data len 2572, time -333333, type sync atom sidx
 write_data len 996, time 5166667, type sync atom sidx
-write_data len 148, time nopts, type trailer atom -
-5c873f6e37d5af09e3c6329cf94cd6ca 4939 vfr
+write_data len 116, time nopts, type trailer atom -
+9383f37a1e6f68a3df7d33a422e6fa48 4907 vfr
 write_data len 1223, time nopts, type header atom ftyp
 write_data len 2572, time -333333, type sync atom sidx
 write_data len 996, time 5166667, type sync atom sidx
-write_data len 148, time nopts, type trailer atom -
-5c873f6e37d5af09e3c6329cf94cd6ca 4939 vfr-noduration
+write_data len 116, time nopts, type trailer atom -
+9383f37a1e6f68a3df7d33a422e6fa48 4907 vfr-noduration
 write_data len 1231, time nopts, type header atom ftyp
 write_data len 1500, time -333333, type sync atom moof
 write_data len 1500, time nopts, type unknown atom -
@@ -130,32 +130,32 @@ write_data len 916, time nopts, type unknown atom -
 write_data len 1500, time 9666667, type sync atom moof
 write_data len 1500, time nopts, type unknown atom -
 write_data len 1004, time nopts, type unknown atom -
-write_data len 148, time nopts, type trailer atom -
-08b6401dc81912e5264245b7233c4ab3 9299 large_frag
+write_data len 116, time nopts, type trailer atom -
+5de214b13b33cab917e51955800df4c1 9267 large_frag
 write_data len 1231, time nopts, type header atom ftyp
 write_data len 684, time -33333, type sync atom moof
 write_data len 504, time 800000, type boundary atom moof
 write_data len 420, time 1266667, type boundary atom moof
 write_data len 668, time 1566667, type sync atom moof
 write_data len 440, time 2233333, type boundary atom moof
-write_data len 262, time nopts, type trailer atom -
-a5d087611a9229ba91eb0964cf2f17d9 4209 vfr-noduration-interleave
+write_data len 182, time nopts, type trailer atom -
+237574a23c7e8756709aba165185a21d 4129 vfr-noduration-interleave
 write_data len 1231, time nopts, type header atom ftyp
 write_data len 916, time 0, type sync atom moof
 write_data len 908, time 1000000, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-d81c3a0ce5940a2db74c99ad435e0560 3203 delay-moov-elst-neg-cts
+write_data len 116, time nopts, type trailer atom -
+d18b242b44b61eb0f9f08bd90b1ddda7 3171 delay-moov-elst-neg-cts
 write_data len 36, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 write_data len 900, time 0, type sync atom moof
 write_data len 908, time 1000000, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-3be575022e446855bca1e45b7942cc0c 3115 empty-moov-neg-cts
+write_data len 116, time nopts, type trailer atom -
+fe62747979c27360deff72230a1ef89f 3083 empty-moov-neg-cts
 write_data len 28, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 write_data len 70, time 0, type boundary atom emsg
 write_data len 1832, time 0, type sync atom moof
 write_data len 70, time 2000000, type boundary atom emsg
 write_data len 1840, time 2000000, type sync atom moof
-write_data len 148, time nopts, type trailer atom -
-b72c56c795693820b156f452354a51ff 5111 emsg
+write_data len 116, time nopts, type trailer atom -
+aebe863339fc84ce191528e5f3e9f91a 5079 emsg
-- 
2.39.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to