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 #define MODPLUG_STATIC
26 #include <libmodplug/modplug.h>
27 #include "libavutil/avstring.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
35 typedef struct ModPlugContext
{
38 uint8_t *buf
; ///< input file content
49 int max_size
; ///< max file size to allocate
51 /* optional video stream */
52 double ts_per_packet
; ///< used to define the pts/dts using packet_count;
53 int packet_count
; ///< total number of audio packets
54 int print_textinfo
; ///< bool flag for printing speed, tempo, order, ...
55 int video_stream
; ///< 1 if the user want a video stream, otherwise 0
56 int w
; ///< video stream width in char (one char = 8x8px)
57 int h
; ///< video stream height in char (one char = 8x8px)
58 int video_switch
; ///< 1 if current packet is video, otherwise 0
59 int fsize
; ///< constant frame size
60 int linesize
; ///< line size in bytes
61 char *color_eval
; ///< color eval user input expression
62 AVExpr
*expr
; ///< parsed color eval expression
65 static const char * const var_names
[] = {
69 "speed", "tempo", "order", "pattern", "row",
77 VAR_SPEED
, VAR_TEMPO
, VAR_ORDER
, VAR_PATTERN
, VAR_ROW
,
81 #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
82 #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
84 #define OFFSET(x) offsetof(ModPlugContext, x)
85 #define D AV_OPT_FLAG_DECODING_PARAM
86 static const AVOption options
[] = {
87 {"noise_reduction", "Enable noise reduction 0(off)-1(on)", OFFSET(noise_reduction
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, D
},
88 {"reverb_depth", "Reverb level 0(quiet)-100(loud)", OFFSET(reverb_depth
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 100, D
},
89 {"reverb_delay", "Reverb delay in ms, usually 40-200ms", OFFSET(reverb_delay
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, INT_MAX
, D
},
90 {"bass_amount", "XBass level 0(quiet)-100(loud)", OFFSET(bass_amount
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 100, D
},
91 {"bass_range", "XBass cutoff in Hz 10-100", OFFSET(bass_range
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 100, D
},
92 {"surround_depth", "Surround level 0(quiet)-100(heavy)", OFFSET(surround_depth
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 100, D
},
93 {"surround_delay", "Surround delay in ms, usually 5-40ms", OFFSET(surround_delay
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, INT_MAX
, D
},
94 {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
95 OFFSET(max_size
), AV_OPT_TYPE_INT
, {.i64
= FF_MODPLUG_DEF_FILE_SIZE
}, 0, FF_MODPLUG_MAX_FILE_SIZE
, D
},
96 {"video_stream_expr", "Color formula", OFFSET(color_eval
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, D
},
97 {"video_stream", "Make demuxer output a video stream", OFFSET(video_stream
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, D
},
98 {"video_stream_w", "Video stream width in char (one char = 8x8px)", OFFSET(w
), AV_OPT_TYPE_INT
, {.i64
= 30}, 20, 512, D
},
99 {"video_stream_h", "Video stream height in char (one char = 8x8px)", OFFSET(h
), AV_OPT_TYPE_INT
, {.i64
= 30}, 20, 512, D
},
100 {"video_stream_ptxt", "Print speed, tempo, order, ... in video stream", OFFSET(print_textinfo
), AV_OPT_TYPE_INT
, {.i64
= 1}, 0, 1, D
},
104 static int modplug_read_close(AVFormatContext
*s
)
106 ModPlugContext
*modplug
= s
->priv_data
;
107 ModPlug_Unload(modplug
->f
);
108 av_freep(&modplug
->buf
);
112 #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
113 if (modplug->opt) { \
114 settings.libopt = modplug->opt; \
115 settings.mFlags |= flag; \
119 #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
120 if (n_## entry_name ##s) { \
123 for (i = 0; i < n_## entry_name ##s; i++) { \
124 char item_name[64] = {0}; \
125 fname(f, i, item_name); \
129 av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
130 av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
134 extra = av_asprintf(", %u/%u " #entry_name "%s", \
135 n, n_## entry_name ##s, n > 1 ? "s" : ""); \
137 return AVERROR(ENOMEM); \
138 av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
143 static int modplug_load_metadata(AVFormatContext
*s
)
145 ModPlugContext
*modplug
= s
->priv_data
;
146 ModPlugFile
*f
= modplug
->f
;
148 const char *name
= ModPlug_GetName(f
);
149 const char *msg
= ModPlug_GetMessage(f
);
151 unsigned n_instruments
= ModPlug_NumInstruments(f
);
152 unsigned n_samples
= ModPlug_NumSamples(f
);
153 unsigned n_patterns
= ModPlug_NumPatterns(f
);
154 unsigned n_channels
= ModPlug_NumChannels(f
);
156 if (name
&& *name
) av_dict_set(&s
->metadata
, "name", name
, 0);
157 if (msg
&& *msg
) av_dict_set(&s
->metadata
, "message", msg
, 0);
159 extra
= av_asprintf("%u pattern%s, %u channel%s",
160 n_patterns
, n_patterns
> 1 ? "s" : "",
161 n_channels
, n_channels
> 1 ? "s" : "");
163 return AVERROR(ENOMEM
);
164 av_dict_set(&s
->metadata
, "extra info", extra
, AV_DICT_DONT_STRDUP_VAL
);
166 ADD_META_MULTIPLE_ENTRIES(instrument
, ModPlug_InstrumentName
);
167 ADD_META_MULTIPLE_ENTRIES(sample
, ModPlug_SampleName
);
172 #define AUDIO_PKT_SIZE 512
174 static int modplug_read_header(AVFormatContext
*s
)
177 AVIOContext
*pb
= s
->pb
;
178 ModPlug_Settings settings
;
179 ModPlugContext
*modplug
= s
->priv_data
;
180 int64_t sz
= avio_size(pb
);
184 av_log(s
, AV_LOG_WARNING
, "Could not determine file size\n");
185 sz
= modplug
->max_size
;
186 } else if (modplug
->max_size
&& sz
> modplug
->max_size
) {
187 sz
= modplug
->max_size
;
188 av_log(s
, AV_LOG_WARNING
, "Max file size reach%s, allocating %"PRIi64
"B "
189 "but demuxing is likely to fail due to incomplete buffer\n",
190 sz
== FF_MODPLUG_DEF_FILE_SIZE
? " (see -max_size)" : "", sz
);
193 if (modplug
->color_eval
) {
194 int r
= av_expr_parse(&modplug
->expr
, modplug
->color_eval
, var_names
,
195 NULL
, NULL
, NULL
, NULL
, 0, s
);
200 modplug
->buf
= av_malloc(modplug
->max_size
);
202 return AVERROR(ENOMEM
);
203 sz
= avio_read(pb
, modplug
->buf
, sz
);
205 ModPlug_GetSettings(&settings
);
206 settings
.mChannels
= 2;
208 settings
.mFrequency
= 44100;
209 settings
.mResamplingMode
= MODPLUG_RESAMPLE_FIR
; // best quality
210 settings
.mLoopCount
= 0; // prevents looping forever
212 if (modplug
->noise_reduction
) settings
.mFlags
|= MODPLUG_ENABLE_NOISE_REDUCTION
;
213 SET_OPT_IF_REQUESTED(mReverbDepth
, reverb_depth
, MODPLUG_ENABLE_REVERB
);
214 SET_OPT_IF_REQUESTED(mReverbDelay
, reverb_delay
, MODPLUG_ENABLE_REVERB
);
215 SET_OPT_IF_REQUESTED(mBassAmount
, bass_amount
, MODPLUG_ENABLE_MEGABASS
);
216 SET_OPT_IF_REQUESTED(mBassRange
, bass_range
, MODPLUG_ENABLE_MEGABASS
);
217 SET_OPT_IF_REQUESTED(mSurroundDepth
, surround_depth
, MODPLUG_ENABLE_SURROUND
);
218 SET_OPT_IF_REQUESTED(mSurroundDelay
, surround_delay
, MODPLUG_ENABLE_SURROUND
);
220 if (modplug
->reverb_depth
) settings
.mReverbDepth
= modplug
->reverb_depth
;
221 if (modplug
->reverb_delay
) settings
.mReverbDelay
= modplug
->reverb_delay
;
222 if (modplug
->bass_amount
) settings
.mBassAmount
= modplug
->bass_amount
;
223 if (modplug
->bass_range
) settings
.mBassRange
= modplug
->bass_range
;
224 if (modplug
->surround_depth
) settings
.mSurroundDepth
= modplug
->surround_depth
;
225 if (modplug
->surround_delay
) settings
.mSurroundDelay
= modplug
->surround_delay
;
227 ModPlug_SetSettings(&settings
);
229 modplug
->f
= ModPlug_Load(modplug
->buf
, sz
);
231 av_freep(&modplug
->buf
);
232 return AVERROR_INVALIDDATA
;
234 st
= avformat_new_stream(s
, NULL
);
236 ret
= AVERROR(ENOMEM
);
239 avpriv_set_pts_info(st
, 64, 1, 1000);
240 st
->duration
= ModPlug_GetLength(modplug
->f
);
241 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
242 st
->codecpar
->codec_id
= AV_CODEC_ID_PCM_S16LE
;
243 st
->codecpar
->ch_layout
.nb_channels
= settings
.mChannels
;
244 st
->codecpar
->sample_rate
= settings
.mFrequency
;
246 // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
247 modplug
->ts_per_packet
= 1000*AUDIO_PKT_SIZE
/ (4*44100.);
249 if (modplug
->video_stream
) {
250 AVStream
*vst
= avformat_new_stream(s
, NULL
);
252 ret
= AVERROR(ENOMEM
);
255 avpriv_set_pts_info(vst
, 64, 1, 1000);
256 vst
->duration
= st
->duration
;
257 vst
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
258 vst
->codecpar
->codec_id
= AV_CODEC_ID_XBIN
;
259 vst
->codecpar
->width
= modplug
->w
<< 3;
260 vst
->codecpar
->height
= modplug
->h
<< 3;
261 modplug
->linesize
= modplug
->w
* 3;
262 modplug
->fsize
= modplug
->linesize
* modplug
->h
;
265 ret
= modplug_load_metadata(s
);
270 modplug_read_close(s
);
274 static void write_text(uint8_t *dst
, const char *s
, int linesize
, int x
, int y
)
277 dst
+= y
*linesize
+ x
*3;
278 for (i
= 0; s
[i
]; i
++, dst
+= 3) {
279 dst
[0] = 0x0; // count - 1
280 dst
[1] = s
[i
]; // char
281 dst
[2] = 0x0f; // background / foreground
285 #define PRINT_INFO(line, name, idvalue) do { \
286 snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
287 write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
288 write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
291 static int modplug_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
293 ModPlugContext
*modplug
= s
->priv_data
;
296 if (modplug
->video_stream
) {
297 modplug
->video_switch
^= 1; // one video packet for one audio packet
298 if (modplug
->video_switch
) {
299 double var_values
[VAR_VARS_NB
];
301 var_values
[VAR_W
] = modplug
->w
;
302 var_values
[VAR_H
] = modplug
->h
;
303 var_values
[VAR_TIME
] = modplug
->packet_count
* modplug
->ts_per_packet
;
304 var_values
[VAR_SPEED
] = ModPlug_GetCurrentSpeed (modplug
->f
);
305 var_values
[VAR_TEMPO
] = ModPlug_GetCurrentTempo (modplug
->f
);
306 var_values
[VAR_ORDER
] = ModPlug_GetCurrentOrder (modplug
->f
);
307 var_values
[VAR_PATTERN
] = ModPlug_GetCurrentPattern(modplug
->f
);
308 var_values
[VAR_ROW
] = ModPlug_GetCurrentRow (modplug
->f
);
310 if ((ret
= av_new_packet(pkt
, modplug
->fsize
)) < 0)
312 pkt
->stream_index
= 1;
313 memset(pkt
->data
, 0, modplug
->fsize
);
315 if (modplug
->print_textinfo
) {
317 PRINT_INFO(0, "speed", VAR_SPEED
);
318 PRINT_INFO(1, "tempo", VAR_TEMPO
);
319 PRINT_INFO(2, "order", VAR_ORDER
);
320 PRINT_INFO(3, "pattern", VAR_PATTERN
);
321 PRINT_INFO(4, "row", VAR_ROW
);
322 PRINT_INFO(5, "ts", VAR_TIME
);
327 for (y
= 0; y
< modplug
->h
; y
++) {
328 for (x
= 0; x
< modplug
->w
; x
++) {
330 var_values
[VAR_X
] = x
;
331 var_values
[VAR_Y
] = y
;
332 color
= av_expr_eval(modplug
->expr
, var_values
, NULL
);
333 pkt
->data
[y
*modplug
->linesize
+ x
*3 + 2] |= av_clip((int)color
, 0, 0xf)<<4;
337 pkt
->pts
= pkt
->dts
= var_values
[VAR_TIME
];
338 pkt
->flags
|= AV_PKT_FLAG_KEY
;
343 if ((ret
= av_new_packet(pkt
, AUDIO_PKT_SIZE
)) < 0)
346 if (modplug
->video_stream
)
347 pkt
->pts
= pkt
->dts
= modplug
->packet_count
++ * modplug
->ts_per_packet
;
349 pkt
->size
= ModPlug_Read(modplug
->f
, pkt
->data
, AUDIO_PKT_SIZE
);
350 if (pkt
->size
<= 0) {
351 return pkt
->size
== 0 ? AVERROR_EOF
: AVERROR(EIO
);
356 static int modplug_read_seek(AVFormatContext
*s
, int stream_idx
, int64_t ts
, int flags
)
358 ModPlugContext
*modplug
= s
->priv_data
;
359 ModPlug_Seek(modplug
->f
, (int)ts
);
360 if (modplug
->video_stream
)
361 modplug
->packet_count
= ts
/ modplug
->ts_per_packet
;
365 static const char modplug_extensions
[] = "669,abc,amf,ams,dbm,dmf,dsm,far,it,mdl,med,mid,mod,mt2,mtm,okt,psm,ptm,s3m,stm,ult,umx,xm,itgz,itr,itz,mdgz,mdr,mdz,s3gz,s3r,s3z,xmgz,xmr,xmz";
367 static int modplug_probe(const AVProbeData
*p
)
369 if (av_match_ext(p
->filename
, modplug_extensions
)) {
370 if (p
->buf_size
< 16384)
371 return AVPROBE_SCORE_EXTENSION
/2-1;
373 return AVPROBE_SCORE_EXTENSION
;
378 static const AVClass modplug_class
= {
379 .class_name
= "ModPlug demuxer",
380 .item_name
= av_default_item_name
,
382 .version
= LIBAVUTIL_VERSION_INT
,
385 const FFInputFormat ff_libmodplug_demuxer
= {
386 .p
.name
= "libmodplug",
387 .p
.long_name
= NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
388 .p
.extensions
= modplug_extensions
,
389 .p
.priv_class
= &modplug_class
,
390 .priv_data_size
= sizeof(ModPlugContext
),
391 .read_probe
= modplug_probe
,
392 .read_header
= modplug_read_header
,
393 .read_packet
= modplug_read_packet
,
394 .read_close
= modplug_read_close
,
395 .read_seek
= modplug_read_seek
,