3 * Copyright (c) 2005 Vidar Madsen
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/channel_layout.h"
24 #include "avio_internal.h"
29 typedef struct MMFContext {
30 int64_t atrpos, atsqpos, awapos;
34 static const int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
36 static int mmf_rate(int code)
38 if ((code < 0) || (code > 4))
40 return mmf_rates[code];
44 static int mmf_rate_code(int rate)
47 for (i = 0; i < 5; i++)
48 if (mmf_rates[i] == rate)
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)
59 avio_seek(pb, start - 4, SEEK_SET);
60 avio_wb32(pb, (uint32_t)(pos - start));
61 avio_seek(pb, pos, SEEK_SET);
64 static int mmf_write_header(AVFormatContext *s)
66 MMFContext *mmf = s->priv_data;
67 AVIOContext *pb = s->pb;
71 rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
73 av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n",
74 s->streams[0]->codec->sample_rate);
78 ffio_wfourcc(pb, "MMMD");
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 */
88 pos = ff_start_tag(pb, "OPDA");
89 avio_write(pb, "VN:libavcodec,", sizeof("VN:libavcodec,") -1); /* metadata ("ST:songtitle,VN:version,...") */
92 avio_write(pb, "ATR\x00", 4);
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 */
102 ffio_wfourcc(pb, "Atsq");
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);
108 mmf->awapos = ff_start_tag(pb, "Awa\x01");
110 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
117 static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
119 AVIOContext *pb = s->pb;
120 avio_write(pb, pkt->data, pkt->size);
124 /* Write a variable-length symbol */
125 static void put_varlength(AVIOContext *pb, int val)
131 avio_w8(pb, 0x80 | val >> 7);
132 avio_w8(pb, 0x7f & val);
136 static int mmf_write_trailer(AVFormatContext *s)
138 AVIOContext *pb = s->pb;
139 MMFContext *mmf = s->priv_data;
143 if (s->pb->seekable) {
144 /* Fill in length fields */
145 end_tag_be(pb, mmf->awapos);
146 end_tag_be(pb, mmf->atrpos);
150 size = pos - mmf->awapos;
152 /* Fill Atsq chunk */
153 avio_seek(pb, mmf->atsqpos, SEEK_SET);
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 */
162 put_varlength(pb, gatetime); /* start time */
163 avio_write(pb, "\xff\x00", 2); /* nop */
165 /* "end of sequence" */
166 avio_write(pb, "\x00\x00\x00\x00", 4);
168 avio_seek(pb, pos, SEEK_SET);
174 #endif /* CONFIG_MMF_MUXER */
176 static int mmf_probe(AVProbeData *p)
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;
189 static int mmf_read_header(AVFormatContext *s)
191 MMFContext *mmf = s->priv_data;
193 AVIOContext *pb = s->pb;
199 if (tag != MKTAG('M', 'M', 'M', 'D'))
201 avio_skip(pb, 4); /* file_size */
203 /* Skip some unused chunks that may or may not be present */
204 for (;; avio_skip(pb, size)) {
206 size = avio_rb32(pb);
207 if (tag == MKTAG('C', 'N', 'T', 'I'))
209 if (tag == MKTAG('O', 'P', 'D', 'A'))
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");
219 if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
220 av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
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);
229 av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
232 avio_r8(pb); /* wave base bit */
233 avio_r8(pb); /* time base d */
234 avio_r8(pb); /* time base g */
236 /* Skip some unused chunks that may or may not be present */
237 for (;; avio_skip(pb, size)) {
239 size = avio_rb32(pb);
240 if (tag == MKTAG('A', 't', 's', 'q'))
242 if (tag == MKTAG('A', 's', 'p', 'I'))
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);
252 mmf->data_size = size;
254 st = avformat_new_stream(s, NULL);
256 return AVERROR(ENOMEM);
258 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
259 st->codec->codec_id = AV_CODEC_ID_ADPCM_YAMAHA;
260 st->codec->sample_rate = rate;
261 st->codec->channels = 1;
262 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
263 st->codec->bits_per_coded_sample = 4;
264 st->codec->bit_rate = st->codec->sample_rate *
265 st->codec->bits_per_coded_sample;
267 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
272 #define MAX_SIZE 4096
274 static int mmf_read_packet(AVFormatContext *s, AVPacket *pkt)
276 MMFContext *mmf = s->priv_data;
279 if (s->pb->eof_reached)
283 if (size > mmf->data_size)
284 size = mmf->data_size;
289 if (av_new_packet(pkt, size))
291 pkt->stream_index = 0;
293 ret = avio_read(s->pb, pkt->data, pkt->size);
295 av_packet_unref(pkt);
297 mmf->data_size -= ret;
303 #if CONFIG_MMF_DEMUXER
304 AVInputFormat ff_mmf_demuxer = {
306 .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
307 .priv_data_size = sizeof(MMFContext),
308 .read_probe = mmf_probe,
309 .read_header = mmf_read_header,
310 .read_packet = mmf_read_packet,
311 .read_seek = ff_pcm_read_seek,
316 AVOutputFormat ff_mmf_muxer = {
318 .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
319 .mime_type = "application/vnd.smaf",
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,