1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 /**
20 * @file
21 * ModPlug demuxer
22 * @todo better probing than extensions matching
23 */
24
25 #define MODPLUG_STATIC
26 #include <libmodplug/modplug.h>
32
37
38 /* options */
46
48
49 /* optional video stream */
50 double ts_per_packet;
///< used to define the pts/dts using packet_count;
53 int video_stream;
///< 1 if the user want a video stream, otherwise 0
54 int w;
///< video stream width in char (one char = 8x8px)
55 int h;
///< video stream height in char (one char = 8x8px)
57 int fsize;
///< constant frame size
62
64 "x", "y",
65 "w", "h",
66 "t",
67 "speed", "tempo", "order", "pattern", "row",
69 };
70
77 };
78
79 #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
80 #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
81
82 #define OFFSET(x) offsetof(ModPlugContext, x)
83 #define D AV_OPT_FLAG_DECODING_PARAM
85 {
"noise_reduction",
"Enable noise reduction 0(off)-1(on)",
OFFSET(noise_reduction),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D},
86 {
"reverb_depth",
"Reverb level 0(quiet)-100(loud)",
OFFSET(reverb_depth),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
87 {
"reverb_delay",
"Reverb delay in ms, usually 40-200ms",
OFFSET(reverb_delay),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D},
88 {
"bass_amount",
"XBass level 0(quiet)-100(loud)",
OFFSET(bass_amount),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
90 {
"surround_depth",
"Surround level 0(quiet)-100(heavy)",
OFFSET(surround_depth),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
91 {
"surround_delay",
"Surround delay in ms, usually 5-40ms",
OFFSET(surround_delay),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D},
92 {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
96 {
"video_stream_w",
"Video stream width in char (one char = 8x8px)",
OFFSET(w),
AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512,
D},
97 {
"video_stream_h",
"Video stream height in char (one char = 8x8px)",
OFFSET(
h),
AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512,
D},
98 {
"video_stream_ptxt",
"Print speed, tempo, order, ... in video stream",
OFFSET(print_textinfo),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
D},
100 };
101
102 #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
103 if (modplug->opt) { \
104 settings.libopt = modplug->opt; \
105 settings.mFlags |= flag; \
106 } \
107 } while (0)
108
109 #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
110 if (n_## entry_name ##s) { \
111 unsigned i, n = 0; \
112 \
113 for (i = 0; i < n_## entry_name ##s; i++) { \
114 char item_name[64] = {0}; \
115 fname(f, i, item_name); \
116 if (!*item_name) \
117 continue; \
118 if (n) \
119 av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
120 av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
121 n++; \
122 } \
123 \
124 extra = av_asprintf(", %u/%u " #entry_name "%s", \
125 n, n_## entry_name ##s, n > 1 ? "s" : ""); \
126 if (!extra) \
127 return AVERROR(ENOMEM); \
128 av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
129 av_free(extra); \
130 } \
131 } while (0)
132
134 {
136 ModPlugFile *f = modplug->
f;
137 char *extra;
138 const char *
name = ModPlug_GetName(f);
139 const char *msg = ModPlug_GetMessage(f);
140
141 unsigned n_instruments = ModPlug_NumInstruments(f);
142 unsigned n_samples = ModPlug_NumSamples(f);
143 unsigned n_patterns = ModPlug_NumPatterns(f);
144 unsigned n_channels = ModPlug_NumChannels(f);
145
148
150 n_patterns, n_patterns > 1 ? "s" : "",
151 n_channels, n_channels > 1 ? "s" : "");
152 if (!extra)
155
158
159 return 0;
160 }
161
162 #define AUDIO_PKT_SIZE 512
163
165 {
168 ModPlug_Settings settings;
171
172 if (sz < 0) {
178 "but demuxing is likely to fail due to incomplete buffer\n",
180 }
181
185 if (r < 0)
187 }
188
193
194 ModPlug_GetSettings(&settings);
195 settings.mChannels = 2;
196 settings.mBits = 16;
197 settings.mFrequency = 44100;
198 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
199 settings.mLoopCount = 0; // prevents looping forever
200
201 if (modplug->
noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
208
215
216 ModPlug_SetSettings(&settings);
217
218 modplug->
f = ModPlug_Load(modplug->
buf, sz);
221
223 if (!st)
226 st->
duration = ModPlug_GetLength(modplug->
f);
231
232 // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
234
237 if (!vst)
247 }
248
250 }
251
253 {
254 int i;
255 dst += y*linesize + x*3;
256 for (i = 0; s[i]; i++, dst += 3) {
257 dst[0] = 0x0; // count - 1
258 dst[1] = s[i]; // char
259 dst[2] = 0x0f; // background / foreground
260 }
261 }
262
263 #define PRINT_INFO(line, name, idvalue) do { \
264 snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
265 write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
266 write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
267 } while (0)
268
270 {
272
274 modplug->
video_switch ^= 1;
// one video packet for one audio packet
277
278 var_values[
VAR_W ] = modplug->
w;
279 var_values[
VAR_H ] = modplug->
h;
281 var_values[
VAR_SPEED ] = ModPlug_GetCurrentSpeed (modplug->
f);
282 var_values[
VAR_TEMPO ] = ModPlug_GetCurrentTempo (modplug->
f);
283 var_values[
VAR_ORDER ] = ModPlug_GetCurrentOrder (modplug->
f);
284 var_values[
VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->
f);
285 var_values[
VAR_ROW ] = ModPlug_GetCurrentRow (modplug->
f);
286
291
293 char intbuf[32];
300 }
301
303 int x, y;
304 for (y = 0; y < modplug->
h; y++) {
305 for (x = 0; x < modplug->
w; x++) {
307 var_values[
VAR_X] = x;
308 var_values[
VAR_Y] = y;
310 pkt->
data[y*modplug->
linesize + x*3 + 2] |= av_clip((
int)color, 0, 0xf)<<4;
311 }
312 }
313 }
316 return 0;
317 }
318 }
319
322
325
327 if (pkt->
size <= 0) {
330 }
331 return 0;
332 }
333
335 {
337 ModPlug_Unload(modplug->
f);
339 return 0;
340 }
341
343 {
345 ModPlug_Seek(modplug->
f, (
int)ts);
348 return 0;
349 }
350
351 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";
352
354 {
358 else
360 }
361 return 0;
362 }
363
369 };
370
372 .
name =
"libmodplug",
381 .priv_class = &modplug_class,
382 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define FF_MODPLUG_DEF_FILE_SIZE
int64_t avio_size(AVIOContext *s)
Get the filesize.
int h
video stream height in char (one char = 8x8px)
int linesize
line size in bytes
int fsize
constant frame size
static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
static AVStream * video_stream
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
AVInputFormat ff_libmodplug_demuxer
static const char *const var_names[]
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
int packet_count
total number of audio packets
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
static av_cold int read_close(AVFormatContext *ctx)
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
static const AVOption options[]
int video_stream
1 if the user want a video stream, otherwise 0
#define SET_OPT_IF_REQUESTED(libopt, opt, flag)
static int modplug_load_metadata(AVFormatContext *s)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
int print_textinfo
bool flag for printing speed, tempo, order, ...
AVDictionary * metadata
Metadata that applies to the whole file.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
int buf_size
Size of buf except extra allocated bytes.
char * av_asprintf(const char *fmt,...)
static const AVClass modplug_class
uint8_t * buf
input file content
#define FF_MODPLUG_MAX_FILE_SIZE
AVExpr * expr
parsed color eval expression
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
int width
picture width / height.
char * color_eval
color eval user input expression
static int modplug_read_header(AVFormatContext *s)
static const char modplug_extensions[]
static int read_header(FFV1Context *f)
int video_switch
1 if current packet is video, otherwise 0
static int modplug_probe(AVProbeData *p)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
enum AVMediaType codec_type
static void write_text(uint8_t *dst, const char *s, int linesize, int x, int y)
int sample_rate
samples per second
AVIOContext * pb
I/O context.
int max_size
max file size to allocate
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
This structure contains the data a format has to probe a file.
int w
video stream width in char (one char = 8x8px)
#define PRINT_INFO(line, name, idvalue)
int64_t duration
Decoding: duration of the stream, in stream time base.
double ts_per_packet
used to define the pts/dts using packet_count;
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
int channels
number of audio channels
void * priv_data
Format private data.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static int modplug_read_close(AVFormatContext *s)
#define ADD_META_MULTIPLE_ENTRIES(entry_name, fname)
This structure stores compressed data.
static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
simple arithmetic expression evaluator