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.

214 lines
7.5KB

  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;
  68. char *buf;
  69. int ret;
  70. size = avio_size(s->pb);
  71. if (size <= 0)
  72. return AVERROR_INVALIDDATA;
  73. buf = av_malloc(size);
  74. if (!buf)
  75. return AVERROR(ENOMEM);
  76. size = avio_read(s->pb, buf, size);
  77. if (size < 0) {
  78. av_log(s, AV_LOG_ERROR, "Reading input buffer failed.\n");
  79. av_freep(&buf);
  80. return size;
  81. }
  82. openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
  83. av_freep(&buf);
  84. if (!openmpt->module)
  85. return AVERROR_INVALIDDATA;
  86. openmpt->channels = av_get_channel_layout_nb_channels(openmpt->layout);
  87. if (openmpt->subsong >= openmpt_module_get_num_subsongs(openmpt->module)) {
  88. openmpt_module_destroy(openmpt->module);
  89. av_log(s, AV_LOG_ERROR, "Invalid subsong index: %d\n", openmpt->subsong);
  90. return AVERROR(EINVAL);
  91. }
  92. if (openmpt->subsong != -2) {
  93. if (openmpt->subsong >= 0) {
  94. av_dict_set_int(&s->metadata, "track", openmpt->subsong + 1, 0);
  95. }
  96. ret = openmpt_module_select_subsong(openmpt->module, openmpt->subsong);
  97. if (!ret){
  98. openmpt_module_destroy(openmpt->module);
  99. av_log(s, AV_LOG_ERROR, "Could not select requested subsong: %d", openmpt->subsong);
  100. return AVERROR(EINVAL);
  101. }
  102. }
  103. openmpt->duration = openmpt_module_get_duration_seconds(openmpt->module);
  104. add_meta(s, "artist", openmpt_module_get_metadata(openmpt->module, "artist"));
  105. add_meta(s, "title", openmpt_module_get_metadata(openmpt->module, "title"));
  106. add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
  107. add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
  108. add_meta(s, "date", openmpt_module_get_metadata(openmpt->module, "date"));
  109. st = avformat_new_stream(s, NULL);
  110. if (!st) {
  111. openmpt_module_destroy(openmpt->module);
  112. openmpt->module = NULL;
  113. return AVERROR(ENOMEM);
  114. }
  115. avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE);
  116. st->duration = llrint(openmpt->duration*AV_TIME_BASE);
  117. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  118. st->codecpar->codec_id = AV_NE(AV_CODEC_ID_PCM_F32BE, AV_CODEC_ID_PCM_F32LE);
  119. st->codecpar->channels = openmpt->channels;
  120. st->codecpar->sample_rate = openmpt->sample_rate;
  121. return 0;
  122. }
  123. #define AUDIO_PKT_SIZE 2048
  124. static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
  125. {
  126. OpenMPTContext *openmpt = s->priv_data;
  127. int n_samples = AUDIO_PKT_SIZE / (openmpt->channels ? openmpt->channels*4 : 4);
  128. int ret;
  129. if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
  130. return ret;
  131. switch (openmpt->channels) {
  132. case 1:
  133. ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
  134. n_samples, (float *)pkt->data);
  135. break;
  136. case 2:
  137. ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
  138. n_samples, (float *)pkt->data);
  139. break;
  140. case 4:
  141. ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
  142. n_samples, (float *)pkt->data);
  143. break;
  144. default:
  145. av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->channels);
  146. return AVERROR(EINVAL);
  147. }
  148. if (ret < 1) {
  149. pkt->size = 0;
  150. return AVERROR_EOF;
  151. }
  152. pkt->size = ret * (openmpt->channels * 4);
  153. return 0;
  154. }
  155. static int read_close_openmpt(AVFormatContext *s)
  156. {
  157. OpenMPTContext *openmpt = s->priv_data;
  158. openmpt_module_destroy(openmpt->module);
  159. openmpt->module = NULL;
  160. return 0;
  161. }
  162. static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
  163. {
  164. OpenMPTContext *openmpt = s->priv_data;
  165. openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
  166. return 0;
  167. }
  168. static const AVClass class_openmpt = {
  169. .class_name = "libopenmpt",
  170. .item_name = av_default_item_name,
  171. .option = options,
  172. .version = LIBAVUTIL_VERSION_INT,
  173. };
  174. AVInputFormat ff_libopenmpt_demuxer = {
  175. .name = "libopenmpt",
  176. .long_name = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
  177. .priv_data_size = sizeof(OpenMPTContext),
  178. .read_header = read_header_openmpt,
  179. .read_packet = read_packet_openmpt,
  180. .read_close = read_close_openmpt,
  181. .read_seek = read_seek_openmpt,
  182. .priv_class = &class_openmpt,
  183. .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",
  184. };