FFmpeg
Loading...
Searching...
No Matches
libopenmpt.c
Go to the documentation of this file.
1/*
2 * Tracked MOD demuxer (libopenmpt)
3 * Copyright (c) 2016 Josh de Kock
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <libopenmpt/libopenmpt.h>
23#include <libopenmpt/libopenmpt_stream_callbacks_file.h>
24#include <libopenmpt/libopenmpt_version.h>
25/* Shims to support libopenmpt < 0.3.0 (as documented by libopenmpt) */
26#if !defined(OPENMPT_API_VERSION_MAKE)
27#define OPENMPT_API_VERSION_MAKE(major, minor, patch) (((major)<<24)|((minor)<<16)|((patch)<<0))
28#endif
29#if !defined(OPENMPT_API_VERSION_AT_LEAST)
30#define OPENMPT_API_VERSION_AT_LEAST(major, minor, patch) (OPENMPT_API_VERSION >= OPENMPT_API_VERSION_MAKE((major), (minor), (patch)))
31#endif
32
34#include "libavutil/mem.h"
35#include "libavutil/opt.h"
36#include "avformat.h"
37#include "demux.h"
38#include "internal.h"
39
40typedef struct OpenMPTContext {
41 const AVClass *class;
42 openmpt_module *module;
43
44 double duration;
45 /* options */
50
51#define OFFSET(x) offsetof(OpenMPTContext, x)
52#define A AV_OPT_FLAG_AUDIO_PARAM
53#define D AV_OPT_FLAG_DECODING_PARAM
54static const AVOption options[] = {
55 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 48000 }, 1000, INT_MAX, A | D },
56 { "layout", "set channel layout", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, { .str = "stereo" }, 0, 0, A | D },
57 { "subsong", "set subsong", OFFSET(subsong), AV_OPT_TYPE_INT, { .i64 = -2 }, -2, INT_MAX, A | D, .unit = "subsong"},
58 { "all", "all", 0, AV_OPT_TYPE_CONST, { .i64 = -1}, 0, 0, A | D, .unit = "subsong" },
59 { "auto", "auto", 0, AV_OPT_TYPE_CONST, { .i64 = -2}, 0, 0, A | D, .unit = "subsong" },
60 { NULL }
61};
62
63static void openmpt_logfunc(const char *message, void *userdata)
64{
65 int level = AV_LOG_INFO;
66 if (strstr(message, "ERROR") != NULL) {
68 }
69 av_log(userdata, level, "%s\n", message);
70}
71
72#define add_meta(s, name, meta) \
73do { \
74 const char *value = meta; \
75 if (value && value[0]) \
76 av_dict_set(&s->metadata, name, value, 0); \
77 openmpt_free_string(value); \
78} while(0)
79
81{
82 AVStream *st;
83 OpenMPTContext *openmpt = s->priv_data;
85 char *buf;
86#if OPENMPT_API_VERSION_AT_LEAST(0,3,0)
87 int error;
88#endif
89 int ret;
90
91 size = avio_size(s->pb);
92 if (size <= 0)
94 buf = av_malloc(size);
95 if (!buf)
96 return AVERROR(ENOMEM);
97 size = avio_read(s->pb, buf, size);
98 if (size < 0) {
99 av_log(s, AV_LOG_ERROR, "Reading input buffer failed.\n");
100 av_freep(&buf);
101 return size;
102 }
103
104#if OPENMPT_API_VERSION_AT_LEAST(0,3,0)
105 error = OPENMPT_ERROR_OK;
106 openmpt->module = openmpt_module_create_from_memory2(buf, size, openmpt_logfunc, s, NULL, NULL, &error, NULL, NULL);
107 av_freep(&buf);
108 if (!openmpt->module) {
109 if (error == OPENMPT_ERROR_OUT_OF_MEMORY)
110 return AVERROR(ENOMEM);
111 else if (error >= OPENMPT_ERROR_GENERAL)
112 return AVERROR_INVALIDDATA;
113 else
114 return AVERROR_UNKNOWN;
115 }
116#else
117 openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
118 av_freep(&buf);
119 if (!openmpt->module)
120 return AVERROR_INVALIDDATA;
121#endif
122
123 if (openmpt->subsong >= openmpt_module_get_num_subsongs(openmpt->module)) {
124 av_log(s, AV_LOG_ERROR, "Invalid subsong index: %d\n", openmpt->subsong);
125 return AVERROR(EINVAL);
126 }
127
128 if (openmpt->subsong != -2) {
129 if (openmpt->subsong >= 0) {
130 av_dict_set_int(&s->metadata, "track", openmpt->subsong + 1, 0);
131 }
132 ret = openmpt_module_select_subsong(openmpt->module, openmpt->subsong);
133 if (!ret){
134 av_log(s, AV_LOG_ERROR, "Could not select requested subsong: %d", openmpt->subsong);
135 return AVERROR(EINVAL);
136 }
137 }
138
139 openmpt->duration = openmpt_module_get_duration_seconds(openmpt->module);
140
141 add_meta(s, "artist", openmpt_module_get_metadata(openmpt->module, "artist"));
142 add_meta(s, "title", openmpt_module_get_metadata(openmpt->module, "title"));
143 add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
144 add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
145 add_meta(s, "date", openmpt_module_get_metadata(openmpt->module, "date"));
146
148 if (!st)
149 return AVERROR(ENOMEM);
151 if (openmpt->duration >= 0 && openmpt->duration < ((double)INT64_MAX + 1) / AV_TIME_BASE)
152 st->duration = llrint(openmpt->duration*AV_TIME_BASE);
153
156 st->codecpar->sample_rate = openmpt->sample_rate;
157 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &openmpt->ch_layout);
158 if (ret < 0)
159 return ret;
160
161 return 0;
162}
163
164#define AUDIO_PKT_SIZE 2048
165
167{
168 OpenMPTContext *openmpt = s->priv_data;
169 int n_samples = AUDIO_PKT_SIZE / (openmpt->ch_layout.nb_channels ? openmpt->ch_layout.nb_channels*4 : 4);
170 int ret;
171
172 if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
173 return ret;
174
175 double pos = openmpt_module_get_position_seconds(openmpt->module);
176
177 switch (openmpt->ch_layout.nb_channels) {
178 case 1:
179 ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
180 n_samples, (float *)pkt->data);
181 break;
182 case 2:
183 ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
184 n_samples, (float *)pkt->data);
185 break;
186 case 4:
187 ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
188 n_samples, (float *)pkt->data);
189 break;
190 default:
191 av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->ch_layout.nb_channels);
192 return AVERROR(EINVAL);
193 }
194
195 if (ret < 1) {
196 pkt->size = 0;
197 return AVERROR_EOF;
198 }
199
200 pkt->size = ret * (openmpt->ch_layout.nb_channels * 4);
201
202 if (pos >= 0 && pos < ((double)INT64_MAX + 1) / AV_TIME_BASE)
203 pkt->pts = llrint(pos * AV_TIME_BASE);
204
205 return 0;
206}
207
209{
210 OpenMPTContext *openmpt = s->priv_data;
211 if (openmpt->module) {
212 openmpt_module_destroy(openmpt->module);
213 openmpt->module = NULL;
214 }
215 return 0;
216}
217
218static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
219{
220 OpenMPTContext *openmpt = s->priv_data;
221 if (ts < 0)
222 ts = 0;
223 openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
224 return 0;
225}
226
228{
229 const char *ext;
230 if (p->filename) {
231 ext = strrchr(p->filename, '.');
232 if (ext && strlen(ext + 1) > 0) {
233 ext++; /* skip '.' */
234 if (openmpt_is_extension_supported(ext) == 1)
236 }
237 }
238 return 0;
239}
240
241static int read_probe_openmpt(const AVProbeData *p)
242{
243#if OPENMPT_API_VERSION_AT_LEAST(0,3,0)
244 int probe_result;
245 if (p->buf && p->buf_size > 0) {
246 probe_result = openmpt_probe_file_header_without_filesize(
247 OPENMPT_PROBE_FILE_HEADER_FLAGS_DEFAULT,
248 p->buf, p->buf_size,
250 if (probe_result == OPENMPT_PROBE_FILE_HEADER_RESULT_SUCCESS) {
251 /* As probing here relies on code external to FFmpeg, do not return
252 * AVPROBE_SCORE_MAX in order to reduce the impact in the rare
253 * cases of false positives.
254 */
255 return (AVPROBE_SCORE_MAX * 3) / 4 + 1;
256 } else if (probe_result == OPENMPT_PROBE_FILE_HEADER_RESULT_WANTMOREDATA) {
257 if (probe_openmpt_extension(p) > 0) {
258 return AVPROBE_SCORE_RETRY;
259 } else {
260 if (p->buf_size >= openmpt_probe_file_header_get_recommended_size()) {
261 /* We have already received the recommended amount of data
262 * and still cannot decide. Return a rather low score.
263 */
264 return AVPROBE_SCORE_RETRY / 2;
265 } else {
266 /* The file extension is unknown and we have very few data
267 * bytes available. libopenmpt cannot decide anything here,
268 * and returning any score > 0 would result in successful
269 * probing of random data.
270 */
271 return 0;
272 }
273 }
274 } else if (probe_result == OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE) {
275 return 0;
276 }
277 }
278#endif
279 /* for older libopenmpt, fall back to file extension probing */
280 return probe_openmpt_extension(p);
281}
282
283static const AVClass class_openmpt = {
284 .class_name = "libopenmpt",
285 .item_name = av_default_item_name,
286 .option = options,
287 .version = LIBAVUTIL_VERSION_INT,
288};
289
291 .p.name = "libopenmpt",
292 .p.long_name = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
293 .p.priv_class = &class_openmpt,
294#if OPENMPT_API_VERSION_AT_LEAST(0,3,0)
295 .p.extensions = "669,amf,ams,dbm,digi,dmf,dsm,dtm,far,gdm,ice,imf,it,j2b,m15,mdl,med,mmcmp,mms,mo3,mod,mptm,mt2,mtm,nst,okt,plm,ppm,psm,pt36,ptm,s3m,sfx,sfx2,st26,stk,stm,stp,ult,umx,wow,xm,xpk",
296#else
297 .p.extensions = "669,amf,ams,dbm,digi,dmf,dsm,far,gdm,ice,imf,it,j2b,m15,mdl,med,mmcmp,mms,mo3,mod,mptm,mt2,mtm,nst,okt,plm,ppm,psm,pt36,ptm,s3m,sfx,sfx2,st26,stk,stm,ult,umx,wow,xm,xpk",
298#endif
299 .priv_data_size = sizeof(OpenMPTContext),
300 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
306};
const FFInputFormat ff_libopenmpt_demuxer
Definition libopenmpt.c:290
#define A(x)
Definition vpx_arith.h:28
#define D
Definition avdct.c:35
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
#define AVPROBE_SCORE_RETRY
Definition avformat.h:478
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition opt.h:330
@ AV_CODEC_ID_PCM_F32LE
Definition codec_id.h:351
@ AV_CODEC_ID_PCM_F32BE
Definition codec_id.h:350
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:177
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define AUDIO_PKT_SIZE
Definition libgme.c:82
#define llrint(x)
Definition libm.h:396
static int read_close_openmpt(AVFormatContext *s)
Definition libopenmpt.c:208
static void openmpt_logfunc(const char *message, void *userdata)
Definition libopenmpt.c:63
#define add_meta(s, name, meta)
Definition libopenmpt.c:72
static int read_header_openmpt(AVFormatContext *s)
Definition libopenmpt.c:80
static int probe_openmpt_extension(const AVProbeData *p)
Definition libopenmpt.c:227
static const AVClass class_openmpt
Definition libopenmpt.c:283
static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
Definition libopenmpt.c:218
static int read_probe_openmpt(const AVProbeData *p)
Definition libopenmpt.c:241
#define OFFSET(x)
Definition libopenmpt.c:51
static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
Definition libopenmpt.c:166
#define AV_NE(be, le)
Definition macros.h:33
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
openmpt_module * module
Definition libopenmpt.c:42
double duration
Definition libopenmpt.c:44
AVChannelLayout ch_layout
Definition libopenmpt.c:47
uint8_t level
Definition svq3.c:208
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
int size