PR #20846 opened by Lukas (lholliger) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20846 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20846.patch
QuickTime File Format (QTFF/MOV) have a list of well-known datatypes and there is currently little in place to decode or encode these values besides a small part in mov.c. This adds the ability to encode and decode various types for integers, floats, and strings. These well-known types have usage to decoding some parts on the moov atom or for metadata boxed/mebx values. Also updated mov.c to use this function. >From 9a4c805ea90401f8c51f31be1c4c523815dfa100 Mon Sep 17 00:00:00 2001 From: lholliger <[email protected]> Date: Wed, 5 Nov 2025 14:15:08 -0500 Subject: [PATCH 1/2] libavutil: add qtff well-known type functions QuickTime File Format (QTFF/MOV) have a list of well-known datatypes and there is currently little in place to decode or encode these values besides a small part in mov.c. This adds the ability to encode and decode various types for integers, floats, and strings. These well-known types have usage to decoding some parts on the moov atom or for metadata boxed/mebx values. Signed-off-by: lholliger <[email protected]> --- libavutil/Makefile | 2 + libavutil/qtff.c | 380 +++++++++++++++++++++++++++++++++++++++++++++ libavutil/qtff.h | 59 +++++++ 3 files changed, 441 insertions(+) create mode 100644 libavutil/qtff.c create mode 100644 libavutil/qtff.h diff --git a/libavutil/Makefile b/libavutil/Makefile index ee77e51c08..792b587529 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -75,6 +75,7 @@ HEADERS = adler32.h \ pixdesc.h \ pixelutils.h \ pixfmt.h \ + qtff.h \ random_seed.h \ rc4.h \ rational.h \ @@ -168,6 +169,7 @@ OBJS = adler32.o \ parseutils.o \ pixdesc.o \ pixelutils.o \ + qtff.o \ random_seed.o \ rational.o \ refstruct.o \ diff --git a/libavutil/qtff.c b/libavutil/qtff.c new file mode 100644 index 0000000000..a58d360958 --- /dev/null +++ b/libavutil/qtff.c @@ -0,0 +1,380 @@ +/* + * copyright (c) 2025 Lukas Holliger + * + * 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 + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "qtff.h" +#include "intfloat.h" +#include "intreadwrite.h" +#include "error.h" + +int av_qtff_convert_well_known_to_str(int data_type, const uint8_t *data, int data_size, + char *str, int str_size) +{ + if (!data || !str || str_size <= 0) + return AVERROR(EINVAL); + + switch (data_type) { + case 0: // Reserved + case 2: // UTF-16 + case 3: // S/JIS + case 4: // UTF-8 sort + case 5: // UTF-16 sort + case 13: // JPEG + case 14: // PNG + case 27: // BMP + case 28: // QuickTime Metadata atom + case 70: // BE PointF32 + case 71: // BE DimensionsF32 + case 72: // BE RectF32 + case 79: // AffineTransformF64 + return AVERROR_PATCHWELCOME; + case 1: { // UTF-8 + int len = data_size < str_size - 1 ? data_size : str_size - 1; + memcpy(str, data, len); + str[len] = '\0'; + break; + } + case 21: { // BE Signed Integer (variable size, not usable for timed metadata) + int val = 0; + switch (data_size) { + case 1: + val = (int8_t)AV_RB8(data); + break; + case 2: + val = (int16_t)AV_RB16(data); + break; + case 3: + val = ((int32_t)(AV_RB24(data) << 8)) >> 8; + break; + case 4: + val = (int32_t)AV_RB32(data); + break; + default: + return AVERROR(EINVAL); + } + + if (snprintf(str, str_size, "%d", val) >= str_size) + return AVERROR(ENOMEM); + break; + } + case 22: { // BE Unsigned Integer (variable size, not usable for timed metadata) + unsigned int val = 0; + switch (data_size) { + case 1: + val = AV_RB8(data); + break; + case 2: + val = AV_RB16(data); + break; + case 3: + val = AV_RB24(data); + break; + case 4: + val = AV_RB32(data); + break; + default: + return AVERROR(EINVAL); + } + + if (snprintf(str, str_size, "%u", val) >= str_size) + return AVERROR(ENOMEM); + break; + } + case 23: { // BE float32 + float val; + if (data_size != 4) + return AVERROR(EINVAL); + + val = av_int2float(AV_RB32(data)); + if (snprintf(str, str_size, "%f", val) >= str_size) + return AVERROR(ENOMEM); + break; + } + case 24: { // BE float64 + double val; + if (data_size != 8) + return AVERROR(EINVAL); + + val = av_int2double(AV_RB64(data)); + if (snprintf(str, str_size, "%f", val) >= str_size) + return AVERROR(ENOMEM); + break; + } + case 65: // 8-bit Signed Integer + if (data_size != 1) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%d", (int)(int8_t)data[0]) >= str_size) + return AVERROR(ENOMEM); + break; + case 66: // BE 16-bit Signed Integer + if (data_size != 2) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%d", (int)(int16_t)AV_RB16(data)) >= str_size) + return AVERROR(ENOMEM); + break; + case 67: // BE 32-bit Signed Integer + if (data_size != 4) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%d", (int)(int32_t)AV_RB32(data)) >= str_size) + return AVERROR(ENOMEM); + break; + case 74: // BE 64-bit Signed Integer + if (data_size != 8) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%lld", (long long)(int64_t)AV_RB64(data)) >= str_size) + return AVERROR(ENOMEM); + break; + case 75: // 8-bit Unsigned Integer + if (data_size != 1) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%u", (unsigned int)data[0]) >= str_size) + return AVERROR(ENOMEM); + break; + case 76: // BE 16-bit Unsigned Integer + if (data_size != 2) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%u", (unsigned int)AV_RB16(data)) >= str_size) + return AVERROR(ENOMEM); + break; + case 77: // BE 32-bit Unsigned Integer + if (data_size != 4) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%u", (unsigned int)AV_RB32(data)) >= str_size) + return AVERROR(ENOMEM); + break; + case 78: // BE 64-bit Unsigned Integer + if (data_size != 8) + return AVERROR(EINVAL); + if (snprintf(str, str_size, "%llu", (unsigned long long)AV_RB64(data)) >= str_size) + return AVERROR(ENOMEM); + break; + default: + return AVERROR(EINVAL); + } + + return 0; +} + +int av_qtff_convert_str_to_well_known(int data_type, const char *str, uint8_t *data, int data_size) +{ + if (!str || !data) + return AVERROR(EINVAL); + + switch (data_type) { + case 0: // Reserved + case 2: // UTF-16 + case 3: // S/JIS + case 4: // UTF-8 sort + case 5: // UTF-16 sort + case 13: // JPEG + case 14: // PNG + case 27: // BMP + case 28: // QuickTime Metadata atom + case 70: // BE PointF32 + case 71: // BE DimensionsF32 + case 72: // BE RectF32 + case 79: // AffineTransformF64 + return AVERROR_PATCHWELCOME; // these are defined well-known types, but not implemented to be parsed + case 1: { // UTF-8 + int len = strlen(str); + if (len > data_size) + return AVERROR(ENOMEM); + memcpy(data, str, len); + return len; + } + case 21: { // BE Signed Integer (variable size, not usable for timed metadata) + long long val; + char *endptr; + + val = strtoll(str, &endptr, 10); + if (endptr == str || *endptr != '\0') + return AVERROR(EINVAL); + + switch (data_size) { + case 1: + if (val < INT8_MIN || val > INT8_MAX) + return AVERROR(ERANGE); + AV_WB8(data, (uint8_t)val); + break; + case 2: + if (val < INT16_MIN || val > INT16_MAX) + return AVERROR(ERANGE); + AV_WB16(data, (int16_t)val); + break; + case 3: + if (val < -8388608 || val > 8388607) // 24-bit signed range + return AVERROR(ERANGE); + AV_WB24(data, (int32_t)val); + break; + case 4: + if (val < INT32_MIN || val > INT32_MAX) + return AVERROR(ERANGE); + AV_WB32(data, (int32_t)val); + break; + default: + return AVERROR(EINVAL); + } + break; + } + case 22: { // BE unsigned integer, variable size + unsigned long long val; + char *endptr; + + val = strtoull(str, &endptr, 10); + if (endptr == str || *endptr != '\0') + return AVERROR(EINVAL); + + switch (data_size) { + case 1: + if (val > UINT8_MAX) + return AVERROR(ERANGE); + AV_WB8(data, (uint8_t)val); + break; + case 2: + if (val > UINT16_MAX) + return AVERROR(ERANGE); + AV_WB16(data, (uint16_t)val); + break; + case 3: + if (val > 16777215) // 24-bit unsigned range + return AVERROR(ERANGE); + AV_WB24(data, (uint32_t)val); + break; + case 4: + if (val > UINT32_MAX) + return AVERROR(ERANGE); + AV_WB32(data, (uint32_t)val); + break; + default: + return AVERROR(EINVAL); + } + break; + } + case 23: { // BE float32 + float val; + char *endptr; + + if (data_size != 4) + return AVERROR(EINVAL); + + val = strtof(str, &endptr); + if (endptr == str || *endptr != '\0') + return AVERROR(EINVAL); + + AV_WB32(data, av_float2int(val)); + break; + } + case 24: { // BE float64 + double val; + char *endptr; + + if (data_size != 8) + return AVERROR(EINVAL); + + val = strtod(str, &endptr); + if (endptr == str || *endptr != '\0') + return AVERROR(EINVAL); + + AV_WB64(data, av_double2int(val)); + break; + } + case 65: // 8-bit Signed Integer + case 66: // BE 16-bit Signed Integer + case 67: // BE 32-bit Signed Integer + case 74: { // BE 64-bit Signed Integer + long long val; + char *endptr; + int expected_size = (data_type == 65) ? 1 : (data_type == 66) ? 2 : (data_type == 67) ? 4 : 8; + + if (data_size != expected_size) + return AVERROR(EINVAL); + + val = strtoll(str, &endptr, 10); + if (endptr == str || *endptr != '\0') + return AVERROR(EINVAL); + + switch (data_type) { + case 65: + if (val < INT8_MIN || val > INT8_MAX) + return AVERROR(ERANGE); + AV_WB8(data, (uint8_t)val); + break; + case 66: + if (val < INT16_MIN || val > INT16_MAX) + return AVERROR(ERANGE); + AV_WB16(data, (int16_t)val); + break; + case 67: + if (val < INT32_MIN || val > INT32_MAX) + return AVERROR(ERANGE); + AV_WB32(data, (int32_t)val); + break; + case 74: + AV_WB64(data, (int64_t)val); + break; + } + break; + } + case 75: // 8-bit Unsigned Integer + case 76: // BE 16-bit Unsigned Integer + case 77: // BE 32-bit Unsigned Integer + case 78: { // BE 64-bit Unsigned Integer + unsigned long long val; + char *endptr; + int expected_size = (data_type == 75) ? 1 : (data_type == 76) ? 2 : (data_type == 77) ? 4 : 8; + + if (data_size != expected_size) + return AVERROR(EINVAL); + + val = strtoull(str, &endptr, 10); + if (endptr == str || *endptr != '\0') + return AVERROR(EINVAL); + + switch (data_type) { + case 75: + if (val > UINT8_MAX) + return AVERROR(ERANGE); + AV_WB8(data, (uint8_t)val); + break; + case 76: + if (val > UINT16_MAX) + return AVERROR(ERANGE); + AV_WB16(data, (uint16_t)val); + break; + case 77: + if (val > UINT32_MAX) + return AVERROR(ERANGE); + AV_WB32(data, (uint32_t)val); + break; + case 78: + AV_WB64(data, (uint64_t)val); + break; + } + break; + } + default: + return AVERROR(EINVAL); + } + + return data_size; +} diff --git a/libavutil/qtff.h b/libavutil/qtff.h new file mode 100644 index 0000000000..912ea9e870 --- /dev/null +++ b/libavutil/qtff.h @@ -0,0 +1,59 @@ +/* + * copyright (c) 2025 Lukas Holliger + * + * 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 + */ + +#ifndef AVUTIL_QTFF_H +#define AVUTIL_QTFF_H + +#include <stdint.h> + +/** + * @file + * QuickTime File Format (QTFF) utilities + */ + +/** + * Convert a QuickTime well-known type to a string + * + * @param data_type QuickTime metadata data type + * @param data Pointer to the binary data + * @param data_size Size of the binary data in bytes + * @param str Buffer to write the string representation to + * @param str_size Size of the output buffer + * @return 0 on success, negative AVERROR code on failure + * + * @see https://developer.apple.com/documentation/quicktime-file-format/well-known_types + */ +int av_qtff_convert_well_known_to_str(int data_type, const uint8_t *data, int data_size, + char *str, int str_size); + +/** + * Convert a string to QuickTime well-known value + * + * @param data_type QuickTime metadata data type + * @param str Input string to convert + * @param data Buffer to write the binary data to + * @param data_size Size to use for the binary data + * @return Number of bytes written on success, negative AVERROR code on failure + * + * @see https://developer.apple.com/documentation/quicktime-file-format/well-known_types + */ +int av_qtff_convert_str_to_well_known(int data_type, const char *str, uint8_t *data, int data_size); + +#endif /* AVUTIL_QTFF_H */ -- 2.49.1 >From 3616592b585440ca4984052e53c85ff5df29f3bf Mon Sep 17 00:00:00 2001 From: lholliger <[email protected]> Date: Wed, 5 Nov 2025 14:33:22 -0500 Subject: [PATCH 2/2] libavformat/mov: use qtff decoder util There was previously a segment here to attempt to decode some of the well-known QTFF types but in order to simplify the function and implement more types this segment can be replaced with a call to the qtff util. Signed-off-by: lholliger <[email protected]> --- libavformat/mov.c | 78 +++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 50 deletions(-) diff --git a/libavformat/mov.c b/libavformat/mov.c index 45c562cdc6..9cf62955c2 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -45,6 +45,7 @@ #include "libavutil/aes.h" #include "libavutil/aes_ctr.h" #include "libavutil/pixdesc.h" +#include "libavutil/qtff.h" #include "libavutil/sha.h" #include "libavutil/spherical.h" #include "libavutil/stereo3d.h" @@ -521,61 +522,38 @@ retry: else { if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded mov_read_mac_string(c, pb, str_size, str, str_size_alloc); - } else if (data_type == 21) { // BE signed integer, variable size - int val = 0; - if (str_size == 1) - val = (int8_t)avio_r8(pb); - else if (str_size == 2) - val = (int16_t)avio_rb16(pb); - else if (str_size == 3) - val = ((int32_t)(avio_rb24(pb)<<8))>>8; - else if (str_size == 4) - val = (int32_t)avio_rb32(pb); - if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) { - av_log(c->fc, AV_LOG_ERROR, - "Failed to store the number (%d) in string.\n", val); - av_free(str); - return AVERROR_INVALIDDATA; - } - } else if (data_type == 22) { // BE unsigned integer, variable size - unsigned int val = 0; - if (str_size == 1) - val = avio_r8(pb); - else if (str_size == 2) - val = avio_rb16(pb); - else if (str_size == 3) - val = avio_rb24(pb); - else if (str_size == 4) - val = avio_rb32(pb); - if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) { - av_log(c->fc, AV_LOG_ERROR, - "Failed to store the number (%u) in string.\n", val); - av_free(str); - return AVERROR_INVALIDDATA; - } - } else if (data_type == 23 && str_size >= 4) { // BE float32 - float val = av_int2float(avio_rb32(pb)); - if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) { - av_log(c->fc, AV_LOG_ERROR, - "Failed to store the float32 number (%f) in string.\n", val); - av_free(str); - return AVERROR_INVALIDDATA; - } - } else if (data_type > 1 && data_type != 4) { - // data_type can be 0 if not set at all above. data_type 1 means - // UTF8 and 4 means "UTF8 sort". For any other type (UTF16 or e.g. - // a picture), don't return it blindly in a string that is supposed - // to be UTF8 text. - av_log(c->fc, AV_LOG_WARNING, "Skipping unhandled metadata %s of type %d\n", key, data_type); - av_free(str); - return 0; } else { - int ret = ffio_read_size(pb, str, str_size); + uint8_t *data_buf = av_malloc(str_size); + int ret; + + if (!data_buf) { + av_free(str); + return AVERROR(ENOMEM); + } + + ret = ffio_read_size(pb, data_buf, str_size); if (ret < 0) { + av_free(data_buf); + av_free(str); + return ret; + } + + // Data types 0 and 4 are technically reserved and a special case, but were previously + // decoded to UTF-8 here. + ret = av_qtff_convert_well_known_to_str(data_type == 0 || data_type == 4 ? 1 : data_type, + data_buf, str_size, str, str_size_alloc); + av_free(data_buf); + + if (ret == AVERROR_PATCHWELCOME || ret == AVERROR(EINVAL)) { + av_log(c->fc, AV_LOG_WARNING, "Skipping unhandled metadata %s of type %d\n", key, data_type); + av_free(str); + return 0; + } else if (ret < 0) { + av_log(c->fc, AV_LOG_ERROR, + "Failed to convert metadata %s of type %d to string.\n", key, data_type); av_free(str); return ret; } - str[str_size] = 0; } c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; av_dict_set(metadata, key, str, 0); -- 2.49.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
