PR #26000 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26000
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26000.patch

The demuxer printed the `loci` coordinates with `%f`, so under e.g. 
`LANG=de_DE.UTF-8` the `location` tag became `+42,3630-071,3602/`. They are now 
printed from the 16.16 values with integer math.

The muxer parsed the tag with `strtod()`, which stops at `.` in such locales, 
so it now uses `av_sscanf()`. Without this, a remux under a comma locale would 
drop the location.

Fixes #25916.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 84565b26539ae9809287f8a25576c0e1bc5746d3 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 13:57:21 -0800
Subject: [PATCH] avformat/mov,movenc: handle the loci location independently
 of the locale

Print the coordinates with integer math and parse them with av_sscanf().
Fixes #25916.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mov.c    | 24 ++++++++++++++++--------
 libavformat/movenc.c | 22 ++++++++--------------
 2 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index ca33cb2285b..fa60a86d280 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -286,7 +286,8 @@ static int mov_metadata_loci(MOVContext *c, AVIOContext 
*pb, unsigned len)
     char language[4] = { 0 };
     char buf[200], place[100];
     uint16_t langcode = 0;
-    double longitude, latitude, altitude;
+    int32_t longitude, latitude, altitude;
+    int64_t lat, lon, alt;
     const char *key = "location";
 
     if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
@@ -312,14 +313,21 @@ static int mov_metadata_loci(MOVContext *c, AVIOContext 
*pb, unsigned len)
                "loci too short (%u bytes left, need at least %d)\n", len, 12);
         return AVERROR_INVALIDDATA;
     }
-    longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
-    latitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
-    altitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
+    longitude = avio_rb32(pb);
+    latitude  = avio_rb32(pb);
+    altitude  = avio_rb32(pb);
 
-    // Try to output in the same format as the ?xyz field
-    snprintf(buf, sizeof(buf), "%+08.4f%+09.4f",  latitude, longitude);
-    if (altitude)
-        av_strlcatf(buf, sizeof(buf), "%+f", altitude);
+    // Try to output in the same format as the ?xyz field, independently of 
the locale
+    lat = av_rescale(FFABS((int64_t)latitude),  10000, 1 << 16);
+    lon = av_rescale(FFABS((int64_t)longitude), 10000, 1 << 16);
+    snprintf(buf, sizeof(buf), 
"%c%02"PRId64".%04"PRId64"%c%03"PRId64".%04"PRId64,
+             latitude  < 0 ? '-' : '+', lat / 10000, lat % 10000,
+             longitude < 0 ? '-' : '+', lon / 10000, lon % 10000);
+    if (altitude) {
+        alt = av_rescale(FFABS((int64_t)altitude), 1000000, 1 << 16);
+        av_strlcatf(buf, sizeof(buf), "%c%"PRId64".%06"PRId64,
+                    altitude < 0 ? '-' : '+', alt / 1000000, alt % 1000000);
+    }
     av_strlcatf(buf, sizeof(buf), "/%s", place);
 
     if (*language && strcmp(language, "und")) {
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index f36072470f3..1bf0b2d3d9f 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4845,32 +4845,26 @@ static int mov_write_loci_tag(AVFormatContext *s, 
AVIOContext *pb)
 {
     int lang;
     int64_t pos = avio_tell(pb);
-    double latitude, longitude, altitude;
+    double latitude, longitude, altitude = 0;
     int32_t latitude_fix, longitude_fix, altitude_fix;
     AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
     const char *ptr, *place = "";
-    char *end;
+    int n = 0;
     static const char *astronomical_body = "earth";
     if (!t)
         return 0;
 
     ptr = t->value;
-    latitude = strtod(ptr, &end);
-    if (end == ptr) {
+    if (av_sscanf(ptr, "%lf%lf%n", &latitude, &longitude, &n) < 2) {
         av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
         return 0;
     }
-    ptr = end;
-    longitude = strtod(ptr, &end);
-    if (end == ptr) {
-        av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
-        return 0;
-    }
-    ptr = end;
-    altitude = strtod(ptr, &end);
+    ptr += n;
     /* If no altitude was present, the default 0 should be fine */
-    if (*end == '/')
-        place = end + 1;
+    if (av_sscanf(ptr, "%lf%n", &altitude, &n) == 1)
+        ptr += n;
+    if (*ptr == '/')
+        place = ptr + 1;
 
     latitude_fix  = (int32_t) ((1 << 16) * latitude);
     longitude_fix = (int32_t) ((1 << 16) * longitude);
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to