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.

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