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.

207 lines
7.4KB

  1. /*
  2. * Tracked MOD demuxer (libopenmpt)
  3. * Copyright (c) 2016 Josh de Kock
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <libopenmpt/libopenmpt.h>
  22. #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/opt.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. typedef struct OpenMPTContext {
  28. const AVClass *class;
  29. openmpt_module *module;
  30. int channels;
  31. double duration;
  32. /* options */
  33. int sample_rate;
  34. int64_t layout;
  35. int subsong;
  36. } OpenMPTContext;
  37. #define OFFSET(x) offsetof(OpenMPTContext, x)
  38. #define A AV_OPT_FLAG_AUDIO_PARAM
  39. #define D AV_OPT_FLAG_DECODING_PARAM
  40. static const AVOption options[] = {
  41. { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 48000 }, 1000, INT_MAX, A | D },
  42. { "layout", "set channel layout", OFFSET(layout), AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_STEREO }, 0, INT64_MAX, A | D },
  43. { "subsong", "set subsong", OFFSET(subsong), AV_OPT_TYPE_INT, { .i64 = -2 }, -2, INT_MAX, A | D, "subsong"},
  44. { "all", "all", 0, AV_OPT_TYPE_CONST, { .i64 = -1}, 0, 0, A | D, "subsong" },
  45. { "auto", "auto", 0, AV_OPT_TYPE_CONST, { .i64 = -2}, 0, 0, A | D, "subsong" },
  46. { NULL }
  47. };
  48. static void openmpt_logfunc(const char *message, void *userdata)
  49. {
  50. int level = AV_LOG_INFO;
  51. if (strstr(message, "ERROR") != NULL) {
  52. level = AV_LOG_ERROR;
  53. }
  54. av_log(userdata, level, "%s\n", message);
  55. }
  56. #define add_meta(s, name, meta) \
  57. do { \
  58. const char *value = meta; \
  59. if (value && value[0]) \
  60. av_dict_set(&s->metadata, name, value, 0); \
  61. openmpt_free_string(value); \
  62. } while(0)
  63. static int read_header_openmpt(AVFormatContext *s)
  64. {
  65. AVStream *st;
  66. OpenMPTContext *openmpt = s->priv_data;
  67. int64_t size = avio_size(s->pb);
  68. if (!size)
  69. return AVERROR_INVALIDDATA;
  70. char *buf = av_malloc(size);
  71. int ret;
  72. if (!buf)
  73. return AVERROR(ENOMEM);
  74. size = avio_read(s->pb, buf, size);
  75. openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
  76. av_freep(&buf);
  77. if (!openmpt->module)
  78. return AVERROR_INVALIDDATA;
  79. openmpt->channels = av_get_channel_layout_nb_channels(openmpt->layout);
  80. openmpt->duration = openmpt_module_get_duration_seconds(openmpt->module);
  81. add_meta(s, "artist", openmpt_module_get_metadata(openmpt->module, "artist"));
  82. add_meta(s, "title", openmpt_module_get_metadata(openmpt->module, "title"));
  83. add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
  84. add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
  85. add_meta(s, "date", openmpt_module_get_metadata(openmpt->module, "date"));
  86. if (openmpt->subsong >= openmpt_module_get_num_subsongs(openmpt->module)) {
  87. openmpt_module_destroy(openmpt->module);
  88. av_log(s, AV_LOG_ERROR, "Invalid subsong index: %d\n", openmpt->subsong);
  89. return AVERROR(EINVAL);
  90. }
  91. if (openmpt->subsong != -2) {
  92. if (openmpt->subsong >= 0) {
  93. av_dict_set_int(&s->metadata, "track", openmpt->subsong + 1, 0);
  94. }
  95. ret = openmpt_module_select_subsong(openmpt->module, openmpt->subsong);
  96. if (!ret){
  97. openmpt_module_destroy(openmpt->module);
  98. av_log(s, AV_LOG_ERROR, "Could not select requested subsong: %d", openmpt->subsong);
  99. return AVERROR(EINVAL);
  100. }
  101. }
  102. st = avformat_new_stream(s, NULL);
  103. if (!st) {
  104. openmpt_module_destroy(openmpt->module);
  105. openmpt->module = NULL;
  106. return AVERROR(ENOMEM);
  107. }
  108. avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE);
  109. st->duration = llrint(openmpt->duration*AV_TIME_BASE);
  110. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  111. st->codecpar->codec_id = AV_NE(AV_CODEC_ID_PCM_F32BE, AV_CODEC_ID_PCM_F32LE);
  112. st->codecpar->channels = openmpt->channels;
  113. st->codecpar->sample_rate = openmpt->sample_rate;
  114. return 0;
  115. }
  116. #define AUDIO_PKT_SIZE 2048
  117. static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
  118. {
  119. OpenMPTContext *openmpt = s->priv_data;
  120. int n_samples = AUDIO_PKT_SIZE / (openmpt->channels ? openmpt->channels*4 : 4);
  121. int ret;
  122. if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
  123. return ret;
  124. switch (openmpt->channels) {
  125. case 1:
  126. ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
  127. n_samples, (float *)pkt->data);
  128. break;
  129. case 2:
  130. ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
  131. n_samples, (float *)pkt->data);
  132. break;
  133. case 4:
  134. ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
  135. n_samples, (float *)pkt->data);
  136. break;
  137. default:
  138. av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->channels);
  139. return AVERROR(EINVAL);
  140. }
  141. if (ret < 1) {
  142. pkt->size = 0;
  143. return AVERROR_EOF;
  144. }
  145. pkt->size = ret * (openmpt->channels * 4);
  146. return 0;
  147. }
  148. static int read_close_openmpt(AVFormatContext *s)
  149. {
  150. OpenMPTContext *openmpt = s->priv_data;
  151. openmpt_module_destroy(openmpt->module);
  152. openmpt->module = NULL;
  153. return 0;
  154. }
  155. static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  156. {
  157. OpenMPTContext *openmpt = s->priv_data;
  158. openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
  159. return 0;
  160. }
  161. static const AVClass class_openmpt = {
  162. .class_name = "libopenmpt",
  163. .item_name = av_default_item_name,
  164. .option = options,
  165. .version = LIBAVUTIL_VERSION_INT,
  166. };
  167. AVInputFormat ff_libopenmpt_demuxer = {
  168. .name = "libopenmpt",
  169. .long_name = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
  170. .priv_data_size = sizeof(OpenMPTContext),
  171. .read_header = read_header_openmpt,
  172. .read_packet = read_packet_openmpt,
  173. .read_close = read_close_openmpt,
  174. .read_seek = read_seek_openmpt,
  175. .priv_class = &class_openmpt,
  176. .extensions = "669,amf,ams,dbm,digi,dmf,dsm,far,gdm,imf,it,j2b,m15,mdl,med,mmcmp,mms,mo3,mod,mptm,mt2,mtm,nst,okt,plm,ppm,psm,pt36,ptm,s3m,sfx,sfx2,stk,stm,ult,umx,wow,xm,xpk",
  177. };