aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/matroskaenc.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2023-08-08 16:25:39 +0200
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2023-08-10 23:56:35 +0200
commitd53acf452f609b1235839ddb3d39ea377a965814 (patch)
tree3ea94f4eb2dde178967b646c1d9415ee7f9b56af /libavformat/matroskaenc.c
parent59cb099478ee5293c7f3c00b3e2836e1f000f243 (diff)
downloadffmpeg-d53acf452f609b1235839ddb3d39ea377a965814.tar.gz
avformat/matroskaenc: Don't write \0 unnecessarily
Writing the duration SimpleTag is special: It's size is reserved in advance via an EBML Void element (if seekable) and this reserved space is overwritten when writing the trailer; it does not use put_ebml_string(). The string to write is created via snprintf on a buffer of size 20; this buffer is then written via put_ebml_binary() with a size of 20. EBML strings need not be zero-terminated; if not, they are implicitly terminated by the element's length field. snprintf() always zero-terminates the buffer, i.e. the last byte can be discarded when using an EBML string. This patch does this. The FATE changes are as expected: One byte saved for every track; the only exception is the matroska-qt-mode test: An additional byte is saved because an additional byte could be saved from the enclosing Tags length field. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavformat/matroskaenc.c')
-rw-r--r--libavformat/matroskaenc.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 7cbae47e42..e813ef86cf 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -264,8 +264,12 @@ typedef struct MatroskaMuxContext {
/** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
#define MAX_CUETRACKPOS_SIZE 40
-/** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */
-#define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23)
+/** DURATION_STRING_LENGTH must be <= 112 or the containing
+ * simpletag will need more than one byte for its length field. */
+#define DURATION_STRING_LENGTH 19
+
+/** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", rest for TagString */
+#define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + (2 + 1 + DURATION_STRING_LENGTH))
/** Seek preroll value for opus */
#define OPUS_SEEK_PREROLL 80000000
@@ -3239,7 +3243,7 @@ after_cues:
if (track->duration_offset > 0) {
double duration_sec = track->duration * av_q2d(st->time_base);
- char duration_string[20] = "";
+ char duration_string[DURATION_STRING_LENGTH + 1] = "";
ebml_master simpletag;
av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
@@ -3250,11 +3254,12 @@ after_cues:
2 + 1 + 8 + 23);
put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
- snprintf(duration_string, 20, "%02d:%02d:%012.9f",
+ snprintf(duration_string, sizeof(duration_string), "%02d:%02d:%012.9f",
(int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
fmod(duration_sec, 60));
- put_ebml_binary(tags_bc, MATROSKA_ID_TAGSTRING, duration_string, 20);
+ put_ebml_binary(tags_bc, MATROSKA_ID_TAGSTRING,
+ duration_string, DURATION_STRING_LENGTH);
end_ebml_master(tags_bc, simpletag);
}
}