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.

395 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 * const 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. static int modplug_read_close(AVFormatContext *s)
  92. {
  93. ModPlugContext *modplug = s->priv_data;
  94. ModPlug_Unload(modplug->f);
  95. av_freep(&modplug->buf);
  96. return 0;
  97. }
  98. #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
  99. if (modplug->opt) { \
  100. settings.libopt = modplug->opt; \
  101. settings.mFlags |= flag; \
  102. } \
  103. } while (0)
  104. #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
  105. if (n_## entry_name ##s) { \
  106. unsigned i, n = 0; \
  107. \
  108. for (i = 0; i < n_## entry_name ##s; i++) { \
  109. char item_name[64] = {0}; \
  110. fname(f, i, item_name); \
  111. if (!*item_name) \
  112. continue; \
  113. if (n) \
  114. av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
  115. av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
  116. n++; \
  117. } \
  118. \
  119. extra = av_asprintf(", %u/%u " #entry_name "%s", \
  120. n, n_## entry_name ##s, n > 1 ? "s" : ""); \
  121. if (!extra) \
  122. return AVERROR(ENOMEM); \
  123. av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
  124. av_free(extra); \
  125. } \
  126. } while (0)
  127. static int modplug_load_metadata(AVFormatContext *s)
  128. {
  129. ModPlugContext *modplug = s->priv_data;
  130. ModPlugFile *f = modplug->f;
  131. char *extra;
  132. const char *name = ModPlug_GetName(f);
  133. const char *msg = ModPlug_GetMessage(f);
  134. unsigned n_instruments = ModPlug_NumInstruments(f);
  135. unsigned n_samples = ModPlug_NumSamples(f);
  136. unsigned n_patterns = ModPlug_NumPatterns(f);
  137. unsigned n_channels = ModPlug_NumChannels(f);
  138. if (name && *name) av_dict_set(&s->metadata, "name", name, 0);
  139. if (msg && *msg) av_dict_set(&s->metadata, "message", msg, 0);
  140. extra = av_asprintf("%u pattern%s, %u channel%s",
  141. n_patterns, n_patterns > 1 ? "s" : "",
  142. n_channels, n_channels > 1 ? "s" : "");
  143. if (!extra)
  144. return AVERROR(ENOMEM);
  145. av_dict_set(&s->metadata, "extra info", extra, AV_DICT_DONT_STRDUP_VAL);
  146. ADD_META_MULTIPLE_ENTRIES(instrument, ModPlug_InstrumentName);
  147. ADD_META_MULTIPLE_ENTRIES(sample, ModPlug_SampleName);
  148. return 0;
  149. }
  150. #define AUDIO_PKT_SIZE 512
  151. static int modplug_read_header(AVFormatContext *s)
  152. {
  153. AVStream *st;
  154. AVIOContext *pb = s->pb;
  155. ModPlug_Settings settings;
  156. ModPlugContext *modplug = s->priv_data;
  157. int64_t sz = avio_size(pb);
  158. int ret;
  159. if (sz < 0) {
  160. av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
  161. sz = modplug->max_size;
  162. } else if (modplug->max_size && sz > modplug->max_size) {
  163. sz = modplug->max_size;
  164. av_log(s, AV_LOG_WARNING, "Max file size reach%s, allocating %"PRIi64"B "
  165. "but demuxing is likely to fail due to incomplete buffer\n",
  166. sz == FF_MODPLUG_DEF_FILE_SIZE ? " (see -max_size)" : "", sz);
  167. }
  168. if (modplug->color_eval) {
  169. int r = av_expr_parse(&modplug->expr, modplug->color_eval, var_names,
  170. NULL, NULL, NULL, NULL, 0, s);
  171. if (r < 0)
  172. return r;
  173. }
  174. modplug->buf = av_malloc(modplug->max_size);
  175. if (!modplug->buf)
  176. return AVERROR(ENOMEM);
  177. sz = avio_read(pb, modplug->buf, sz);
  178. ModPlug_GetSettings(&settings);
  179. settings.mChannels = 2;
  180. settings.mBits = 16;
  181. settings.mFrequency = 44100;
  182. settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
  183. settings.mLoopCount = 0; // prevents looping forever
  184. if (modplug->noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
  185. SET_OPT_IF_REQUESTED(mReverbDepth, reverb_depth, MODPLUG_ENABLE_REVERB);
  186. SET_OPT_IF_REQUESTED(mReverbDelay, reverb_delay, MODPLUG_ENABLE_REVERB);
  187. SET_OPT_IF_REQUESTED(mBassAmount, bass_amount, MODPLUG_ENABLE_MEGABASS);
  188. SET_OPT_IF_REQUESTED(mBassRange, bass_range, MODPLUG_ENABLE_MEGABASS);
  189. SET_OPT_IF_REQUESTED(mSurroundDepth, surround_depth, MODPLUG_ENABLE_SURROUND);
  190. SET_OPT_IF_REQUESTED(mSurroundDelay, surround_delay, MODPLUG_ENABLE_SURROUND);
  191. if (modplug->reverb_depth) settings.mReverbDepth = modplug->reverb_depth;
  192. if (modplug->reverb_delay) settings.mReverbDelay = modplug->reverb_delay;
  193. if (modplug->bass_amount) settings.mBassAmount = modplug->bass_amount;
  194. if (modplug->bass_range) settings.mBassRange = modplug->bass_range;
  195. if (modplug->surround_depth) settings.mSurroundDepth = modplug->surround_depth;
  196. if (modplug->surround_delay) settings.mSurroundDelay = modplug->surround_delay;
  197. ModPlug_SetSettings(&settings);
  198. modplug->f = ModPlug_Load(modplug->buf, sz);
  199. if (!modplug->f) {
  200. av_freep(&modplug->buf);
  201. return AVERROR_INVALIDDATA;
  202. }
  203. st = avformat_new_stream(s, NULL);
  204. if (!st) {
  205. ret = AVERROR(ENOMEM);
  206. goto fail;
  207. }
  208. avpriv_set_pts_info(st, 64, 1, 1000);
  209. st->duration = ModPlug_GetLength(modplug->f);
  210. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  211. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  212. st->codecpar->channels = settings.mChannels;
  213. st->codecpar->sample_rate = settings.mFrequency;
  214. // timebase = 1/1000, 2ch 16bits 44.1kHz-> 2*2*44100
  215. modplug->ts_per_packet = 1000*AUDIO_PKT_SIZE / (4*44100.);
  216. if (modplug->video_stream) {
  217. AVStream *vst = avformat_new_stream(s, NULL);
  218. if (!vst) {
  219. ret = AVERROR(ENOMEM);
  220. goto fail;
  221. }
  222. avpriv_set_pts_info(vst, 64, 1, 1000);
  223. vst->duration = st->duration;
  224. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  225. vst->codecpar->codec_id = AV_CODEC_ID_XBIN;
  226. vst->codecpar->width = modplug->w << 3;
  227. vst->codecpar->height = modplug->h << 3;
  228. modplug->linesize = modplug->w * 3;
  229. modplug->fsize = modplug->linesize * modplug->h;
  230. }
  231. ret = modplug_load_metadata(s);
  232. if (ret < 0)
  233. goto fail;
  234. return 0;
  235. fail:
  236. modplug_read_close(s);
  237. return ret;
  238. }
  239. static void write_text(uint8_t *dst, const char *s, int linesize, int x, int y)
  240. {
  241. int i;
  242. dst += y*linesize + x*3;
  243. for (i = 0; s[i]; i++, dst += 3) {
  244. dst[0] = 0x0; // count - 1
  245. dst[1] = s[i]; // char
  246. dst[2] = 0x0f; // background / foreground
  247. }
  248. }
  249. #define PRINT_INFO(line, name, idvalue) do { \
  250. snprintf(intbuf, sizeof(intbuf), "%.0f", var_values[idvalue]); \
  251. write_text(pkt->data, name ":", modplug->linesize, 0+1, line+1); \
  252. write_text(pkt->data, intbuf, modplug->linesize, 10+1, line+1); \
  253. } while (0)
  254. static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
  255. {
  256. ModPlugContext *modplug = s->priv_data;
  257. int ret;
  258. if (modplug->video_stream) {
  259. modplug->video_switch ^= 1; // one video packet for one audio packet
  260. if (modplug->video_switch) {
  261. double var_values[VAR_VARS_NB];
  262. var_values[VAR_W ] = modplug->w;
  263. var_values[VAR_H ] = modplug->h;
  264. var_values[VAR_TIME ] = modplug->packet_count * modplug->ts_per_packet;
  265. var_values[VAR_SPEED ] = ModPlug_GetCurrentSpeed (modplug->f);
  266. var_values[VAR_TEMPO ] = ModPlug_GetCurrentTempo (modplug->f);
  267. var_values[VAR_ORDER ] = ModPlug_GetCurrentOrder (modplug->f);
  268. var_values[VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->f);
  269. var_values[VAR_ROW ] = ModPlug_GetCurrentRow (modplug->f);
  270. if ((ret = av_new_packet(pkt, modplug->fsize)) < 0)
  271. return ret;
  272. pkt->stream_index = 1;
  273. memset(pkt->data, 0, modplug->fsize);
  274. if (modplug->print_textinfo) {
  275. char intbuf[32];
  276. PRINT_INFO(0, "speed", VAR_SPEED);
  277. PRINT_INFO(1, "tempo", VAR_TEMPO);
  278. PRINT_INFO(2, "order", VAR_ORDER);
  279. PRINT_INFO(3, "pattern", VAR_PATTERN);
  280. PRINT_INFO(4, "row", VAR_ROW);
  281. PRINT_INFO(5, "ts", VAR_TIME);
  282. }
  283. if (modplug->expr) {
  284. int x, y;
  285. for (y = 0; y < modplug->h; y++) {
  286. for (x = 0; x < modplug->w; x++) {
  287. double color;
  288. var_values[VAR_X] = x;
  289. var_values[VAR_Y] = y;
  290. color = av_expr_eval(modplug->expr, var_values, NULL);
  291. pkt->data[y*modplug->linesize + x*3 + 2] |= av_clip((int)color, 0, 0xf)<<4;
  292. }
  293. }
  294. }
  295. pkt->pts = pkt->dts = var_values[VAR_TIME];
  296. pkt->flags |= AV_PKT_FLAG_KEY;
  297. return 0;
  298. }
  299. }
  300. if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
  301. return ret;
  302. if (modplug->video_stream)
  303. pkt->pts = pkt->dts = modplug->packet_count++ * modplug->ts_per_packet;
  304. pkt->size = ModPlug_Read(modplug->f, pkt->data, AUDIO_PKT_SIZE);
  305. if (pkt->size <= 0) {
  306. return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
  307. }
  308. return 0;
  309. }
  310. static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  311. {
  312. ModPlugContext *modplug = s->priv_data;
  313. ModPlug_Seek(modplug->f, (int)ts);
  314. if (modplug->video_stream)
  315. modplug->packet_count = ts / modplug->ts_per_packet;
  316. return 0;
  317. }
  318. 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";
  319. static int modplug_probe(const AVProbeData *p)
  320. {
  321. if (av_match_ext(p->filename, modplug_extensions)) {
  322. if (p->buf_size < 16384)
  323. return AVPROBE_SCORE_EXTENSION/2-1;
  324. else
  325. return AVPROBE_SCORE_EXTENSION;
  326. }
  327. return 0;
  328. }
  329. static const AVClass modplug_class = {
  330. .class_name = "ModPlug demuxer",
  331. .item_name = av_default_item_name,
  332. .option = options,
  333. .version = LIBAVUTIL_VERSION_INT,
  334. };
  335. AVInputFormat ff_libmodplug_demuxer = {
  336. .name = "libmodplug",
  337. .long_name = NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
  338. .priv_data_size = sizeof(ModPlugContext),
  339. .read_probe = modplug_probe,
  340. .read_header = modplug_read_header,
  341. .read_packet = modplug_read_packet,
  342. .read_close = modplug_read_close,
  343. .read_seek = modplug_read_seek,
  344. .extensions = modplug_extensions,
  345. .priv_class = &modplug_class,
  346. };