Commit 38353ad6c3 for ffmpeg
commit 38353ad6c3f9912ac0912cfee7763fd18da4badb
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Thu Nov 12 13:57:21 2026 -0800
avformat/mov,movenc: handle the loci location independently of the locale
Print it in the QuickTime ISO 6709 format from the exact 16.16 values,
and parse it with av_sscanf(), rounding to the nearest 16.16 value.
Fixes #25916.
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavformat/mov.c b/libavformat/mov.c
index eba7619ed1..7c91c32999 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,15 +313,19 @@ 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);
- av_strlcatf(buf, sizeof(buf), "/%s", place);
+ // ISO 6709 string as written by QuickTime, independently of the locale
+ lat = llrint(fabs(latitude * (10000.0 / (1 << 16))));
+ lon = llrint(fabs(longitude * (10000.0 / (1 << 16))));
+ alt = llrint(fabs(altitude * (1000.0 / (1 << 16))));
+ snprintf(buf, sizeof(buf), "%c%02"PRId64".%04"PRId64"%c%03"PRId64".%04"PRId64
+ "%c%03"PRId64".%03"PRId64"/%s",
+ latitude < 0 ? '-' : '+', lat / 10000, lat % 10000,
+ longitude < 0 ? '-' : '+', lon / 10000, lon % 10000,
+ altitude < 0 ? '-' : '+', alt / 1000, alt % 1000, place);
if (*language && strcmp(language, "und")) {
char key2[16];
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 27e1dd7718..293a85633e 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4861,36 +4861,30 @@ 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;
-
- latitude_fix = (int32_t) ((1 << 16) * latitude);
- longitude_fix = (int32_t) ((1 << 16) * longitude);
- altitude_fix = (int32_t) ((1 << 16) * altitude);
+ if (av_sscanf(ptr, "%lf%n", &altitude, &n) == 1)
+ ptr += n;
+ if (*ptr == '/')
+ place = ptr + 1;
+
+ latitude_fix = lrint(av_clipd((1 << 16) * latitude, INT32_MIN, INT32_MAX));
+ longitude_fix = lrint(av_clipd((1 << 16) * longitude, INT32_MIN, INT32_MAX));
+ altitude_fix = lrint(av_clipd((1 << 16) * altitude, INT32_MIN, INT32_MAX));
avio_wb32(pb, 0); /* size */
ffio_wfourcc(pb, "loci"); /* type */
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 89e4ef9abc..ad45fb91e3 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -428,6 +428,18 @@ fate-mov-composer-metadata: CMD = transcode wav ./tests/data/asynth-44100-2.wav
"-af aresample -c:a pcm_s16le -t 0.05 -metadata composer=Bach" \
"-af aresample -c:a pcm_s16le" "-show_entries format_tags"
+# 3GPP loci location: ties rounded to even, the altitude clipped to its
+# 16.16 range, and a missing altitude written as zero.
+FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, PCM_S16LE, MP4 MOV, WAV_DEMUXER ARESAMPLE_FILTER) \
+ += fate-mov-mp4-loci fate-mov-mp4-loci-no-altitude
+fate-mov-mp4-loci fate-mov-mp4-loci-no-altitude: tests/data/asynth-44100-2.wav
+fate-mov-mp4-loci: CMD = transcode wav ./tests/data/asynth-44100-2.wav mp4 \
+ "-af aresample -c:a pcm_s16le -t 0.05 -metadata location=+00.03125-000.15625+32767.999999/Place" \
+ "-af aresample -c:a pcm_s16le" "-show_entries format_tags=location"
+fate-mov-mp4-loci-no-altitude: CMD = transcode wav ./tests/data/asynth-44100-2.wav mp4 \
+ "-af aresample -c:a pcm_s16le -t 0.05 -metadata location=-33.8568+151.2153/" \
+ "-af aresample -c:a pcm_s16le" "-show_entries format_tags=location"
+
FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER SETPTS_FILTER MPEG4_ENCODER \
MOV_MUXER MOV_DEMUXER FILE_PROTOCOL) \
+= fate-mov-vfr-bframes-derived-duration
diff --git a/tests/ref/fate/mov-mp4-loci b/tests/ref/fate/mov-mp4-loci
new file mode 100644
index 0000000000..61d8ec6046
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-loci
@@ -0,0 +1,13 @@
+354a338c45ee40362460a8f6e950289a *tests/data/fate/mov-mp4-loci.mp4
+9570 tests/data/fate/mov-mp4-loci.mp4
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 44100
+#channel_layout_name 0: stereo
+0, 0, 0, 1024, 4096, 0x29e3eecf
+0, 1024, 1024, 1024, 4096, 0x18390b96
+0, 2048, 2048, 157, 628, 0x4db5381d
+[FORMAT]
+TAG:location=+00.0312-000.1562+32768.000/Place
+[/FORMAT]
diff --git a/tests/ref/fate/mov-mp4-loci-no-altitude b/tests/ref/fate/mov-mp4-loci-no-altitude
new file mode 100644
index 0000000000..255d784fd4
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-loci-no-altitude
@@ -0,0 +1,13 @@
+5b04c9d342ab6b21efc99dbcf9421113 *tests/data/fate/mov-mp4-loci-no-altitude.mp4
+9565 tests/data/fate/mov-mp4-loci-no-altitude.mp4
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 44100
+#channel_layout_name 0: stereo
+0, 0, 0, 1024, 4096, 0x29e3eecf
+0, 1024, 1024, 1024, 4096, 0x18390b96
+0, 2048, 2048, 157, 628, 0x4db5381d
+[FORMAT]
+TAG:location=-33.8568+151.2153+000.000/
+[/FORMAT]