Browse code

lavf: add libopenmpt demuxer

Fixes ticket #5623

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Josh de Kock authored on 2016/07/15 02:30:27
Showing 6 changed files
... ...
@@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
2 2
 releases are sorted from youngest to oldest.
3 3
 
4 4
 version <next>:
5
+- libopenmpt demuxer
5 6
 
6 7
 
7 8
 version 3.1:
... ...
@@ -245,6 +245,7 @@ External library support:
245 245
   --enable-libopencv       enable video filtering via libopencv [no]
246 246
   --enable-libopenh264     enable H.264 encoding via OpenH264 [no]
247 247
   --enable-libopenjpeg     enable JPEG 2000 de/encoding via OpenJPEG [no]
248
+  --enable-libopenmpt      enable decoding tracked files via libopenmpt [no]
248 249
   --enable-libopus         enable Opus de/encoding via libopus [no]
249 250
   --enable-libpulse        enable Pulseaudio input via libpulse [no]
250 251
   --enable-librubberband   enable rubberband needed for rubberband filter [no]
... ...
@@ -1505,6 +1506,7 @@ EXTERNAL_LIBRARY_LIST="
1505 1505
     libopencv
1506 1506
     libopenh264
1507 1507
     libopenjpeg
1508
+    libopenmpt
1508 1509
     libopus
1509 1510
     libpulse
1510 1511
     librtmp
... ...
@@ -2770,6 +2772,7 @@ libopencore_amrwb_decoder_deps="libopencore_amrwb"
2770 2770
 libopenh264_encoder_deps="libopenh264"
2771 2771
 libopenjpeg_decoder_deps="libopenjpeg"
2772 2772
 libopenjpeg_encoder_deps="libopenjpeg"
2773
+libopenmpt_demuxer_deps="libopenmpt"
2773 2774
 libopus_decoder_deps="libopus"
2774 2775
 libopus_encoder_deps="libopus"
2775 2776
 libopus_encoder_select="audio_frame_queue"
... ...
@@ -5679,6 +5682,7 @@ enabled libopenjpeg       && { check_lib openjpeg-2.1/openjpeg.h opj_version -lo
5679 5679
                                check_lib openjpeg-1.5/openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC ||
5680 5680
                                check_lib openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC ||
5681 5681
                                die "ERROR: libopenjpeg not found"; }
5682
+enabled libopenmpt        && require_pkg_config "libopenmpt >= 0.2.6557" libopenmpt/libopenmpt.h openmpt_module_create
5682 5683
 enabled libopus           && require_pkg_config opus opus_multistream.h opus_multistream_decoder_create
5683 5684
 enabled libpulse          && require_pkg_config libpulse pulse/pulseaudio.h pa_context_new
5684 5685
 enabled librtmp           && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
... ...
@@ -527,6 +527,7 @@ OBJS-$(CONFIG_LIBGME_DEMUXER)            += libgme.o
527 527
 OBJS-$(CONFIG_LIBMODPLUG_DEMUXER)        += libmodplug.o
528 528
 OBJS-$(CONFIG_LIBNUT_DEMUXER)            += libnut.o
529 529
 OBJS-$(CONFIG_LIBNUT_MUXER)              += libnut.o
530
+OBJS-$(CONFIG_LIBOPENMPT_DEMUXER)        += libopenmpt.o
530 531
 OBJS-$(CONFIG_LIBRTMP)                   += librtmp.o
531 532
 OBJS-$(CONFIG_LIBSSH_PROTOCOL)           += libssh.o
532 533
 OBJS-$(CONFIG_LIBSMBCLIENT_PROTOCOL)     += libsmbclient.o
... ...
@@ -375,6 +375,7 @@ void av_register_all(void)
375 375
     REGISTER_DEMUXER (LIBGME,           libgme);
376 376
     REGISTER_DEMUXER (LIBMODPLUG,       libmodplug);
377 377
     REGISTER_MUXDEMUX(LIBNUT,           libnut);
378
+    REGISTER_DEMUXER (LIBOPENMPT,       libopenmpt);
378 379
 
379 380
     initialized = 1;
380 381
 }
381 382
new file mode 100644
... ...
@@ -0,0 +1,176 @@
0
+/*
1
+ * Tracked MOD demuxer (libopenmpt)
2
+ * Copyright (c) 2016 Josh de Kock
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <libopenmpt/libopenmpt.h>
22
+#include <libopenmpt/libopenmpt_stream_callbacks_file.h>
23
+
24
+#include "libavutil/avstring.h"
25
+#include "libavutil/opt.h"
26
+#include "avformat.h"
27
+#include "internal.h"
28
+
29
+typedef struct OpenMPTContext {
30
+    const AVClass *class;
31
+    openmpt_module *module;
32
+
33
+    int channels;
34
+    double duration;
35
+    /* options */
36
+    int sample_rate;
37
+    int64_t layout;
38
+} OpenMPTContext;
39
+
40
+#define OFFSET(x) offsetof(OpenMPTContext, x)
41
+#define A AV_OPT_FLAG_AUDIO_PARAM
42
+#define D AV_OPT_FLAG_DECODING_PARAM
43
+static const AVOption options[] = {
44
+    {"sample_rate", "set sample rate",    OFFSET(sample_rate), AV_OPT_TYPE_INT,            {.i64 = 48000},                   1000, INT_MAX,   A|D},
45
+    {"layout",      "set channel layout", OFFSET(layout),      AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_STEREO},     0,    INT64_MAX, A|D},
46
+    {NULL}
47
+};
48
+
49
+static void openmpt_logfunc(const char *message, void *userdata)
50
+{
51
+    int level = AV_LOG_INFO;
52
+    if (strstr(message, "ERROR") != NULL) {
53
+        level = AV_LOG_ERROR;
54
+    }
55
+    av_log(userdata, level, "%s\n", message);
56
+}
57
+
58
+#define add_meta(s, name, meta)                    \
59
+do {                                               \
60
+    const char *value = meta;                      \
61
+    if (value && value[0])                         \
62
+        av_dict_set(&s->metadata, name, value, 0); \
63
+    openmpt_free_string(value);                    \
64
+} while(0)
65
+
66
+static int read_header_openmpt(AVFormatContext *s)
67
+{
68
+    AVStream *st;
69
+    OpenMPTContext *openmpt = s->priv_data;
70
+    int64_t size = avio_size(s->pb);
71
+    char *buf = av_malloc(size);
72
+
73
+    if (!buf)
74
+        return AVERROR(ENOMEM);
75
+    size = avio_read(s->pb, buf, size);
76
+
77
+    openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
78
+    av_freep(&buf);
79
+    if (!openmpt->module)
80
+            return AVERROR_INVALIDDATA;
81
+
82
+    openmpt->channels   = av_get_channel_layout_nb_channels(openmpt->layout);
83
+    openmpt->duration   = openmpt_module_get_duration_seconds(openmpt->module);
84
+
85
+    add_meta(s, "artist",  openmpt_module_get_metadata(openmpt->module, "artist"));
86
+    add_meta(s, "title",   openmpt_module_get_metadata(openmpt->module, "title"));
87
+    add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
88
+    add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
89
+
90
+    st = avformat_new_stream(s, NULL);
91
+    if (!st)
92
+        return AVERROR(ENOMEM);
93
+    avpriv_set_pts_info(st, 64, 1, 1000);
94
+    if (st->duration > 0)
95
+        st->duration = llrint(openmpt->duration*AV_TIME_BASE);
96
+
97
+    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
98
+    st->codecpar->codec_id    = AV_NE(AV_CODEC_ID_PCM_F32BE, AV_CODEC_ID_PCM_F32LE);
99
+    st->codecpar->channels    = openmpt->channels;
100
+    st->codecpar->sample_rate = openmpt->sample_rate;
101
+
102
+    return 0;
103
+}
104
+
105
+#define AUDIO_PKT_SIZE 2048
106
+
107
+static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
108
+{
109
+    OpenMPTContext *openmpt = s->priv_data;
110
+    int n_samples = AUDIO_PKT_SIZE / (openmpt->channels ? openmpt->channels*4 : 4);
111
+    int ret;
112
+
113
+    if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
114
+        return ret;
115
+
116
+    switch (openmpt->channels) {
117
+    case 1:
118
+        ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
119
+                                             n_samples, (float *)pkt->data);
120
+        break;
121
+    case 2:
122
+        ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
123
+                                                           n_samples, (float *)pkt->data);
124
+        break;
125
+    case 4:
126
+        ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
127
+                                                         n_samples, (float *)pkt->data);
128
+        break;
129
+    default:
130
+        av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->channels);
131
+        return AVERROR(EINVAL);
132
+    }
133
+
134
+    if (ret < 1) {
135
+        pkt->size = 0;
136
+        return AVERROR_EOF;
137
+    }
138
+
139
+    pkt->size = ret * (openmpt->channels * 4);
140
+
141
+    return 0;
142
+}
143
+
144
+static int read_close_openmpt(AVFormatContext *s)
145
+{
146
+    OpenMPTContext *openmpt = s->priv_data;
147
+    openmpt_module_destroy(openmpt->module);
148
+    return 0;
149
+}
150
+
151
+static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
152
+{
153
+    OpenMPTContext *openmpt = s->priv_data;
154
+    openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
155
+    return 0;
156
+}
157
+
158
+static const AVClass class_openmpt = {
159
+    .class_name = "libopenmpt",
160
+    .item_name  = av_default_item_name,
161
+    .option     = options,
162
+    .version    = LIBAVUTIL_VERSION_INT,
163
+};
164
+
165
+AVInputFormat ff_libopenmpt_demuxer = {
166
+    .name           = "libopenmpt",
167
+    .long_name      = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
168
+    .priv_data_size = sizeof(OpenMPTContext),
169
+    .read_header    = read_header_openmpt,
170
+    .read_packet    = read_packet_openmpt,
171
+    .read_close     = read_close_openmpt,
172
+    .read_seek      = read_seek_openmpt,
173
+    .priv_class     = &class_openmpt,
174
+    .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",
175
+};
... ...
@@ -32,7 +32,7 @@
32 32
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
33 33
 // Also please add any ticket numbers that you belive might be affected here
34 34
 #define LIBAVFORMAT_VERSION_MAJOR  57
35
-#define LIBAVFORMAT_VERSION_MINOR  41
35
+#define LIBAVFORMAT_VERSION_MINOR  42
36 36
 #define LIBAVFORMAT_VERSION_MICRO 100
37 37
 
38 38
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \