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 #include <libmodplug/modplug.h>
31
36
37 /* options */
45
47
48 /* optional video stream */
49 double ts_per_packet;
///< used to define the pts/dts using packet_count;
52 int video_stream;
///< 1 if the user want a video stream, otherwise 0
53 int w;
///< video stream width in char (one char = 8x8px)
54 int h;
///< video stream height in char (one char = 8x8px)
56 int fsize;
///< constant frame size
61
63 "x", "y",
64 "w", "h",
65 "t",
66 "speed", "tempo", "order", "pattern", "row",
67 NULL
68 };
69
76 };
77
78 #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
79 #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
80
81 #define OFFSET(x) offsetof(ModPlugContext, x)
82 #define D AV_OPT_FLAG_DECODING_PARAM
84 {
"noise_reduction",
"Enable noise reduction 0(off)-1(on)",
OFFSET(noise_reduction),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D},
85 {
"reverb_depth",
"Reverb level 0(quiet)-100(loud)",
OFFSET(reverb_depth),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
86 {
"reverb_delay",
"Reverb delay in ms, usually 40-200ms",
OFFSET(reverb_delay),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D},
87 {
"bass_amount",
"XBass level 0(quiet)-100(loud)",
OFFSET(bass_amount),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
89 {
"surround_depth",
"Surround level 0(quiet)-100(heavy)",
OFFSET(surround_depth),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100,
D},
90 {
"surround_delay",
"Surround delay in ms, usually 5-40ms",
OFFSET(surround_delay),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D},
91 {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
95 {
"video_stream_w",
"Video stream width in char (one char = 8x8px)",
OFFSET(w),
AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512,
D},
96 {
"video_stream_h",
"Video stream height in char (one char = 8x8px)",
OFFSET(h),
AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512,
D},
97 {
"video_stream_ptxt",
"Print speed, tempo, order, ... in video stream",
OFFSET(print_textinfo),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
D},
98 {NULL},
99 };
100
101 #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
102 if (modplug->opt) { \
103 settings.libopt = modplug->opt; \
104 settings.mFlags |= flag; \
105 } \
106 } while (0)
107
108 #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
109 if (n_## entry_name ##s) { \
110 unsigned i, n = 0; \
111 \
112 for (i = 0; i < n_## entry_name ##s; i++) { \
113 char item_name[64] = {0}; \
114 fname(f, i, item_name); \
115 if (!*item_name) \
116 continue; \
117 if (n) \
118 av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
119 av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
120 n++; \
121 } \
122 \
123 extra = av_asprintf(", %u/%u " #entry_name "%s", \
124 n, n_## entry_name ##s, n > 1 ? "s" : ""); \
125 if (!extra) \
126 return AVERROR(ENOMEM); \
127 av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
128 av_free(extra); \
129 } \
130 } while (0)
131
133 {
135 ModPlugFile *f = modplug->
f;
136 char *extra;
137 const char *
name = ModPlug_GetName(f);
138 const char *msg = ModPlug_GetMessage(f);
139
140 unsigned n_instruments = ModPlug_NumInstruments(f);
141 unsigned n_samples = ModPlug_NumSamples(f);
142 unsigned n_patterns = ModPlug_NumPatterns(f);
143 unsigned n_channels = ModPlug_NumChannels(f);
144
147
149 n_patterns, n_patterns > 1 ? "s" : "",
150 n_channels, n_channels > 1 ? "s" : "");
151 if (!extra)
154
157
158 return 0;
159 }
160
161 #define AUDIO_PKT_SIZE 512
162
164 {
167 ModPlug_Settings settings;
170
171 if (sz < 0) {
177 "but demuxing is likely to fail due to incomplete buffer\n",
179 }
180
183 NULL, NULL, NULL, NULL, 0, s);
184 if (r < 0)
186 }
187
192
193 ModPlug_GetSettings(&settings);
194 settings.mChannels = 2;
195 settings.mBits = 16;
196 settings.mFrequency = 44100;
197 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
198 settings.mLoopCount = 0; // prevents looping forever
199
200 if (modplug->
noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
207
214
215 ModPlug_SetSettings(&settings);
216
217 modplug->
f = ModPlug_Load(modplug->
buf, sz);
220
222 if (!st)
225 st->
duration = ModPlug_GetLength(modplug->
f);
230
231 // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
233
236 if (!vst)
246 }
247
249 }
250
252 {
253 int i;
254 dst += y*linesize + x*3;
255 for (i = 0; s[i]; i++, dst += 3) {
256 dst[0] = 0x0; // count - 1
257 dst[1] = s[i]; // char
258 dst[2] = 0x0f; // background / foreground
259 }
260 }
261
262 #define PRINT_INFO(line, name, idvalue) do { \
263 snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
264 write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
265 write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
266 } while (0)
267
269 {
271
273 modplug->
video_switch ^= 1;
// one video packet for one audio packet
276
277 var_values[
VAR_W ] = modplug->
w;
278 var_values[
VAR_H ] = modplug->
h;
280 var_values[
VAR_SPEED ] = ModPlug_GetCurrentSpeed (modplug->
f);
281 var_values[
VAR_TEMPO ] = ModPlug_GetCurrentTempo (modplug->
f);
282 var_values[
VAR_ORDER ] = ModPlug_GetCurrentOrder (modplug->
f);
283 var_values[
VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->
f);
284 var_values[
VAR_ROW ] = ModPlug_GetCurrentRow (modplug->
f);
285
290
292 char intbuf[32];
299 }
300
303 for (y = 0; y < modplug->
h; y++) {
304 for (x = 0; x < modplug->
w; x++) {
306 var_values[
VAR_X] = x;
309 pkt->
data[y*modplug->
linesize + x*3 + 2] |= av_clip((
int)color, 0, 0xf)<<4;
310 }
311 }
312 }
315 return 0;
316 }
317 }
318
321
324
326 if (pkt->
size <= 0) {
329 }
330 return 0;
331 }
332
334 {
336 ModPlug_Unload(modplug->
f);
338 return 0;
339 }
340
342 {
344 ModPlug_Seek(modplug->
f, (
int)ts);
347 return 0;
348 }
349
350 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";
351
353 {
357 else
359 }
360 return 0;
361 }
362
368 };
369
371 .
name =
"libmodplug",
380 .priv_class = &modplug_class,
381 };