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.

111 lines
3.4KB

  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 ModPlug options (noise reduction, reverb, bass boost, ...)
  22. * @todo metadata
  23. */
  24. #include <libmodplug/modplug.h>
  25. #include "avformat.h"
  26. typedef struct ModPlugContext {
  27. ModPlugFile *f;
  28. uint8_t buf[5 * 1<<20]; ///< input file content, 5M max
  29. } ModPlugContext;
  30. static int modplug_read_header(AVFormatContext *s, AVFormatParameters *ap)
  31. {
  32. AVStream *st;
  33. AVIOContext *pb = s->pb;
  34. ModPlug_Settings settings;
  35. ModPlugContext *modplug = s->priv_data;
  36. int sz = avio_read(pb, modplug->buf, sizeof modplug->buf);
  37. ModPlug_GetSettings(&settings);
  38. settings.mChannels = 2;
  39. settings.mBits = 16;
  40. settings.mFrequency = 44100;
  41. settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; // best quality
  42. settings.mLoopCount = 0; // prevents looping forever
  43. ModPlug_SetSettings(&settings);
  44. modplug->f = ModPlug_Load(modplug->buf, sz);
  45. if (!modplug->f)
  46. return AVERROR_INVALIDDATA;
  47. st = av_new_stream(s, 0);
  48. if (!st)
  49. return AVERROR(ENOMEM);
  50. av_set_pts_info(st, 64, 1, 1000);
  51. st->duration = ModPlug_GetLength(modplug->f);
  52. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  53. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  54. st->codec->channels = settings.mChannels;
  55. st->codec->sample_rate = settings.mFrequency;
  56. return 0;
  57. }
  58. static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
  59. {
  60. int ret, n;
  61. ModPlugContext *modplug = s->priv_data;
  62. uint8_t buf[512];
  63. n = ModPlug_Read(modplug->f, buf, sizeof buf);
  64. if (n <= 0)
  65. return AVERROR(EIO);
  66. ret = av_new_packet(pkt, n);
  67. if (ret)
  68. return ret;
  69. pkt->pts = pkt->dts = AV_NOPTS_VALUE;
  70. pkt->size = n;
  71. memcpy(pkt->data, buf, n);
  72. return 0;
  73. }
  74. static int modplug_read_close(AVFormatContext *s)
  75. {
  76. ModPlugContext *modplug = s->priv_data;
  77. ModPlug_Unload(modplug->f);
  78. return 0;
  79. }
  80. static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  81. {
  82. const ModPlugContext *modplug = s->priv_data;
  83. ModPlug_Seek(modplug->f, (int)ts);
  84. return 0;
  85. }
  86. AVInputFormat ff_libmodplug_demuxer = {
  87. .name = "libmodplug",
  88. .long_name = NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
  89. .priv_data_size = sizeof(ModPlugContext),
  90. .read_header = modplug_read_header,
  91. .read_packet = modplug_read_packet,
  92. .read_close = modplug_read_close,
  93. .read_seek = modplug_read_seek,
  94. .extensions = "669,abc,amf,ams,dbm,dmf,dsm,far,it,mdl,med,mid,mod,mt2,mtm,okt,psm,ptm,s3m,stm,ult,umx,xm"
  95. ",itgz,itr,itz,mdgz,mdr,mdz,s3gz,s3r,s3z,xmgz,xmr,xmz", // compressed mods
  96. };