[FFmpeg-devel] [PATCHv5] lavf: add libopenmpt demuxer

Josh de Kock josh at itanimul.li
Sun Jul 10 17:12:01 EEST 2016


Fixes ticket #5623

TODO: bump lavf minor
---
 Changelog                |   1 +
 configure                |   4 ++
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/libopenmpt.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 182 insertions(+)
 create mode 100644 libavformat/libopenmpt.c

diff --git a/Changelog b/Changelog
index 5b34d34..f2a1dcd 100644
--- a/Changelog
+++ b/Changelog
@@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
 releases are sorted from youngest to oldest.
 
 version <next>:
+- libopenmpt demuxer
 
 
 version 3.1:
diff --git a/configure b/configure
index e015c75..0c7b5e9 100755
--- a/configure
+++ b/configure
@@ -245,6 +245,7 @@ External library support:
   --enable-libopencv       enable video filtering via libopencv [no]
   --enable-libopenh264     enable H.264 encoding via OpenH264 [no]
   --enable-libopenjpeg     enable JPEG 2000 de/encoding via OpenJPEG [no]
+  --enable-libopenmpt      enable decoding tracked files via libopenmpt [no]
   --enable-libopus         enable Opus de/encoding via libopus [no]
   --enable-libpulse        enable Pulseaudio input via libpulse [no]
   --enable-librubberband   enable rubberband needed for rubberband filter [no]
@@ -1505,6 +1506,7 @@ EXTERNAL_LIBRARY_LIST="
     libopencv
     libopenh264
     libopenjpeg
+    libopenmpt
     libopus
     libpulse
     librtmp
@@ -2770,6 +2772,7 @@ libopencore_amrwb_decoder_deps="libopencore_amrwb"
 libopenh264_encoder_deps="libopenh264"
 libopenjpeg_decoder_deps="libopenjpeg"
 libopenjpeg_encoder_deps="libopenjpeg"
+libopenmpt_demuxer_deps="libopenmpt"
 libopus_decoder_deps="libopus"
 libopus_encoder_deps="libopus"
 libopus_encoder_select="audio_frame_queue"
@@ -5679,6 +5682,7 @@ enabled libopenjpeg       && { check_lib openjpeg-2.1/openjpeg.h opj_version -lo
                                check_lib openjpeg-1.5/openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC ||
                                check_lib openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC ||
                                die "ERROR: libopenjpeg not found"; }
+enabled libopenmpt        && require_pkg_config "libopenmpt >= 0.2.6557" libopenmpt/libopenmpt.h openmpt_module_create
 enabled libopus           && require_pkg_config opus opus_multistream.h opus_multistream_decoder_create
 enabled libpulse          && require_pkg_config libpulse pulse/pulseaudio.h pa_context_new
 enabled librtmp           && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
diff --git a/libavformat/Makefile b/libavformat/Makefile
index c49f9de..6451c1c 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -527,6 +527,7 @@ OBJS-$(CONFIG_LIBGME_DEMUXER)            += libgme.o
 OBJS-$(CONFIG_LIBMODPLUG_DEMUXER)        += libmodplug.o
 OBJS-$(CONFIG_LIBNUT_DEMUXER)            += libnut.o
 OBJS-$(CONFIG_LIBNUT_MUXER)              += libnut.o
+OBJS-$(CONFIG_LIBOPENMPT_DEMUXER)        += libopenmpt.o
 OBJS-$(CONFIG_LIBRTMP)                   += librtmp.o
 OBJS-$(CONFIG_LIBSSH_PROTOCOL)           += libssh.o
 OBJS-$(CONFIG_LIBSMBCLIENT_PROTOCOL)     += libsmbclient.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index d490cc4..58c33a6 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -375,6 +375,7 @@ void av_register_all(void)
     REGISTER_DEMUXER (LIBGME,           libgme);
     REGISTER_DEMUXER (LIBMODPLUG,       libmodplug);
     REGISTER_MUXDEMUX(LIBNUT,           libnut);
+    REGISTER_DEMUXER (LIBOPENMPT,       libopenmpt);
 
     initialized = 1;
 }
diff --git a/libavformat/libopenmpt.c b/libavformat/libopenmpt.c
new file mode 100644
index 0000000..a237ad6
--- /dev/null
+++ b/libavformat/libopenmpt.c
@@ -0,0 +1,175 @@
+/*
+ * Tracked MOD demuxer (libopenmpt)
+ * Copyright (c) 2016 Josh de Kock
+ *
+ * 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 <libopenmpt/libopenmpt.h>
+#include <libopenmpt/libopenmpt_stream_callbacks_file.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct OpenMPTContext {
+    const AVClass *class;
+    openmpt_module *module;
+
+    int channels;
+    double duration;
+    /* options */
+    int sample_rate;
+    int64_t layout;
+} 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(layout),      AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_STEREO},     0,    INT64_MAX, A|D},
+    {NULL}
+};
+
+static void openmpt_logfunc(const char *message, void *userdata)
+{
+    int level = AV_LOG_INFO;
+    if (strstr(message, "ERROR") != NULL) {
+        level = AV_LOG_ERROR;
+    }
+    av_log(userdata, level, "%s\n", message);
+}
+
+#define add_meta(s, name, meta)                    \
+do {                                               \
+    const char *value = meta;                      \
+    if (value && value[0])                         \
+        av_dict_set(&s->metadata, name, value, 0); \
+} while(0)
+
+static int read_header_openmpt(AVFormatContext *s)
+{
+    AVStream *st;
+    OpenMPTContext *openmpt = s->priv_data;
+    int64_t size = avio_size(s->pb);
+    char *buf = av_malloc(size);
+
+    if (!buf)
+        return AVERROR(ENOMEM);
+    size = avio_read(s->pb, buf, size);
+
+    openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
+    av_freep(&buf);
+    if (!openmpt->module)
+            return AVERROR(ENOMEM);
+
+    openmpt->channels   = av_get_channel_layout_nb_channels(openmpt->layout);
+    openmpt->duration   = openmpt_module_get_duration_seconds(openmpt->module);
+
+    add_meta(s, "artist",  openmpt_module_get_metadata(openmpt->module, "artist"));
+    add_meta(s, "title",   openmpt_module_get_metadata(openmpt->module, "title"));
+    add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
+    add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    avpriv_set_pts_info(st, 64, 1, 1000);
+    if (st->duration > 0)
+        st->duration = llrint(openmpt->duration*AV_TIME_BASE);
+
+    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id    = AV_NE(AV_CODEC_ID_PCM_F32BE, AV_CODEC_ID_PCM_F32LE);
+    st->codecpar->channels    = openmpt->channels;
+    st->codecpar->sample_rate = openmpt->sample_rate;
+
+    return 0;
+}
+
+#define AUDIO_PKT_SIZE 2048
+
+static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
+{
+    OpenMPTContext *openmpt = s->priv_data;
+    int n_samples = AUDIO_PKT_SIZE / (openmpt->channels ? openmpt->channels*4 : 4);
+    int ret;
+
+    if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
+        return ret;
+
+    switch (openmpt->channels) {
+    case 1:
+        ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
+                                             n_samples, (float *)pkt->data);
+        break;
+    case 2:
+        ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
+                                                           n_samples, (float *)pkt->data);
+        break;
+    case 4:
+        ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
+                                                         n_samples, (float *)pkt->data);
+        break;
+    default:
+        av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->channels);
+        return AVERROR(EINVAL);
+    }
+
+    if (ret < 1) {
+        pkt->size = 0;
+        return AVERROR_EOF;
+    }
+
+    pkt->size = ret * (openmpt->channels * 4);
+
+    return 0;
+}
+
+static int read_close_openmpt(AVFormatContext *s)
+{
+    OpenMPTContext *openmpt = s->priv_data;
+    openmpt_module_destroy(openmpt->module);
+    return 0;
+}
+
+static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
+{
+    OpenMPTContext *openmpt = s->priv_data;
+    openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
+    return 0;
+}
+
+static const AVClass class_openmpt = {
+    .class_name = "libopenmpt",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_libopenmpt_demuxer = {
+    .name           = "libopenmpt",
+    .long_name      = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
+    .priv_data_size = sizeof(OpenMPTContext),
+    .read_header    = read_header_openmpt,
+    .read_packet    = read_packet_openmpt,
+    .read_close     = read_close_openmpt,
+    .read_seek      = read_seek_openmpt,
+    .priv_class     = &class_openmpt,
+    .extensions     = "669,amf,ams,dbm,digi,dmf,dsm,far,gdm,imf,it,j2b,m15,mdl,med,mmcmp,mms,mo3,mod,mptm,mt2,mtm,nst,okt,plm,ppm,psm,pt36,ptm,s3m,sfx,sfx2,stk,stm,ult,umx,wow,xm,xpk",
+};
-- 
2.7.4 (Apple Git-66)



More information about the ffmpeg-devel mailing list