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.

237 lines
9.9KB

  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. #include <libmodplug/modplug.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/opt.h"
  26. #include "avformat.h"
  27. typedef struct ModPlugContext {
  28. const AVClass *class;
  29. ModPlugFile *f;
  30. uint8_t *buf; ///< input file content
  31. /* options */
  32. int noise_reduction;
  33. int reverb_depth;
  34. int reverb_delay;
  35. int bass_amount;
  36. int bass_range;
  37. int surround_depth;
  38. int surround_delay;
  39. int max_size; ///< max file size to allocate
  40. } ModPlugContext;
  41. #define FF_MODPLUG_MAX_FILE_SIZE (100 * 1<<20) // 100M
  42. #define FF_MODPLUG_DEF_FILE_SIZE ( 5 * 1<<20) // 5M
  43. #define OFFSET(x) offsetof(ModPlugContext, x)
  44. #define D AV_OPT_FLAG_DECODING_PARAM
  45. static const AVOption options[] = {
  46. {"noise_reduction", "Enable noise reduction 0(off)-1(on)", OFFSET(noise_reduction), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, D},
  47. {"reverb_depth", "Reverb level 0(quiet)-100(loud)", OFFSET(reverb_depth), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
  48. {"reverb_delay", "Reverb delay in ms, usually 40-200ms", OFFSET(reverb_delay), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, D},
  49. {"bass_amount", "XBass level 0(quiet)-100(loud)", OFFSET(bass_amount), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
  50. {"bass_range", "XBass cutoff in Hz 10-100", OFFSET(bass_range), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
  51. {"surround_depth", "Surround level 0(quiet)-100(heavy)", OFFSET(surround_depth), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 100, D},
  52. {"surround_delay", "Surround delay in ms, usually 5-40ms", OFFSET(surround_delay), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, D},
  53. {"max_size", "Max file size supported (in bytes). Default is 5MB. Set to 0 for no limit (not recommended)",
  54. OFFSET(max_size), FF_OPT_TYPE_INT, {.dbl = FF_MODPLUG_DEF_FILE_SIZE}, 0, FF_MODPLUG_MAX_FILE_SIZE, D},
  55. {NULL},
  56. };
  57. #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do { \
  58. if (modplug->opt) { \
  59. settings.libopt = modplug->opt; \
  60. settings.mFlags |= flag; \
  61. } \
  62. } while (0)
  63. #define ADD_META_MULTIPLE_ENTRIES(entry_name, fname) do { \
  64. if (n_## entry_name ##s) { \
  65. unsigned i, n = 0; \
  66. \
  67. for (i = 0; i < n_## entry_name ##s; i++) { \
  68. char item_name[64] = {0}; \
  69. fname(f, i, item_name); \
  70. if (!*item_name) \
  71. continue; \
  72. if (n) \
  73. av_dict_set(&s->metadata, #entry_name, "\n", AV_DICT_APPEND); \
  74. av_dict_set(&s->metadata, #entry_name, item_name, AV_DICT_APPEND); \
  75. n++; \
  76. } \
  77. \
  78. extra = av_asprintf(", %u/%u " #entry_name "%s", \
  79. n, n_## entry_name ##s, n > 1 ? "s" : ""); \
  80. if (!extra) \
  81. return AVERROR(ENOMEM); \
  82. av_dict_set(&s->metadata, "extra info", extra, AV_DICT_APPEND); \
  83. av_free(extra); \
  84. } \
  85. } while (0)
  86. static int modplug_load_metadata(AVFormatContext *s)
  87. {
  88. ModPlugContext *modplug = s->priv_data;
  89. ModPlugFile *f = modplug->f;
  90. char *extra;
  91. const char *name = ModPlug_GetName(f);
  92. const char *msg = ModPlug_GetMessage(f);
  93. unsigned n_instruments = ModPlug_NumInstruments(f);
  94. unsigned n_samples = ModPlug_NumSamples(f);
  95. unsigned n_patterns = ModPlug_NumPatterns(f);
  96. unsigned n_channels = ModPlug_NumChannels(f);
  97. if (name && *name) av_dict_set(&s->metadata, "name", name, 0);
  98. if (msg && *msg) av_dict_set(&s->metadata, "message", msg, 0);
  99. extra = av_asprintf("%u pattern%s, %u channel%s",
  100. n_patterns, n_patterns > 1 ? "s" : "",
  101. n_channels, n_channels > 1 ? "s" : "");
  102. if (!extra)
  103. return AVERROR(ENOMEM);
  104. av_dict_set(&s->metadata, "extra info", extra, AV_DICT_DONT_STRDUP_VAL);
  105. ADD_META_MULTIPLE_ENTRIES(instrument, ModPlug_InstrumentName);
  106. ADD_META_MULTIPLE_ENTRIES(sample, ModPlug_SampleName);
  107. return 0;
  108. }
  109. static int modplug_read_header(AVFormatContext *s, AVFormatParameters *ap)
  110. {
  111. AVStream *st;
  112. AVIOContext *pb = s->pb;
  113. ModPlug_Settings settings;
  114. ModPlugContext *modplug = s->priv_data;
  115. int sz = avio_size(pb);
  116. if (sz < 0) {
  117. av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
  118. sz = modplug->max_size;
  119. } else if (modplug->max_size && sz > modplug->max_size) {
  120. sz = modplug->max_size;
  121. av_log(s, AV_LOG_WARNING, "Max file size reach%s, allocating %dB "
  122. "but demuxing is likely to fail due to incomplete buffer\n",
  123. sz == FF_MODPLUG_DEF_FILE_SIZE ? " (see -max_size)" : "", sz);
  124. }
  125. modplug->buf = av_malloc(modplug->max_size);
  126. if (!modplug->buf)
  127. return AVERROR(ENOMEM);
  128. sz = avio_read(pb, modplug->buf, sz);
  129. ModPlug_GetSettings(&settings);
  130. settings.mChannels = 2;
  131. settings.mBits = 16;
  132. settings.mFrequency = 44100;
  133. settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
  134. settings.mLoopCount = 0; // prevents looping forever
  135. if (modplug->noise_reduction) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
  136. SET_OPT_IF_REQUESTED(mReverbDepth, reverb_depth, MODPLUG_ENABLE_REVERB);
  137. SET_OPT_IF_REQUESTED(mReverbDelay, reverb_delay, MODPLUG_ENABLE_REVERB);
  138. SET_OPT_IF_REQUESTED(mBassAmount, bass_amount, MODPLUG_ENABLE_MEGABASS);
  139. SET_OPT_IF_REQUESTED(mBassRange, bass_range, MODPLUG_ENABLE_MEGABASS);
  140. SET_OPT_IF_REQUESTED(mSurroundDepth, surround_depth, MODPLUG_ENABLE_SURROUND);
  141. SET_OPT_IF_REQUESTED(mSurroundDelay, surround_delay, MODPLUG_ENABLE_SURROUND);
  142. if (modplug->reverb_depth) settings.mReverbDepth = modplug->reverb_depth;
  143. if (modplug->reverb_delay) settings.mReverbDelay = modplug->reverb_delay;
  144. if (modplug->bass_amount) settings.mBassAmount = modplug->bass_amount;
  145. if (modplug->bass_range) settings.mBassRange = modplug->bass_range;
  146. if (modplug->surround_depth) settings.mSurroundDepth = modplug->surround_depth;
  147. if (modplug->surround_delay) settings.mSurroundDelay = modplug->surround_delay;
  148. ModPlug_SetSettings(&settings);
  149. modplug->f = ModPlug_Load(modplug->buf, sz);
  150. if (!modplug->f)
  151. return AVERROR_INVALIDDATA;
  152. st = av_new_stream(s, 0);
  153. if (!st)
  154. return AVERROR(ENOMEM);
  155. av_set_pts_info(st, 64, 1, 1000);
  156. st->duration = ModPlug_GetLength(modplug->f);
  157. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  158. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  159. st->codec->channels = settings.mChannels;
  160. st->codec->sample_rate = settings.mFrequency;
  161. return modplug_load_metadata(s);
  162. }
  163. static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
  164. {
  165. ModPlugContext *modplug = s->priv_data;
  166. if (av_new_packet(pkt, 512) < 0)
  167. return AVERROR(ENOMEM);
  168. pkt->size = ModPlug_Read(modplug->f, pkt->data, 512);
  169. if (pkt->size <= 0) {
  170. av_free_packet(pkt);
  171. return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
  172. }
  173. return 0;
  174. }
  175. static int modplug_read_close(AVFormatContext *s)
  176. {
  177. ModPlugContext *modplug = s->priv_data;
  178. ModPlug_Unload(modplug->f);
  179. av_freep(&modplug->buf);
  180. return 0;
  181. }
  182. static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  183. {
  184. const ModPlugContext *modplug = s->priv_data;
  185. ModPlug_Seek(modplug->f, (int)ts);
  186. return 0;
  187. }
  188. static const AVClass modplug_class = {
  189. .class_name = "ModPlug demuxer",
  190. .item_name = av_default_item_name,
  191. .option = options,
  192. .version = LIBAVUTIL_VERSION_INT,
  193. };
  194. AVInputFormat ff_libmodplug_demuxer = {
  195. .name = "libmodplug",
  196. .long_name = NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
  197. .priv_data_size = sizeof(ModPlugContext),
  198. .read_header = modplug_read_header,
  199. .read_packet = modplug_read_packet,
  200. .read_close = modplug_read_close,
  201. .read_seek = modplug_read_seek,
  202. .extensions = "669,abc,amf,ams,dbm,dmf,dsm,far,it,mdl,med,mid,mod,mt2,mtm,okt,psm,ptm,s3m,stm,ult,umx,xm"
  203. ",itgz,itr,itz,mdgz,mdr,mdz,s3gz,s3r,s3z,xmgz,xmr,xmz", // compressed mods
  204. .priv_class = &modplug_class,
  205. };