Libav
mmf.c
Go to the documentation of this file.
1 /*
2  * Yamaha SMAF format
3  * Copyright (c) 2005 Vidar Madsen
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "internal.h"
26 #include "pcm.h"
27 #include "riff.h"
28 
29 typedef struct {
30  int64_t atrpos, atsqpos, awapos;
31  int64_t data_size;
32 } MMFContext;
33 
34 static const int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
35 
36 static int mmf_rate(int code)
37 {
38  if ((code < 0) || (code > 4))
39  return -1;
40  return mmf_rates[code];
41 }
42 
43 #if CONFIG_MMF_MUXER
44 static int mmf_rate_code(int rate)
45 {
46  int i;
47  for (i = 0; i < 5; i++)
48  if (mmf_rates[i] == rate)
49  return i;
50  return -1;
51 }
52 
53 /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
54 static void end_tag_be(AVIOContext *pb, int64_t start)
55 {
56  int64_t pos;
57 
58  pos = avio_tell(pb);
59  avio_seek(pb, start - 4, SEEK_SET);
60  avio_wb32(pb, (uint32_t)(pos - start));
61  avio_seek(pb, pos, SEEK_SET);
62 }
63 
64 static int mmf_write_header(AVFormatContext *s)
65 {
66  MMFContext *mmf = s->priv_data;
67  AVIOContext *pb = s->pb;
68  int64_t pos;
69  int rate;
70 
71  rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
72  if (rate < 0) {
73  av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n",
74  s->streams[0]->codec->sample_rate);
75  return -1;
76  }
77 
78  ffio_wfourcc(pb, "MMMD");
79  avio_wb32(pb, 0);
80  pos = ff_start_tag(pb, "CNTI");
81  avio_w8(pb, 0); /* class */
82  avio_w8(pb, 0); /* type */
83  avio_w8(pb, 0); /* code type */
84  avio_w8(pb, 0); /* status */
85  avio_w8(pb, 0); /* counts */
86  end_tag_be(pb, pos);
87 
88  pos = ff_start_tag(pb, "OPDA");
89  avio_write(pb, "VN:libavcodec,", sizeof("VN:libavcodec,") -1); /* metadata ("ST:songtitle,VN:version,...") */
90  end_tag_be(pb, pos);
91 
92  avio_write(pb, "ATR\x00", 4);
93  avio_wb32(pb, 0);
94  mmf->atrpos = avio_tell(pb);
95  avio_w8(pb, 0); /* format type */
96  avio_w8(pb, 0); /* sequence type */
97  avio_w8(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
98  avio_w8(pb, 0); /* wave base bit */
99  avio_w8(pb, 2); /* time base d */
100  avio_w8(pb, 2); /* time base g */
101 
102  ffio_wfourcc(pb, "Atsq");
103  avio_wb32(pb, 16);
104  mmf->atsqpos = avio_tell(pb);
105  /* Will be filled on close */
106  avio_write(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
107 
108  mmf->awapos = ff_start_tag(pb, "Awa\x01");
109 
110  avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
111 
112  avio_flush(pb);
113 
114  return 0;
115 }
116 
117 static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
118 {
119  AVIOContext *pb = s->pb;
120  avio_write(pb, pkt->data, pkt->size);
121  return 0;
122 }
123 
124 /* Write a variable-length symbol */
125 static void put_varlength(AVIOContext *pb, int val)
126 {
127  if (val < 128)
128  avio_w8(pb, val);
129  else {
130  val -= 128;
131  avio_w8(pb, 0x80 | val >> 7);
132  avio_w8(pb, 0x7f & val);
133  }
134 }
135 
136 static int mmf_write_trailer(AVFormatContext *s)
137 {
138  AVIOContext *pb = s->pb;
139  MMFContext *mmf = s->priv_data;
140  int64_t pos, size;
141  int gatetime;
142 
143  if (s->pb->seekable) {
144  /* Fill in length fields */
145  end_tag_be(pb, mmf->awapos);
146  end_tag_be(pb, mmf->atrpos);
147  end_tag_be(pb, 8);
148 
149  pos = avio_tell(pb);
150  size = pos - mmf->awapos;
151 
152  /* Fill Atsq chunk */
153  avio_seek(pb, mmf->atsqpos, SEEK_SET);
154 
155  /* "play wav" */
156  avio_w8(pb, 0); /* start time */
157  avio_w8(pb, 1); /* (channel << 6) | wavenum */
158  gatetime = size * 500 / s->streams[0]->codec->sample_rate;
159  put_varlength(pb, gatetime); /* duration */
160 
161  /* "nop" */
162  put_varlength(pb, gatetime); /* start time */
163  avio_write(pb, "\xff\x00", 2); /* nop */
164 
165  /* "end of sequence" */
166  avio_write(pb, "\x00\x00\x00\x00", 4);
167 
168  avio_seek(pb, pos, SEEK_SET);
169 
170  avio_flush(pb);
171  }
172  return 0;
173 }
174 #endif /* CONFIG_MMF_MUXER */
175 
176 static int mmf_probe(AVProbeData *p)
177 {
178  /* check file header */
179  if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
180  p->buf[2] == 'M' && p->buf[3] == 'D' &&
181  p->buf[8] == 'C' && p->buf[9] == 'N' &&
182  p->buf[10] == 'T' && p->buf[11] == 'I')
183  return AVPROBE_SCORE_MAX;
184  else
185  return 0;
186 }
187 
188 /* mmf input */
190 {
191  MMFContext *mmf = s->priv_data;
192  unsigned int tag;
193  AVIOContext *pb = s->pb;
194  AVStream *st;
195  int64_t size;
196  int rate, params;
197 
198  tag = avio_rl32(pb);
199  if (tag != MKTAG('M', 'M', 'M', 'D'))
200  return -1;
201  avio_skip(pb, 4); /* file_size */
202 
203  /* Skip some unused chunks that may or may not be present */
204  for (;; avio_skip(pb, size)) {
205  tag = avio_rl32(pb);
206  size = avio_rb32(pb);
207  if (tag == MKTAG('C', 'N', 'T', 'I'))
208  continue;
209  if (tag == MKTAG('O', 'P', 'D', 'A'))
210  continue;
211  break;
212  }
213 
214  /* Tag = "ATRx", where "x" = track number */
215  if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
216  av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
217  return -1;
218  }
219  if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
220  av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
221  return -1;
222  }
223 
224  avio_r8(pb); /* format type */
225  avio_r8(pb); /* sequence type */
226  params = avio_r8(pb); /* (channel << 7) | (format << 4) | rate */
227  rate = mmf_rate(params & 0x0f);
228  if (rate < 0) {
229  av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
230  return -1;
231  }
232  avio_r8(pb); /* wave base bit */
233  avio_r8(pb); /* time base d */
234  avio_r8(pb); /* time base g */
235 
236  /* Skip some unused chunks that may or may not be present */
237  for (;; avio_skip(pb, size)) {
238  tag = avio_rl32(pb);
239  size = avio_rb32(pb);
240  if (tag == MKTAG('A', 't', 's', 'q'))
241  continue;
242  if (tag == MKTAG('A', 's', 'p', 'I'))
243  continue;
244  break;
245  }
246 
247  /* Make sure it's followed by an Awa chunk, aka wave data */
248  if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
249  av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
250  return -1;
251  }
252  mmf->data_size = size;
253 
254  st = avformat_new_stream(s, NULL);
255  if (!st)
256  return AVERROR(ENOMEM);
257 
260  st->codec->sample_rate = rate;
261  st->codec->channels = 1;
263  st->codec->bits_per_coded_sample = 4;
264  st->codec->bit_rate = st->codec->sample_rate *
266 
267  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
268 
269  return 0;
270 }
271 
272 #define MAX_SIZE 4096
273 
275 {
276  MMFContext *mmf = s->priv_data;
277  int ret, size;
278 
279  if (s->pb->eof_reached)
280  return AVERROR(EIO);
281 
282  size = MAX_SIZE;
283  if (size > mmf->data_size)
284  size = mmf->data_size;
285 
286  if (!size)
287  return AVERROR(EIO);
288 
289  if (av_new_packet(pkt, size))
290  return AVERROR(EIO);
291  pkt->stream_index = 0;
292 
293  ret = avio_read(s->pb, pkt->data, pkt->size);
294  if (ret < 0)
295  av_free_packet(pkt);
296 
297  mmf->data_size -= ret;
298 
299  pkt->size = ret;
300  return ret;
301 }
302 
303 #if CONFIG_MMF_DEMUXER
304 AVInputFormat ff_mmf_demuxer = {
305  .name = "mmf",
306  .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
307  .priv_data_size = sizeof(MMFContext),
312 };
313 #endif
314 
315 #if CONFIG_MMF_MUXER
316 AVOutputFormat ff_mmf_muxer = {
317  .name = "mmf",
318  .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
319  .mime_type = "application/vnd.smaf",
320  .extensions = "mmf",
321  .priv_data_size = sizeof(MMFContext),
322  .audio_codec = AV_CODEC_ID_ADPCM_YAMAHA,
323  .video_codec = AV_CODEC_ID_NONE,
324  .write_header = mmf_write_header,
325  .write_packet = mmf_write_packet,
326  .write_trailer = mmf_write_trailer,
327 };
328 #endif
Bytestream IO Context.
Definition: avio.h:68
int64_t awapos
Definition: mmf.c:30
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
Definition: riffenc.c:31
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:2829
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
Definition: mmf.c:29
static int mmf_probe(AVProbeData *p)
Definition: mmf.c:176
Format I/O context.
Definition: avformat.h:922
static int mmf_rate(int code)
Definition: mmf.c:36
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
uint8_t * data
Definition: avcodec.h:973
uint32_t tag
Definition: movenc.c:844
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
#define MAX_SIZE
Definition: mmf.c:272
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#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:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
int64_t data_size
Definition: mmf.c:31
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
Definition: avcodec.h:1114
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
const char * name
Definition: avformat.h:446
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int64_t atsqpos
Definition: mmf.c:30
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:26
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
static int mmf_read_header(AVFormatContext *s)
Definition: mmf.c:189
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static const int mmf_rates[]
Definition: mmf.c:34
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
static int mmf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mmf.c:274
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1792
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950
int64_t atrpos
Definition: mmf.c:30