2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @todo better probing than extensions matching
25 #include <libmodplug/modplug.h>
26 #include "libavutil/avstring.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/opt.h"
31 typedef struct ModPlugContext {
34 uint8_t *buf; ///< input file content
45 int max_size; ///< max file size to allocate
47 /* optional video stream */
48 double ts_per_packet; ///< used to define the pts/dts using packet_count;
49 int packet_count; ///< total number of audio packets
50 int print_textinfo; ///< bool flag for printing speed, tempo, order, ...
51 int video_stream; ///< 1 if the user want a video stream, otherwise 0
52 int w; ///< video stream width in char (one char = 8x8px)
53 int h; ///< video stream height in char (one char = 8x8px)
54 int video_switch; ///< 1 if current packet is video, otherwise 0
55 int fsize; ///< constant frame size
56 int linesize; ///< line size in bytes
57 char *color_eval; ///< color eval user input expression
58 AVExpr *expr; ///< parsed color eval expression
61 static const char *var_names[] = {
65 "speed", "tempo", "order", "pattern", "row",
73 VAR_SPEED, VAR_TEMPO, VAR_ORDER, VAR_PATTERN, VAR_ROW,
77 #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
78 #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
80 #define OFFSET(x) offsetof(ModPlugContext, x)
81 #define D AV_OPT_FLAG_DECODING_PARAM
82 static const AVOption options[] = {
83 {"noise_reduction", "Enable noise reduction 0(off)-1(on)", OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, D},
84 {"reverb_depth", "Reverb level 0(quiet)-100(loud)", OFFSET(reverb_depth), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
85 {"reverb_delay", "Reverb delay in ms, usually 40-200ms", OFFSET(reverb_delay), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, D},
86 {"bass_amount", "XBass level 0(quiet)-100(loud)", OFFSET(bass_amount), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
87 {"bass_range", "XBass cutoff in Hz 10-100", OFFSET(bass_range), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
88 {"surround_depth", "Surround level 0(quiet)-100(heavy)", OFFSET(surround_depth), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
89 {"surround_delay", "Surround delay in ms, usually 5-40ms", OFFSET(surround_delay), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, D},
90 {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
91 OFFSET(max_size), AV_OPT_TYPE_INT, {.dbl = FF_MODPLUG_DEF_FILE_SIZE}, 0, FF_MODPLUG_MAX_FILE_SIZE, D},
92 {"video_stream_expr", "Color formula", OFFSET(color_eval), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D},
93 {"video_stream", "Make demuxer output a video stream", OFFSET(video_stream), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, D},
94 {"video_stream_w", "Video stream width in char (one char = 8x8px)", OFFSET(w), AV_OPT_TYPE_INT, {.dbl = 30}, 20, 512, D},
95 {"video_stream_h", "Video stream height in char (one char = 8x8px)", OFFSET(h), AV_OPT_TYPE_INT, {.dbl = 30}, 20, 512, D},
96 {"video_stream_ptxt", "Print speed, tempo, order, ... in video stream", OFFSET(print_textinfo), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, D},
100 #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
101 if (modplug->opt) { \
102 settings.libopt = modplug->opt; \
103 settings.mFlags |= flag; \
107 #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
108 if (n_## entry_name ##s) { \
111 for (i = 0; i < n_## entry_name ##s; i++) { \
112 char item_name[64] = {0}; \
113 fname(f, i, item_name); \
117 av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
118 av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
122 extra = av_asprintf(", %u/%u " #entry_name "%s", \
123 n, n_## entry_name ##s, n > 1 ? "s" : ""); \
125 return AVERROR(ENOMEM); \
126 av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
131 static int modplug_load_metadata(AVFormatContext *s)
133 ModPlugContext *modplug = s->priv_data;
134 ModPlugFile *f = modplug->f;
136 const char *name = ModPlug_GetName(f);
137 const char *msg = ModPlug_GetMessage(f);
139 unsigned n_instruments = ModPlug_NumInstruments(f);
140 unsigned n_samples = ModPlug_NumSamples(f);
141 unsigned n_patterns = ModPlug_NumPatterns(f);
142 unsigned n_channels = ModPlug_NumChannels(f);
144 if (name && *name) av_dict_set(&s->metadata, "name", name, 0);
145 if (msg && *msg) av_dict_set(&s->metadata, "message", msg, 0);
147 extra = av_asprintf("%u pattern%s, %u channel%s",
148 n_patterns, n_patterns > 1 ? "s" : "",
149 n_channels, n_channels > 1 ? "s" : "");
151 return AVERROR(ENOMEM);
152 av_dict_set(&s->metadata, "extra info", extra, AV_DICT_DONT_STRDUP_VAL);
154 ADD_META_MULTIPLE_ENTRIES(instrument, ModPlug_InstrumentName);
155 ADD_META_MULTIPLE_ENTRIES(sample, ModPlug_SampleName);
160 #define AUDIO_PKT_SIZE 512
162 static int modplug_read_header(AVFormatContext *s, AVFormatParameters *ap)
165 AVIOContext *pb = s->pb;
166 ModPlug_Settings settings;
167 ModPlugContext *modplug = s->priv_data;
168 int sz = avio_size(pb);
171 av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
172 sz = modplug->max_size;
173 } else if (modplug->max_size && sz > modplug->max_size) {
174 sz = modplug->max_size;
175 av_log(s, AV_LOG_WARNING, "Max file size reach%s, allocating %dB "
176 "but demuxing is likely to fail due to incomplete buffer\n",
177 sz == FF_MODPLUG_DEF_FILE_SIZE ? " (see -max_size)" : "", sz);
180 if (modplug->color_eval) {
181 int r = av_expr_parse(&modplug->expr, modplug->color_eval, var_names,
182 NULL, NULL, NULL, NULL, 0, s);
187 modplug->buf = av_malloc(modplug->max_size);
189 return AVERROR(ENOMEM);
190 sz = avio_read(pb, modplug->buf, sz);
192 ModPlug_GetSettings(&settings);
193 settings.mChannels = 2;
195 settings.mFrequency = 44100;
196 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
197 settings.mLoopCount = 0; // prevents looping forever
199 if (modplug->noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
200 SET_OPT_IF_REQUESTED(mReverbDepth, reverb_depth, MODPLUG_ENABLE_REVERB);
201 SET_OPT_IF_REQUESTED(mReverbDelay, reverb_delay, MODPLUG_ENABLE_REVERB);
202 SET_OPT_IF_REQUESTED(mBassAmount, bass_amount, MODPLUG_ENABLE_MEGABASS);
203 SET_OPT_IF_REQUESTED(mBassRange, bass_range, MODPLUG_ENABLE_MEGABASS);
204 SET_OPT_IF_REQUESTED(mSurroundDepth, surround_depth, MODPLUG_ENABLE_SURROUND);
205 SET_OPT_IF_REQUESTED(mSurroundDelay, surround_delay, MODPLUG_ENABLE_SURROUND);
207 if (modplug->reverb_depth) settings.mReverbDepth = modplug->reverb_depth;
208 if (modplug->reverb_delay) settings.mReverbDelay = modplug->reverb_delay;
209 if (modplug->bass_amount) settings.mBassAmount = modplug->bass_amount;
210 if (modplug->bass_range) settings.mBassRange = modplug->bass_range;
211 if (modplug->surround_depth) settings.mSurroundDepth = modplug->surround_depth;
212 if (modplug->surround_delay) settings.mSurroundDelay = modplug->surround_delay;
214 ModPlug_SetSettings(&settings);
216 modplug->f = ModPlug_Load(modplug->buf, sz);
218 return AVERROR_INVALIDDATA;
220 st = av_new_stream(s, 0);
222 return AVERROR(ENOMEM);
223 av_set_pts_info(st, 64, 1, 1000);
224 st->duration = ModPlug_GetLength(modplug->f);
225 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
226 st->codec->codec_id = CODEC_ID_PCM_S16LE;
227 st->codec->channels = settings.mChannels;
228 st->codec->sample_rate = settings.mFrequency;
230 // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
231 modplug->ts_per_packet = 1000*AUDIO_PKT_SIZE / (4*44100.);
233 if (modplug->video_stream) {
234 AVStream *vst = av_new_stream(s, 1);
236 return AVERROR(ENOMEM);
237 av_set_pts_info(vst, 64, 1, 1000);
238 vst->duration = st->duration;
239 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
240 vst->codec->codec_id = CODEC_ID_XBIN;
241 vst->codec->width = modplug->w << 3;
242 vst->codec->height = modplug->h << 3;
243 modplug->linesize = modplug->w * 3;
244 modplug->fsize = modplug->linesize * modplug->h;
247 return modplug_load_metadata(s);
250 static void write_text(uint8_t *dst, const char *s, int linesize, int x, int y)
253 dst += y*linesize + x*3;
254 for (i = 0; s[i]; i++, dst += 3) {
255 dst[0] = 0x0; // count - 1
256 dst[1] = s[i]; // char
257 dst[2] = 0x0f; // background / foreground
261 #define PRINT_INFO(line, name, idvalue) do { \
262 snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
263 write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
264 write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
267 static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
269 ModPlugContext *modplug = s->priv_data;
271 if (modplug->video_stream) {
272 modplug->video_switch ^= 1; // one video packet for one audio packet
273 if (modplug->video_switch) {
274 double var_values[VAR_VARS_NB];
276 var_values[VAR_W ] = modplug->w;
277 var_values[VAR_H ] = modplug->h;
278 var_values[VAR_TIME ] = modplug->packet_count * modplug->ts_per_packet;
279 var_values[VAR_SPEED ] = ModPlug_GetCurrentSpeed (modplug->f);
280 var_values[VAR_TEMPO ] = ModPlug_GetCurrentTempo (modplug->f);
281 var_values[VAR_ORDER ] = ModPlug_GetCurrentOrder (modplug->f);
282 var_values[VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->f);
283 var_values[VAR_ROW ] = ModPlug_GetCurrentRow (modplug->f);
285 if (av_new_packet(pkt, modplug->fsize) < 0)
286 return AVERROR(ENOMEM);
287 pkt->stream_index = 1;
288 memset(pkt->data, 0, modplug->fsize);
290 if (modplug->print_textinfo) {
292 PRINT_INFO(0, "speed", VAR_SPEED);
293 PRINT_INFO(1, "tempo", VAR_TEMPO);
294 PRINT_INFO(2, "order", VAR_ORDER);
295 PRINT_INFO(3, "pattern", VAR_PATTERN);
296 PRINT_INFO(4, "row", VAR_ROW);
297 PRINT_INFO(5, "ts", VAR_TIME);
302 for (y = 0; y < modplug->h; y++) {
303 for (x = 0; x < modplug->w; x++) {
305 var_values[VAR_X] = x;
306 var_values[VAR_Y] = y;
307 color = av_expr_eval(modplug->expr, var_values, NULL);
308 pkt->data[y*modplug->linesize + x*3 + 2] |= av_clip((int)color, 0, 0xf)<<4;
312 pkt->pts = pkt->dts = var_values[VAR_TIME];
313 pkt->flags |= AV_PKT_FLAG_KEY;
318 if (av_new_packet(pkt, AUDIO_PKT_SIZE) < 0)
319 return AVERROR(ENOMEM);
321 if (modplug->video_stream)
322 pkt->pts = pkt->dts = modplug->packet_count++ * modplug->ts_per_packet;
324 pkt->size = ModPlug_Read(modplug->f, pkt->data, AUDIO_PKT_SIZE);
325 if (pkt->size <= 0) {
327 return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
332 static int modplug_read_close(AVFormatContext *s)
334 ModPlugContext *modplug = s->priv_data;
335 ModPlug_Unload(modplug->f);
336 av_freep(&modplug->buf);
340 static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
342 ModPlugContext *modplug = s->priv_data;
343 ModPlug_Seek(modplug->f, (int)ts);
344 if (modplug->video_stream)
345 modplug->packet_count = ts / modplug->ts_per_packet;
349 static const AVClass modplug_class = {
350 .class_name = "ModPlug demuxer",
351 .item_name = av_default_item_name,
353 .version = LIBAVUTIL_VERSION_INT,
356 AVInputFormat ff_libmodplug_demuxer = {
357 .name = "libmodplug",
358 .long_name = NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
359 .priv_data_size = sizeof(ModPlugContext),
360 .read_header = modplug_read_header,
361 .read_packet = modplug_read_packet,
362 .read_close = modplug_read_close,
363 .read_seek = modplug_read_seek,
364 .extensions = "669,abc,amf,ams,dbm,dmf,dsm,far,it,mdl,med,mid,mod,mt2,mtm,okt,psm,ptm,s3m,stm,ult,umx,xm"
365 ",itgz,itr,itz,mdgz,mdr,mdz,s3gz,s3r,s3z,xmgz,xmr,xmz", // compressed mods
366 .priv_class = &modplug_class,