FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libopenmpt.c
Go to the documentation of this file.
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 */
38  int64_t layout;
39  int subsong;
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 
72 {
73  AVStream *st;
74  OpenMPTContext *openmpt = s->priv_data;
75  int64_t size = avio_size(s->pb);
76  if (size <= 0)
77  return AVERROR_INVALIDDATA;
78  char *buf = av_malloc(size);
79  int ret;
80 
81 
82  if (!buf)
83  return AVERROR(ENOMEM);
84  size = avio_read(s->pb, buf, size);
85  if (size < 0) {
86  av_log(s, AV_LOG_ERROR, "Reading input buffer failed.\n");
87  av_freep(&buf);
88  return size;
89  }
90 
91  openmpt->module = openmpt_module_create_from_memory(buf, size, openmpt_logfunc, s, NULL);
92  av_freep(&buf);
93  if (!openmpt->module)
94  return AVERROR_INVALIDDATA;
95 
97 
98  if (openmpt->subsong >= openmpt_module_get_num_subsongs(openmpt->module)) {
99  openmpt_module_destroy(openmpt->module);
100  av_log(s, AV_LOG_ERROR, "Invalid subsong index: %d\n", openmpt->subsong);
101  return AVERROR(EINVAL);
102  }
103 
104  if (openmpt->subsong != -2) {
105  if (openmpt->subsong >= 0) {
106  av_dict_set_int(&s->metadata, "track", openmpt->subsong + 1, 0);
107  }
108  ret = openmpt_module_select_subsong(openmpt->module, openmpt->subsong);
109  if (!ret){
110  openmpt_module_destroy(openmpt->module);
111  av_log(s, AV_LOG_ERROR, "Could not select requested subsong: %d", openmpt->subsong);
112  return AVERROR(EINVAL);
113  }
114  }
115 
116  openmpt->duration = openmpt_module_get_duration_seconds(openmpt->module);
117 
118  add_meta(s, "artist", openmpt_module_get_metadata(openmpt->module, "artist"));
119  add_meta(s, "title", openmpt_module_get_metadata(openmpt->module, "title"));
120  add_meta(s, "encoder", openmpt_module_get_metadata(openmpt->module, "tracker"));
121  add_meta(s, "comment", openmpt_module_get_metadata(openmpt->module, "message"));
122  add_meta(s, "date", openmpt_module_get_metadata(openmpt->module, "date"));
123 
124  st = avformat_new_stream(s, NULL);
125  if (!st) {
126  openmpt_module_destroy(openmpt->module);
127  openmpt->module = NULL;
128  return AVERROR(ENOMEM);
129  }
130  avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE);
131  st->duration = llrint(openmpt->duration*AV_TIME_BASE);
132 
135  st->codecpar->channels = openmpt->channels;
136  st->codecpar->sample_rate = openmpt->sample_rate;
137 
138  return 0;
139 }
140 
141 #define AUDIO_PKT_SIZE 2048
142 
144 {
145  OpenMPTContext *openmpt = s->priv_data;
146  int n_samples = AUDIO_PKT_SIZE / (openmpt->channels ? openmpt->channels*4 : 4);
147  int ret;
148 
149  if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
150  return ret;
151 
152  switch (openmpt->channels) {
153  case 1:
154  ret = openmpt_module_read_float_mono(openmpt->module, openmpt->sample_rate,
155  n_samples, (float *)pkt->data);
156  break;
157  case 2:
158  ret = openmpt_module_read_interleaved_float_stereo(openmpt->module, openmpt->sample_rate,
159  n_samples, (float *)pkt->data);
160  break;
161  case 4:
162  ret = openmpt_module_read_interleaved_float_quad(openmpt->module, openmpt->sample_rate,
163  n_samples, (float *)pkt->data);
164  break;
165  default:
166  av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d", openmpt->channels);
167  return AVERROR(EINVAL);
168  }
169 
170  if (ret < 1) {
171  pkt->size = 0;
172  return AVERROR_EOF;
173  }
174 
175  pkt->size = ret * (openmpt->channels * 4);
176 
177  return 0;
178 }
179 
181 {
182  OpenMPTContext *openmpt = s->priv_data;
183  openmpt_module_destroy(openmpt->module);
184  openmpt->module = NULL;
185  return 0;
186 }
187 
188 static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
189 {
190  OpenMPTContext *openmpt = s->priv_data;
191  openmpt_module_set_position_seconds(openmpt->module, (double)ts/AV_TIME_BASE);
192  return 0;
193 }
194 
195 static const AVClass class_openmpt = {
196  .class_name = "libopenmpt",
197  .item_name = av_default_item_name,
198  .option = options,
199  .version = LIBAVUTIL_VERSION_INT,
200 };
201 
203  .name = "libopenmpt",
204  .long_name = NULL_IF_CONFIG_SMALL("Tracker formats (libopenmpt)"),
205  .priv_data_size = sizeof(OpenMPTContext),
210  .priv_class = &class_openmpt,
211  .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",
212 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:334
AVOption.
Definition: opt.h:246
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4737
static int read_packet_openmpt(AVFormatContext *s, AVPacket *pkt)
Definition: libopenmpt.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
int size
Definition: avcodec.h:1680
static int read_header_openmpt(AVFormatContext *s)
Definition: libopenmpt.c:71
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
double duration
Definition: libopenmpt.c:35
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Format I/O context.
Definition: avformat.h:1349
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_malloc(s)
AVOptions.
static int read_close_openmpt(AVFormatContext *s)
Definition: libopenmpt.c:180
#define AV_NE(be, le)
Definition: common.h:50
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
uint8_t * data
Definition: avcodec.h:1679
#define AUDIO_PKT_SIZE
Definition: libopenmpt.c:141
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static const AVClass class_openmpt
Definition: libopenmpt.c:195
ptrdiff_t size
Definition: opengl_enc.c:101
#define A
Definition: libopenmpt.c:43
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
int64_t layout
Definition: libopenmpt.c:38
static const AVOption options[]
Definition: libopenmpt.c:45
#define OFFSET(x)
Definition: libopenmpt.c:42
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define D
Definition: libopenmpt.c:44
sample_rate
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
AVInputFormat ff_libopenmpt_demuxer
Definition: libopenmpt.c:202
void * buf
Definition: avisynth_c.h:690
#define llrint(x)
Definition: libm.h:394
Describe the class of an AVClass context structure.
Definition: log.h:67
uint8_t level
Definition: svq3.c:207
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
#define add_meta(s, name, meta)
Definition: libopenmpt.c:63
int sample_rate
Audio only.
Definition: avcodec.h:4262
Main libavformat public API header.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:147
void * priv_data
Format private data.
Definition: avformat.h:1377
uint64_t layout
int channels
Audio only.
Definition: avcodec.h:4258
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
static int read_seek_openmpt(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
Definition: libopenmpt.c:188
This structure stores compressed data.
Definition: avcodec.h:1656
openmpt_module * module
Definition: libopenmpt.c:32
static void openmpt_logfunc(const char *message, void *userdata)
Definition: libopenmpt.c:54