]> git.sesse.net Git - ffmpeg/blob - libavformat/libopenmpt.c
a87283b77be114f8aa86aef95cdfae077296e952
[ffmpeg] / libavformat / libopenmpt.c
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
22 #include <libopenmpt/libopenmpt.h>
23 #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
24
25 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "internal.h"
29
30 typedef struct OpenMPTContext {
31     const AVClass *class;
32     openmpt_module *module;
33
34     int channels;
35     double duration;
36     /* options */
37     int sample_rate;
38     int64_t layout;
39     int subsong;
40 } OpenMPTContext;
41
42 #define OFFSET(x) offsetof(OpenMPTContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM
44 #define D AV_OPT_FLAG_DECODING_PARAM
45 static const AVOption options[] = {
46     { "sample_rate", "set sample rate",    OFFSET(sample_rate), AV_OPT_TYPE_INT,            { .i64 = 48000 },               1000, INT_MAX,   A | D },
47     { "layout",      "set channel layout", OFFSET(layout),      AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_STEREO }, 0,    INT64_MAX, A | D },
48     { "subsong",     "set subsong",        OFFSET(subsong),     AV_OPT_TYPE_INT,            { .i64 = -2 },                  -2,   INT_MAX,   A | D, "subsong"},
49     { "all",         "all",                0,                   AV_OPT_TYPE_CONST,          { .i64 = -1},                   0,    0,         A | D, "subsong" },
50     { "auto",        "auto",               0,                   AV_OPT_TYPE_CONST,          { .i64 = -2},                   0,    0,         A | D, "subsong" },
51     { NULL }
52 };
53
54 static void openmpt_logfunc(const char *message, void *userdata)
55 {
56     int level = AV_LOG_INFO;
57     if (strstr(message, "ERROR") != NULL) {
58         level = AV_LOG_ERROR;
59     }
60     av_log(userdata, level, "%s\n", message);
61 }
62
63 #define add_meta(s, name, meta)                    \
64 do {                                               \
65     const char *value = meta;                      \
66     if (value && value[0])                         \
67         av_dict_set(&s->metadata, name, value, 0); \
68     openmpt_free_string(value);                    \
69 } while(0)
70
71 static int read_header_openmpt(AVFormatContext *s)
72 {
73     AVStream *st;
74     OpenMPTContext *openmpt = s->priv_data;
75     int64_t size = avio_size(s->pb);
76     char *buf = av_malloc(size);
77     int ret;
78
79
80     if (!buf)
81         return AVERROR(ENOMEM);
82     size = avio_read(s->pb, buf, size);
83
84     openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
85     av_freep(&buf);
86     if (!openmpt->module)
87             return AVERROR_INVALIDDATA;
88
89     openmpt->channels   = av_get_channel_layout_nb_channels(openmpt->layout);
90     openmpt->duration   = openmpt_module_get_duration_seconds(openmpt->module);
91
92     add_meta(s, "artist",  openmpt_module_get_metadata(openmpt->module, "artist"));
93     add_meta(s, "title",   openmpt_module_get_metadata(openmpt->module, "title"));
94     add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
95     add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
96
97     if (openmpt->subsong >= openmpt_module_get_num_subsongs(openmpt->module)) {
98         openmpt_module_destroy(openmpt->module);
99         av_log(s, AV_LOG_ERROR, "Invalid subsong index: %d\n", openmpt->subsong);
100         return AVERROR(EINVAL);
101     }
102
103     if (openmpt->subsong != -2) {
104         if (openmpt->subsong >= 0) {
105             av_dict_set_int(&s->metadata, "track", openmpt->subsong + 1, 0);
106         }
107         ret = openmpt_module_select_subsong(openmpt->module, openmpt->subsong);
108         if (!ret){
109             openmpt_module_destroy(openmpt->module);
110             av_log(s, AV_LOG_ERROR, "Could not select requested subsong: %d", openmpt->subsong);
111             return AVERROR(EINVAL);
112         }
113     }
114
115     st = avformat_new_stream(s, NULL);
116     if (!st) {
117         openmpt_module_destroy(openmpt->module);
118         openmpt->module = NULL;
119         return AVERROR(ENOMEM);
120     }
121     avpriv_set_pts_info(st, 64, 1, 1000);
122     if (st->duration > 0)
123         st->duration = llrint(openmpt->duration*AV_TIME_BASE);
124
125     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
126     st->codecpar->codec_id    = AV_NE(AV_CODEC_ID_PCM_F32BE, AV_CODEC_ID_PCM_F32LE);
127     st->codecpar->channels    = openmpt->channels;
128     st->codecpar->sample_rate = openmpt->sample_rate;
129
130     return 0;
131 }
132
133 #define AUDIO_PKT_SIZE 2048
134
135 static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
136 {
137     OpenMPTContext *openmpt = s->priv_data;
138     int n_samples = AUDIO_PKT_SIZE / (openmpt->channels ? openmpt->channels*4 : 4);
139     int ret;
140
141     if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
142         return ret;
143
144     switch (openmpt->channels) {
145     case 1:
146         ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
147                                              n_samples, (float *)pkt->data);
148         break;
149     case 2:
150         ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
151                                                            n_samples, (float *)pkt->data);
152         break;
153     case 4:
154         ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
155                                                          n_samples, (float *)pkt->data);
156         break;
157     default:
158         av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->channels);
159         return AVERROR(EINVAL);
160     }
161
162     if (ret < 1) {
163         pkt->size = 0;
164         return AVERROR_EOF;
165     }
166
167     pkt->size = ret * (openmpt->channels * 4);
168
169     return 0;
170 }
171
172 static int read_close_openmpt(AVFormatContext *s)
173 {
174     OpenMPTContext *openmpt = s->priv_data;
175     openmpt_module_destroy(openmpt->module);
176     openmpt->module = NULL;
177     return 0;
178 }
179
180 static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
181 {
182     OpenMPTContext *openmpt = s->priv_data;
183     openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
184     return 0;
185 }
186
187 static const AVClass class_openmpt = {
188     .class_name = "libopenmpt",
189     .item_name  = av_default_item_name,
190     .option     = options,
191     .version    = LIBAVUTIL_VERSION_INT,
192 };
193
194 AVInputFormat ff_libopenmpt_demuxer = {
195     .name           = "libopenmpt",
196     .long_name      = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
197     .priv_data_size = sizeof(OpenMPTContext),
198     .read_header    = read_header_openmpt,
199     .read_packet    = read_packet_openmpt,
200     .read_close     = read_close_openmpt,
201     .read_seek      = read_seek_openmpt,
202     .priv_class     = &class_openmpt,
203     .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",
204 };