Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Paper via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Paper <paper@tflc.us>
Subject: [FFmpeg-devel] [PATCH] avformat/libopenmpt: implement file reading callbacks
Date: Thu,  4 Sep 2025 10:08:40 -0400
Message-ID: <20250904140840.6590-1-paper@tflc.us> (raw)

Before, this code would unnecessarily waste quite a bit of memory
reading the entire file in, and in many cases just freeing the
result and throwing it out anyway.

This commit changes the behavior to use libopenmpt's callback
structure. This is much more memory efficient, especially for
particularly large files.

Both pre-0.3.0 and post-0.3.0 preprocessor branches have been
tested working with a plain amiga-style 'M.K.' module in ffplay.

Signed-off-by: Paper <paper@tflc.us>
---
 libavformat/libopenmpt.c | 93 +++++++++++++++++++++++++++++-----------
 1 file changed, 68 insertions(+), 25 deletions(-)

diff --git a/libavformat/libopenmpt.c b/libavformat/libopenmpt.c
index 46af4bff22..2c8aa2659a 100644
--- a/libavformat/libopenmpt.c
+++ b/libavformat/libopenmpt.c
@@ -46,17 +46,24 @@ typedef struct OpenMPTContext {
     int sample_rate;
     AVChannelLayout ch_layout;
     int subsong;
+    int interp;
 } OpenMPTContext;
 
 #define OFFSET(x) offsetof(OpenMPTContext, x)
 #define A AV_OPT_FLAG_AUDIO_PARAM
 #define D AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
-    { "sample_rate", "set sample rate",    OFFSET(sample_rate), AV_OPT_TYPE_INT,            { .i64 = 48000 },               1000, INT_MAX,   A | D },
-    { "layout",      "set channel layout", OFFSET(ch_layout),   AV_OPT_TYPE_CHLAYOUT,       { .str = "stereo" },            0,    0,         A | D },
-    { "subsong",     "set subsong",        OFFSET(subsong),     AV_OPT_TYPE_INT,            { .i64 = -2 },                  -2,   INT_MAX,   A | D, .unit = "subsong"},
-    { "all",         "all",                0,                   AV_OPT_TYPE_CONST,          { .i64 = -1},                   0,    0,         A | D, .unit = "subsong" },
-    { "auto",        "auto",               0,                   AV_OPT_TYPE_CONST,          { .i64 = -2},                   0,    0,         A | D, .unit = "subsong" },
+    { "sample_rate",   "set sample rate",          OFFSET(sample_rate), AV_OPT_TYPE_INT,      { .i64 = 48000 },    1000, INT_MAX, A | D },
+    { "layout",        "set channel layout",       OFFSET(ch_layout),   AV_OPT_TYPE_CHLAYOUT, { .str = "stereo" }, 0,    0,       A | D },
+    { "subsong",       "set subsong",              OFFSET(subsong),     AV_OPT_TYPE_INT,      { .i64 = -2 },       -2,   INT_MAX, A | D, .unit = "subsong"},
+    { "all",           "all",                      0,                   AV_OPT_TYPE_CONST,    { .i64 = -1 },       0,    0,       A | D, .unit = "subsong" },
+    { "auto",          "auto",                     0,                   AV_OPT_TYPE_CONST,    { .i64 = -2 },       0,    0,       A | D, .unit = "subsong" },
+    { "interpolation", "set interpolation method", OFFSET(interp),      AV_OPT_TYPE_INT,      { .i64 = 0 },        0,    INT_MAX, A | D, .unit = "interpolation"},
+    { "default",       "default",                  0,                   AV_OPT_TYPE_CONST,    { .i64 = 0 },        0,    0,       A | D, .unit = "interpolation"},
+    { "none",          "none",                     0,                   AV_OPT_TYPE_CONST,    { .i64 = 1 },        0,    0,       A | D, .unit = "interpolation"},
+    { "linear",        "linear",                   0,                   AV_OPT_TYPE_CONST,    { .i64 = 2 },        0,    0,       A | D, .unit = "interpolation"},
+    { "cubic",         "cubic",                    0,                   AV_OPT_TYPE_CONST,    { .i64 = 4 },        0,    0,       A | D, .unit = "interpolation"},
+    { "8-tap-sinc",    "8-tap windowed sinc",      0,                   AV_OPT_TYPE_CONST,    { .i64 = 8 },        0,    0,       A | D, .unit = "interpolation"},
     { NULL }
 };
 
@@ -69,6 +76,51 @@ static void openmpt_logfunc(const char *message, void *userdata)
     av_log(userdata, level, "%s\n", message);
 }
 
+static size_t av_openmpt_read(void *stream, void *dst, size_t bytes)
+{
+    AVIOContext *ioctx = stream;
+    int r;
+
+    /* avio_read takes an int, cap the size accordingly */
+    if (bytes > (size_t)INT_MAX)
+        bytes = (size_t)INT_MAX;
+
+    r = avio_read(ioctx, dst, bytes);
+
+    return (r >= 0) ? r : 0;
+}
+
+static int av_openmpt_seek(void *stream, int64_t offset, int whence)
+{
+    AVIOContext *ioctx = stream;
+    int whence_av;
+
+    switch (whence) {
+    case OPENMPT_STREAM_SEEK_SET: whence_av = SEEK_SET; break;
+    case OPENMPT_STREAM_SEEK_CUR: whence_av = SEEK_CUR; break;
+    case OPENMPT_STREAM_SEEK_END: whence_av = SEEK_END; break;
+    /* invalid value, punt */
+    default: return -1;
+    }
+
+    /* openmpt expects stdio-style seek; don't return new position */
+    return (avio_seek(ioctx, offset, whence_av) < 0) ? -1 : 0;
+}
+
+static int64_t av_openmpt_tell(void *stream)
+{
+    AVIOContext *ioctx = stream;
+    int64_t r = avio_tell(ioctx);
+
+    return (r < 0) ? -1 : r;
+}
+
+static const openmpt_stream_callbacks openmpt_cbs = {
+    av_openmpt_read,
+    av_openmpt_seek,
+    av_openmpt_tell
+};
+
 #define add_meta(s, name, meta)                    \
 do {                                               \
     const char *value = meta;                      \
@@ -81,30 +133,14 @@ static int read_header_openmpt(AVFormatContext *s)
 {
     AVStream *st;
     OpenMPTContext *openmpt = s->priv_data;
-    int64_t size;
-    char *buf;
 #if OPENMPT_API_VERSION_AT_LEAST(0,3,0)
     int error;
 #endif
     int ret;
 
-    size = avio_size(s->pb);
-    if (size <= 0)
-        return AVERROR_INVALIDDATA;
-    buf = av_malloc(size);
-    if (!buf)
-        return AVERROR(ENOMEM);
-    size = avio_read(s->pb, buf, size);
-    if (size < 0) {
-        av_log(s, AV_LOG_ERROR, "Reading input buffer failed.\n");
-        av_freep(&buf);
-        return size;
-    }
-
 #if OPENMPT_API_VERSION_AT_LEAST(0,3,0)
     error = OPENMPT_ERROR_OK;
-    openmpt->module = openmpt_module_create_from_memory2(buf, size, openmpt_logfunc, s, NULL, NULL, &error, NULL, NULL);
-    av_freep(&buf);
+    openmpt->module = openmpt_module_create2(openmpt_cbs, s->pb, openmpt_logfunc, s, NULL, NULL, &error, NULL, NULL);
     if (!openmpt->module) {
         if (error == OPENMPT_ERROR_OUT_OF_MEMORY)
             return AVERROR(ENOMEM);
@@ -114,12 +150,19 @@ static int read_header_openmpt(AVFormatContext *s)
             return AVERROR_UNKNOWN;
     }
 #else
-    openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
-    av_freep(&buf);
+    openmpt->module = openmpt_module_create(openmpt_cbs, s->pb, openmpt_logfunc, s, NULL);
     if (!openmpt->module)
-            return AVERROR_INVALIDDATA;
+        return AVERROR_INVALIDDATA;
 #endif
 
+    ret = openmpt_module_set_render_param(openmpt->module,
+        OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH,
+        openmpt->interp);
+    if (!ret) {
+        av_log(s, AV_LOG_ERROR, "Invalid interpolation setting: %d\n", openmpt->interp);
+        return AVERROR(EINVAL);
+    }
+
     if (openmpt->subsong >= openmpt_module_get_num_subsongs(openmpt->module)) {
         av_log(s, AV_LOG_ERROR, "Invalid subsong index: %d\n", openmpt->subsong);
         return AVERROR(EINVAL);
-- 
2.51.0

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2025-09-04 14:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250904140840.6590-1-paper@tflc.us \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=paper@tflc.us \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git