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.

107 lines
3.3KB

  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. ModPlugContext *modplug = s->priv_data;
  61. if (av_new_packet(pkt, 512) < 0)
  62. return AVERROR(ENOMEM);
  63. pkt->size = ModPlug_Read(modplug->f, pkt->data, 512);
  64. if (pkt->size <= 0) {
  65. av_free_packet(pkt);
  66. return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
  67. }
  68. return 0;
  69. }
  70. static int modplug_read_close(AVFormatContext *s)
  71. {
  72. ModPlugContext *modplug = s->priv_data;
  73. ModPlug_Unload(modplug->f);
  74. return 0;
  75. }
  76. static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  77. {
  78. const ModPlugContext *modplug = s->priv_data;
  79. ModPlug_Seek(modplug->f, (int)ts);
  80. return 0;
  81. }
  82. AVInputFormat ff_libmodplug_demuxer = {
  83. .name = "libmodplug",
  84. .long_name = NULL_IF_CONFIG_SMALL("ModPlug demuxer"),
  85. .priv_data_size = sizeof(ModPlugContext),
  86. .read_header = modplug_read_header,
  87. .read_packet = modplug_read_packet,
  88. .read_close = modplug_read_close,
  89. .read_seek = modplug_read_seek,
  90. .extensions = "669,abc,amf,ams,dbm,dmf,dsm,far,it,mdl,med,mid,mod,mt2,mtm,okt,psm,ptm,s3m,stm,ult,umx,xm"
  91. ",itgz,itr,itz,mdgz,mdr,mdz,s3gz,s3r,s3z,xmgz,xmr,xmz", // compressed mods
  92. };