PR #25999 opened by Forgejo_Fairy URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25999 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25999.patch
Fixes #25916. `mov_metadata_loci()` uses floating-point printf conversions for latitude, longitude and optional altitude. An embedding application's LC_NUMERIC setting therefore changes the decimal separators in the exported location string. Round to the existing decimal precision and format the integral and fractional parts separately using a literal dot. Preserve padding, negative coordinates rounded to zero, altitude omission, language-specific metadata and place-name punctuation. Do not modify the application locale. ### Tests The first commit adds `fate-mov-loci`, generating metadata-only MOV inputs in memory and comparing actual metadata against `tests/ref/fate/mov-loci`. It prefers an installed comma-decimal locale and falls back to C; an optional argument selects a specific numeric locale. No external sample or additional installed locale is required. The test fails before the fix on the German-locale test system and passes afterward. Explicit C, de_DE.UTF-8 and ps_AF.UTF-8 runs match the reference after the fix, including a multibyte decimal separator case. ### Verification ARM64: `fate-mov`, `fate-mov-loci` and `fate-source` pass, totaling 108 test invocations. Focused FATE testing also passes on native x86-64 and 32-bit x86 with only C-family locales installed. ARM64 AddressSanitizer/UndefinedBehaviorSanitizer testing passes. A separate differential formatter test completed 12 million comparisons against the previous C-locale output across all four standard floating-point rounding modes, including boundary values, without differences or UBSan findings. The affected function is identical in the checked release/4.4, release/5.1, release/6.1, release/7.1, release/8.0, release/8.1, release/9.0 and release/9.1 sources. No release-branch changes are included. >From ff18b05c6c2e5f0a9d5653fb797eb03aa3c43524 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Thu, 12 Nov 2026 21:58:20 +0000 Subject: [PATCH 1/2] avformat/tests: cover loci location metadata formatting Generate metadata-only MOV inputs in memory, covering signs, zero altitude, rounding, numeric limits, language tags and punctuation in place names. Keep expected metadata in a FATE reference. Prefer a comma-decimal locale if available, otherwise fall back to C. Accept an optional LC_NUMERIC locale name for manual tests. This tests non-C formatting where possible without requiring installed locales for FATE. Assisted-by: Fairy --- libavformat/Makefile | 1 + libavformat/tests/mov_loci.c | 124 +++++++++++++++++++++++++++++++++++ tests/fate/libavformat.mak | 4 ++ tests/ref/fate/mov-loci | 15 +++++ 4 files changed, 144 insertions(+) create mode 100644 libavformat/tests/mov_loci.c create mode 100644 tests/ref/fate/mov-loci diff --git a/libavformat/Makefile b/libavformat/Makefile index 891d7f82d9a..60f61ed9107 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -796,6 +796,7 @@ TESTPROGS-$(CONFIG_SRTP) += srtp TESTPROGS-$(CONFIG_IMF_DEMUXER) += imf TESTPROGS-$(CONFIG_MP3_MUXER) += id3v2enc id3v2chap TESTPROGS-$(CONFIG_AU_MUXER) += auenc +TESTPROGS-$(CONFIG_MOV_DEMUXER) += mov_loci TESTPROGS-$(CONFIG_MPEGTS_DEMUXER) += mpegts_probe TESTPROGS-$(CONFIG_OGG_MUXER) += ogg_pages diff --git a/libavformat/tests/mov_loci.c b/libavformat/tests/mov_loci.c new file mode 100644 index 00000000000..1e3f7ffd6db --- /dev/null +++ b/libavformat/tests/mov_loci.c @@ -0,0 +1,124 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + * Generate metadata-only MOV inputs; no external samples are needed. + * Prefer a locale with a comma decimal separator, falling back to C if none + * is installed. An optional argument selects a specific LC_NUMERIC locale. + */ + +#include <locale.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "libavutil/common.h" +#include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" +#include "libavformat/avformat.h" +#include "libavformat/avio_internal.h" + +static int test_loci(int32_t longitude, int32_t latitude, int32_t altitude, + unsigned language, const char *place) +{ + uint8_t buf[256] = { 0 }; + int place_size = strlen(place) + 1; + int size = 45 + place_size; + AVFormatContext *fmt; + const AVDictionaryEntry *entry = NULL; + FFIOContext pb; + int ret; + + AV_WB32(buf, size); + memcpy(buf + 4, "moov", 4); + AV_WB32(buf + 8, size - 8); + memcpy(buf + 12, "udta", 4); + AV_WB32(buf + 16, size - 16); + memcpy(buf + 20, "loci", 4); + /* version, flags and role are zero; body and notes are empty strings. */ + AV_WB16(buf + 28, language); + memcpy(buf + 30, place, place_size); + AV_WB32(buf + 31 + place_size, longitude); + AV_WB32(buf + 35 + place_size, latitude); + AV_WB32(buf + 39 + place_size, altitude); + + ffio_init_read_context(&pb, buf, size); + fmt = avformat_alloc_context(); + if (!fmt) + return 1; + fmt->pb = &pb.pub; + ret = avformat_open_input(&fmt, NULL, av_find_input_format("mov"), NULL); + if (ret < 0) { + fprintf(stderr, "Could not open generated MOV: %d\n", ret); + return 1; + } + + while ((entry = av_dict_get(fmt->metadata, "location", entry, + AV_DICT_IGNORE_SUFFIX))) + printf("%s=%s\n", entry->key, entry->value); + avformat_close_input(&fmt); + return 0; +} + +int main(int argc, char **argv) +{ + static const int32_t coordinates[][3] = { + { -4676662, 2776302, 0 }, + { -4676662, 2776302, 819200 }, + { 4676662, -2776302, -819200 }, + { 0, 0, 0 }, + { 1, -1, 1 }, + { -1, 1, -1 }, + { 180 * 65536, 90 * 65536, -32768 * 65536 }, + { -180 * 65536, -90 * 65536, INT32_MAX - 127 }, + /* Rounding across an integer boundary. */ + { 65535, -65535, 65535 }, + /* Halfway cases, rounding to even in the default rounding mode. */ + { 2048, 6144, 512 }, + { -2048, -6144, 1536 }, + { 2048, 6144, -512 }, + { -2048, -6144, -1536 }, + }; + static const char *const locales[] = { + "de_DE.UTF-8", "de_DE.utf8", "de_DE", "German", "C", + }; + const char *numeric_locale = NULL; + int ret = 0; + + if (argc > 1) { + numeric_locale = setlocale(LC_NUMERIC, argv[1]); + } else { + for (int i = 0; i < FF_ARRAY_ELEMS(locales); i++) { + numeric_locale = setlocale(LC_NUMERIC, locales[i]); + if (numeric_locale) + break; + } + } + if (!numeric_locale) { + fprintf(stderr, "Requested numeric locale is unavailable\n"); + return 1; + } + + for (int i = 0; i < FF_ARRAY_ELEMS(coordinates); i++) + ret |= test_loci(coordinates[i][0], coordinates[i][1], + coordinates[i][2], 0x55c4 /* und */, ""); + /* Locale handling must not rewrite punctuation in the place name. */ + ret |= test_loci(-4676662, 2776302, 819200, 0x15c7 /* eng */, + "Cambridge, MA. +-"); + return ret; +} diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak index 70d5439437f..be37b854ee9 100644 --- a/tests/fate/libavformat.mak +++ b/tests/fate/libavformat.mak @@ -88,6 +88,10 @@ FATE_LIBAVFORMAT += fate-seek_utils fate-seek_utils: libavformat/tests/seek_utils$(EXESUF) fate-seek_utils: CMD = run libavformat/tests/seek_utils$(EXESUF) +FATE_LIBAVFORMAT-$(CONFIG_MOV_DEMUXER) += fate-mov-loci +fate-mov-loci: libavformat/tests/mov_loci$(EXESUF) +fate-mov-loci: CMD = run libavformat/tests/mov_loci$(EXESUF) + FATE_LIBAVFORMAT += $(FATE_LIBAVFORMAT-yes) FATE-$(CONFIG_AVFORMAT) += $(FATE_LIBAVFORMAT) fate-libavformat: $(FATE_LIBAVFORMAT) diff --git a/tests/ref/fate/mov-loci b/tests/ref/fate/mov-loci new file mode 100644 index 00000000000..6419b2c0d6d --- /dev/null +++ b/tests/ref/fate/mov-loci @@ -0,0 +1,15 @@ +location=+42.3630-071.3602/ +location=+42.3630-071.3602+12.500000/ +location=-42.3630+071.3602-12.500000/ +location=+00.0000+000.0000/ +location=-00.0000+000.0000+0.000015/ +location=+00.0000-000.0000-0.000015/ +location=+90.0000+180.0000-32768.000000/ +location=-90.0000-180.0000+32767.998047/ +location=-01.0000+001.0000+0.999985/ +location=+00.0938+000.0312+0.007812/ +location=-00.0938-000.0312+0.023438/ +location=+00.0938+000.0312-0.007812/ +location=-00.0938-000.0312-0.023438/ +location-eng=+42.3630-071.3602+12.500000/Cambridge, MA. +- +location=+42.3630-071.3602+12.500000/Cambridge, MA. +- -- 2.54.0 >From 1ad844d518944a10e996c2faa9854fa90444de9d Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Thu, 12 Nov 2026 21:58:20 +0000 Subject: [PATCH 2/2] avformat/mov: format loci coordinates independently of locale Floating-point printf conversions use the application's LC_NUMERIC decimal separator, so loci metadata becomes +42,3630-071,3602/ under a German locale. Round to the existing decimal precision and print the integral and fractional parts separately with a literal dot. Preserve the original sign when a small negative coordinate rounds to zero, and cover altitude as well as latitude and longitude. Do not change the application locale or rewrite punctuation in the place name. Fixes: #25916 Assisted-by: Fairy --- libavformat/mov.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/libavformat/mov.c b/libavformat/mov.c index ca33cb2285b..ed5209edcca 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -280,6 +280,22 @@ static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) return 0; } +static void mov_loci_coordinate(char *buf, int size, double value, + int integer_digits, int fractional_digits) +{ + int64_t scale = 1, rounded; + + for (int i = 0; i < fractional_digits; i++) + scale *= 10; + rounded = llrint(value * scale); + + // Use a literal decimal point, independent of LC_NUMERIC. Take the sign + // from value so that negative coordinates rounded to zero keep their sign. + av_strlcatf(buf, size, "%c%0*"PRId64".%0*"PRId64, + value < 0 ? '-' : '+', integer_digits, FFABS(rounded) / scale, + fractional_digits, FFABS(rounded) % scale); +} + // 3GPP TS 26.244 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len) { @@ -317,9 +333,11 @@ static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len) altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16); // Try to output in the same format as the ?xyz field - snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude); + buf[0] = 0; + mov_loci_coordinate(buf, sizeof(buf), latitude, 2, 4); + mov_loci_coordinate(buf, sizeof(buf), longitude, 3, 4); if (altitude) - av_strlcatf(buf, sizeof(buf), "%+f", altitude); + mov_loci_coordinate(buf, sizeof(buf), altitude, 1, 6); av_strlcatf(buf, sizeof(buf), "/%s", place); if (*language && strcmp(language, "und")) { -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
