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>
33
37 uint8_t *
buf;
///< input file content
38
39 /* options */
47
49
50 /* optional video stream */
51 double ts_per_packet;
///< used to define the pts/dts using packet_count;
54 int video_stream;
///< 1 if the user want a video stream, otherwise 0
55 int w;
///< video stream width in char (one char = 8x8px)
56 int h;
///< video stream height in char (one char = 8x8px)
58 int fsize;
///< constant frame size
63
65 "x", "y",
66 "w", "h",
67 "t",
68 "speed", "tempo", "order", "pattern", "row",
70 };
71
78 };
79
80 #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
81 #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
82
83 #define OFFSET(x) offsetof(ModPlugContext, x)
84 #define D AV_OPT_FLAG_DECODING_PARAM
86 {
"noise_reduction",
"Enable noise reduction 0(off)-1(on)",
OFFSET(noise_reduction),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D},
87 {
"reverb_depth",
"Reverb level 0(quiet)-100(loud)",
OFFSET(reverb_depth),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
88 {
"reverb_delay",
"Reverb delay in ms, usually 40-200ms",
OFFSET(reverb_delay),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D},
89 {
"bass_amount",
"XBass level 0(quiet)-100(loud)",
OFFSET(bass_amount),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
91 {
"surround_depth",
"Surround level 0(quiet)-100(heavy)",
OFFSET(surround_depth),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
92 {
"surround_delay",
"Surround delay in ms, usually 5-40ms",
OFFSET(surround_delay),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D},
93 {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
97 {
"video_stream_w",
"Video stream width in char (one char = 8x8px)",
OFFSET(
w),
AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512,
D},
98 {
"video_stream_h",
"Video stream height in char (one char = 8x8px)",
OFFSET(
h),
AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512,
D},
99 {
"video_stream_ptxt",
"Print speed, tempo, order, ... in video stream",
OFFSET(print_textinfo),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
D},
101 };
102
104 {
106 ModPlug_Unload(modplug->
f);
108 return 0;
109 }
110
111 #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
112 if (modplug->opt) { \
113 settings.libopt = modplug->opt; \
114 settings.mFlags |= flag; \
115 } \
116 } while (0)
117
118 #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
119 if (n_## entry_name ##s) { \
120 unsigned i, n = 0; \
121 \
122 for (i = 0; i < n_## entry_name ##s; i++) { \
123 char item_name[64] = {0}; \
124 fname(f, i, item_name); \
125 if (!*item_name) \
126 continue; \
127 if (n) \
128 av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
129 av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
130 n++; \
131 } \
132 \
133 extra = av_asprintf(", %u/%u " #entry_name "%s", \
134 n, n_## entry_name ##s, n > 1 ? "s" : ""); \
135 if (!extra) \
136 return AVERROR(ENOMEM); \
137 av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
138 av_free(extra); \
139 } \
140 } while (0)
141
143 {
145 ModPlugFile *
f = modplug->
f;
146 char *extra;
147 const char *
name = ModPlug_GetName(
f);
148 const char *msg = ModPlug_GetMessage(
f);
149
150 unsigned n_instruments = ModPlug_NumInstruments(
f);
151 unsigned n_samples = ModPlug_NumSamples(
f);
152 unsigned n_patterns = ModPlug_NumPatterns(
f);
153 unsigned n_channels = ModPlug_NumChannels(
f);
154
156 if (msg && *msg)
av_dict_set(&
s->metadata,
"message", msg, 0);
157
159 n_patterns, n_patterns > 1 ? "s" : "",
160 n_channels, n_channels > 1 ? "s" : "");
161 if (!extra)
164
167
168 return 0;
169 }
170
171 #define AUDIO_PKT_SIZE 512
172
174 {
177 ModPlug_Settings settings;
181
182 if (sz < 0) {
188 "but demuxing is likely to fail due to incomplete buffer\n",
190 }
191
197 }
198
203
204 ModPlug_GetSettings(&settings);
205 settings.mChannels = 2;
206 settings.mBits = 16;
207 settings.mFrequency = 44100;
208 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
209 settings.mLoopCount = 0; // prevents looping forever
210
211 if (modplug->
noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
218
225
226 ModPlug_SetSettings(&settings);
227
228 modplug->
f = ModPlug_Load(modplug->
buf, sz);
232 }
234 if (!st) {
237 }
239 st->
duration = ModPlug_GetLength(modplug->
f);
244
245 // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
247
250 if (!vst) {
253 }
262 }
263
267 return 0;
271 }
272
273 static void write_text(uint8_t *dst,
const char *
s,
int linesize,
int x,
int y)
274 {
276 dst += y*linesize + x*3;
277 for (
i = 0;
s[
i];
i++, dst += 3) {
278 dst[0] = 0x0; // count - 1
279 dst[1] =
s[
i];
// char
280 dst[2] = 0x0f; // background / foreground
281 }
282 }
283
284 #define PRINT_INFO(line, name, idvalue) do { \
285 snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
286 write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
287 write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
288 } while (0)
289
291 {
294
296 modplug->
video_switch ^= 1;
// one video packet for one audio packet
299
300 var_values[
VAR_W ] = modplug->
w;
301 var_values[
VAR_H ] = modplug->
h;
303 var_values[
VAR_SPEED ] = ModPlug_GetCurrentSpeed (modplug->
f);
304 var_values[
VAR_TEMPO ] = ModPlug_GetCurrentTempo (modplug->
f);
305 var_values[
VAR_ORDER ] = ModPlug_GetCurrentOrder (modplug->
f);
306 var_values[
VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->
f);
307 var_values[
VAR_ROW ] = ModPlug_GetCurrentRow (modplug->
f);
308
313
315 char intbuf[32];
322 }
323
325 int x, y;
326 for (y = 0; y < modplug->
h; y++) {
327 for (x = 0; x < modplug->
w; x++) {
329 var_values[
VAR_X] = x;
330 var_values[
VAR_Y] = y;
333 }
334 }
335 }
338 return 0;
339 }
340 }
341
344
347
351 }
352 return 0;
353 }
354
356 {
358 ModPlug_Seek(modplug->
f, (
int)ts);
361 return 0;
362 }
363
364 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";
365
367 {
371 else
373 }
374 return 0;
375 }
376
382 };
383
385 .
p.
name =
"libmodplug",
395 };