You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
16KB

  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. * @file
  20. * ModPlug demuxer
  21. * @todo better probing than extensions matching
  22. */
  23. #define MODPLUG_STATIC
  24. #include <libmodplug/modplug.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/opt.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. typedef struct ModPlugContext {
  31. const AVClass *class;
  32. ModPlugFile *f;
  33. uint8_t *buf; ///< input file content
  34. /* options */
  35. int noise_reduction;
  36. int reverb_depth;
  37. int reverb_delay;
  38. int bass_amount;
  39. int bass_range;
  40. int surround_depth;
  41. int surround_delay;
  42. int max_size; ///< max file size to allocate
  43. /* optional video stream */
  44. double ts_per_packet; ///< used to define the pts/dts using packet_count;
  45. int packet_count; ///< total number of audio packets
  46. int print_textinfo; ///< bool flag for printing speed, tempo, order, ...
  47. int video_stream; ///< 1 if the user want a video stream, otherwise 0
  48. int w; ///< video stream width in char (one char = 8x8px)
  49. int h; ///< video stream height in char (one char = 8x8px)
  50. int video_switch; ///< 1 if current packet is video, otherwise 0
  51. int fsize; ///< constant frame size
  52. int linesize; ///< line size in bytes
  53. char *color_eval; ///< color eval user input expression
  54. AVExpr *expr; ///< parsed color eval expression
  55. } ModPlugContext;
  56. static const char *var_names[] = {
  57. "x", "y",
  58. "w", "h",
  59. "t",
  60. "speed", "tempo", "order", "pattern", "row",
  61. NULL
  62. };
  63. enum var_name {
  64. VAR_X, VAR_Y,
  65. VAR_W, VAR_H,
  66. VAR_TIME,
  67. VAR_SPEED, VAR_TEMPO, VAR_ORDER, VAR_PATTERN, VAR_ROW,
  68. VAR_VARS_NB
  69. };
  70. #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
  71. #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
  72. #define OFFSET(x) offsetof(ModPlugContext, x)
  73. #define D AV_OPT_FLAG_DECODING_PARAM
  74. static const AVOption options[] = {
  75. {"noise_reduction", "Enable noise reduction 0(off)-1(on)", OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D},
  76. {"reverb_depth", "Reverb level 0(quiet)-100(loud)", OFFSET(reverb_depth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, D},
  77. {"reverb_delay", "Reverb delay in ms, usually 40-200ms", OFFSET(reverb_delay), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D},
  78. {"bass_amount", "XBass level 0(quiet)-100(loud)", OFFSET(bass_amount), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, D},
  79. {"bass_range", "XBass cutoff in Hz 10-100", OFFSET(bass_range), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, D},
  80. {"surround_depth", "Surround level 0(quiet)-100(heavy)", OFFSET(surround_depth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, D},
  81. {"surround_delay", "Surround delay in ms, usually 5-40ms", OFFSET(surround_delay), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D},
  82. {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
  83. OFFSET(max_size), AV_OPT_TYPE_INT, {.i64 = FF_MODPLUG_DEF_FILE_SIZE}, 0, FF_MODPLUG_MAX_FILE_SIZE, D},
  84. {"video_stream_expr", "Color formula", OFFSET(color_eval), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D},
  85. {"video_stream", "Make demuxer output a video stream", OFFSET(video_stream), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D},
  86. {"video_stream_w", "Video stream width in char (one char = 8x8px)", OFFSET(w), AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512, D},
  87. {"video_stream_h", "Video stream height in char (one char = 8x8px)", OFFSET(h), AV_OPT_TYPE_INT, {.i64 = 30}, 20, 512, D},
  88. {"video_stream_ptxt", "Print speed, tempo, order, ... in video stream", OFFSET(print_textinfo), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, D},
  89. {NULL},
  90. };
  91. #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
  92. if (modplug->opt) { \
  93. settings.libopt = modplug->opt; \
  94. settings.mFlags |= flag; \
  95. } \
  96. } while (0)
  97. #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
  98. if (n_## entry_name ##s) { \
  99. unsigned i, n = 0; \
  100. \
  101. for (i = 0; i < n_## entry_name ##s; i++) { \
  102. char item_name[64] = {0}; \
  103. fname(f, i, item_name); \
  104. if (!*item_name) \
  105. continue; \
  106. if (n) \
  107. av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
  108. av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
  109. n++; \
  110. } \
  111. \
  112. extra = av_asprintf(", %u/%u " #entry_name "%s", \
  113. n, n_## entry_name ##s, n > 1 ? "s" : ""); \
  114. if (!extra) \
  115. return AVERROR(ENOMEM); \
  116. av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
  117. av_free(extra); \
  118. } \
  119. } while (0)
  120. static int modplug_load_metadata(AVFormatContext *s)
  121. {
  122. ModPlugContext *modplug = s->priv_data;
  123. ModPlugFile *f = modplug->f;
  124. char *extra;
  125. const char *name = ModPlug_GetName(f);
  126. const char *msg = ModPlug_GetMessage(f);
  127. unsigned n_instruments = ModPlug_NumInstruments(f);
  128. unsigned n_samples = ModPlug_NumSamples(f);
  129. unsigned n_patterns = ModPlug_NumPatterns(f);
  130. unsigned n_channels = ModPlug_NumChannels(f);
  131. if (name && *name) av_dict_set(&s->metadata, "name", name, 0);
  132. if (msg && *msg) av_dict_set(&s->metadata, "message", msg, 0);
  133. extra = av_asprintf("%u pattern%s, %u channel%s",
  134. n_patterns, n_patterns > 1 ? "s" : "",
  135. n_channels, n_channels > 1 ? "s" : "");
  136. if (!extra)
  137. return AVERROR(ENOMEM);
  138. av_dict_set(&s->metadata, "extra info", extra, AV_DICT_DONT_STRDUP_VAL);
  139. ADD_META_MULTIPLE_ENTRIES(instrument, ModPlug_InstrumentName);
  140. ADD_META_MULTIPLE_ENTRIES(sample, ModPlug_SampleName);
  141. return 0;
  142. }
  143. #define AUDIO_PKT_SIZE 512
  144. static int modplug_read_header(AVFormatContext *s)
  145. {
  146. AVStream *st;
  147. AVIOContext *pb = s->pb;
  148. ModPlug_Settings settings;
  149. ModPlugContext *modplug = s->priv_data;
  150. int64_t sz = avio_size(pb);
  151. if (sz < 0) {
  152. av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
  153. sz = modplug->max_size;
  154. } else if (modplug->max_size && sz > modplug->max_size) {
  155. sz = modplug->max_size;
  156. av_log(s, AV_LOG_WARNING, "Max file size reach%s, allocating %"PRIi64"B "
  157. "but demuxing is likely to fail due to incomplete buffer\n",
  158. sz == FF_MODPLUG_DEF_FILE_SIZE ? " (see -max_size)" : "", sz);
  159. }
  160. if (modplug->color_eval) {
  161. int r = av_expr_parse(&modplug->expr, modplug->color_eval, var_names,
  162. NULL, NULL, NULL, NULL, 0, s);
  163. if (r < 0)
  164. return r;
  165. }
  166. modplug->buf = av_malloc(modplug->max_size);
  167. if (!modplug->buf)
  168. return AVERROR(ENOMEM);
  169. sz = avio_read(pb, modplug->buf, sz);
  170. ModPlug_GetSettings(&settings);
  171. settings.mChannels = 2;
  172. settings.mBits = 16;
  173. settings.mFrequency = 44100;
  174. settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
  175. settings.mLoopCount = 0; // prevents looping forever
  176. if (modplug->noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
  177. SET_OPT_IF_REQUESTED(mReverbDepth, reverb_depth, MODPLUG_ENABLE_REVERB);
  178. SET_OPT_IF_REQUESTED(mReverbDelay, reverb_delay, MODPLUG_ENABLE_REVERB);
  179. SET_OPT_IF_REQUESTED(mBassAmount, bass_amount, MODPLUG_ENABLE_MEGABASS);
  180. SET_OPT_IF_REQUESTED(mBassRange, bass_range, MODPLUG_ENABLE_MEGABASS);
  181. SET_OPT_IF_REQUESTED(mSurroundDepth, surround_depth, MODPLUG_ENABLE_SURROUND);
  182. SET_OPT_IF_REQUESTED(mSurroundDelay, surround_delay, MODPLUG_ENABLE_SURROUND);
  183. if (modplug->reverb_depth) settings.mReverbDepth = modplug->reverb_depth;
  184. if (modplug->reverb_delay) settings.mReverbDelay = modplug->reverb_delay;
  185. if (modplug->bass_amount) settings.mBassAmount = modplug->bass_amount;
  186. if (modplug->bass_range) settings.mBassRange = modplug->bass_range;
  187. if (modplug->surround_depth) settings.mSurroundDepth = modplug->surround_depth;
  188. if (modplug->surround_delay) settings.mSurroundDelay = modplug->surround_delay;
  189. ModPlug_SetSettings(&settings);
  190. modplug->f = ModPlug_Load(modplug->buf, sz);
  191. if (!modplug->f)
  192. return AVERROR_INVALIDDATA;
  193. st = avformat_new_stream(s, NULL);
  194. if (!st)
  195. return AVERROR(ENOMEM);
  196. avpriv_set_pts_info(st, 64, 1, 1000);
  197. st->duration = ModPlug_GetLength(modplug->f);
  198. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  199. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  200. st->codec->channels = settings.mChannels;
  201. st->codec->sample_rate = settings.mFrequency;
  202. // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
  203. modplug->ts_per_packet = 1000*AUDIO_PKT_SIZE / (4*44100.);
  204. if (modplug->video_stream) {
  205. AVStream *vst = avformat_new_stream(s, NULL);
  206. if (!vst)
  207. return AVERROR(ENOMEM);
  208. avpriv_set_pts_info(vst, 64, 1, 1000);
  209. vst->duration = st->duration;
  210. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  211. vst->codec->codec_id = AV_CODEC_ID_XBIN;
  212. vst->codec->width = modplug->w << 3;
  213. vst->codec->height = modplug->h << 3;
  214. modplug->linesize = modplug->w * 3;
  215. modplug->fsize = modplug->linesize * modplug->h;
  216. }
  217. return modplug_load_metadata(s);
  218. }
  219. static void write_text(uint8_t *dst, const char *s, int linesize, int x, int y)
  220. {
  221. int i;
  222. dst += y*linesize + x*3;
  223. for (i = 0; s[i]; i++, dst += 3) {
  224. dst[0] = 0x0; // count - 1
  225. dst[1] = s[i]; // char
  226. dst[2] = 0x0f; // background / foreground
  227. }
  228. }
  229. #define PRINT_INFO(line, name, idvalue) do { \
  230. snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
  231. write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
  232. write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
  233. } while (0)
  234. static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
  235. {
  236. ModPlugContext *modplug = s->priv_data;
  237. if (modplug->video_stream) {
  238. modplug->video_switch ^= 1; // one video packet for one audio packet
  239. if (modplug->video_switch) {
  240. double var_values[VAR_VARS_NB];
  241. var_values[VAR_W ] = modplug->w;
  242. var_values[VAR_H ] = modplug->h;
  243. var_values[VAR_TIME ] = modplug->packet_count * modplug->ts_per_packet;
  244. var_values[VAR_SPEED ] = ModPlug_GetCurrentSpeed (modplug->f);
  245. var_values[VAR_TEMPO ] = ModPlug_GetCurrentTempo (modplug->f);
  246. var_values[VAR_ORDER ] = ModPlug_GetCurrentOrder (modplug->f);
  247. var_values[VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->f);
  248. var_values[VAR_ROW ] = ModPlug_GetCurrentRow (modplug->f);
  249. if (av_new_packet(pkt, modplug->fsize) < 0)
  250. return AVERROR(ENOMEM);
  251. pkt->stream_index = 1;
  252. memset(pkt->data, 0, modplug->fsize);
  253. if (modplug->print_textinfo) {
  254. char intbuf[32];
  255. PRINT_INFO(0, "speed", VAR_SPEED);
  256. PRINT_INFO(1, "tempo", VAR_TEMPO);
  257. PRINT_INFO(2, "order", VAR_ORDER);
  258. PRINT_INFO(3, "pattern", VAR_PATTERN);
  259. PRINT_INFO(4, "row", VAR_ROW);
  260. PRINT_INFO(5, "ts", VAR_TIME);
  261. }
  262. if (modplug->expr) {
  263. int x, y;
  264. for (y = 0; y < modplug->h; y++) {
  265. for (x = 0; x < modplug->w; x++) {
  266. double color;
  267. var_values[VAR_X] = x;
  268. var_values[VAR_Y] = y;
  269. color = av_expr_eval(modplug->expr, var_values, NULL);
  270. pkt->data[y*modplug->linesize + x*3 + 2] |= av_clip((int)color, 0, 0xf)<<4;
  271. }
  272. }
  273. }
  274. pkt->pts = pkt->dts = var_values[VAR_TIME];
  275. pkt->flags |= AV_PKT_FLAG_KEY;
  276. return 0;
  277. }
  278. }
  279. if (av_new_packet(pkt, AUDIO_PKT_SIZE) < 0)
  280. return AVERROR(ENOMEM);
  281. if (modplug->video_stream)
  282. pkt->pts = pkt->dts = modplug->packet_count++ * modplug->ts_per_packet;
  283. pkt->size = ModPlug_Read(modplug->f, pkt->data, AUDIO_PKT_SIZE);
  284. if (pkt->size <= 0) {
  285. av_free_packet(pkt);
  286. return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
  287. }
  288. return 0;
  289. }
  290. static int modplug_read_close(AVFormatContext *s)
  291. {
  292. ModPlugContext *modplug = s->priv_data;
  293. ModPlug_Unload(modplug->f);
  294. av_freep(&modplug->buf);
  295. return 0;
  296. }
  297. static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  298. {
  299. ModPlugContext *modplug = s->priv_data;
  300. ModPlug_Seek(modplug->f, (int)ts);
  301. if (modplug->video_stream)
  302. modplug->packet_count = ts / modplug->ts_per_packet;
  303. return 0;
  304. }
  305. 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";
  306. static int modplug_probe(AVProbeData *p)
  307. {
  308. if (av_match_ext(p->filename, modplug_extensions)) {
  309. if (p->buf_size < 16384)
  310. return AVPROBE_SCORE_EXTENSION/2-1;
  311. else
  312. return AVPROBE_SCORE_EXTENSION;
  313. }
  314. return 0;
  315. }
  316. static const AVClass modplug_class = {
  317. .class_name = "ModPlug demuxer",
  318. .item_name = av_default_item_name,
  319. .option = options,
  320. .version = LIBAVUTIL_VERSION_INT,
  321. };
  322. AVInputFormat ff_libmodplug_demuxer = {
  323. .name = "libmodplug",
  324. .long_name = NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
  325. .priv_data_size = sizeof(ModPlugContext),
  326. .read_probe = modplug_probe,
  327. .read_header = modplug_read_header,
  328. .read_packet = modplug_read_packet,
  329. .read_close = modplug_read_close,
  330. .read_seek = modplug_read_seek,
  331. .extensions = modplug_extensions,
  332. .priv_class = &modplug_class,
  333. };