3 * Copyright (c) 2005 Vidar Madsen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 offset_t atrpos, atsqpos, awapos;
27 static int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
29 static int mmf_rate_code(int rate)
32 for(i = 0; i < 5; i++)
33 if(mmf_rates[i] == rate)
38 static int mmf_rate(int code)
40 if((code < 0) || (code > 4))
42 return mmf_rates[code];
46 /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
47 static void end_tag_be(ByteIOContext *pb, offset_t start)
52 url_fseek(pb, start - 4, SEEK_SET);
53 put_be32(pb, (uint32_t)(pos - start));
54 url_fseek(pb, pos, SEEK_SET);
57 static int mmf_write_header(AVFormatContext *s)
59 MMFContext *mmf = s->priv_data;
60 ByteIOContext *pb = &s->pb;
64 rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
66 av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
72 pos = start_tag(pb, "CNTI");
73 put_byte(pb, 0); /* class */
74 put_byte(pb, 0); /* type */
75 put_byte(pb, 0); /* code type */
76 put_byte(pb, 0); /* status */
77 put_byte(pb, 0); /* counts */
78 put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
81 put_buffer(pb, "ATR\x00", 4);
83 mmf->atrpos = url_ftell(pb);
84 put_byte(pb, 0); /* format type */
85 put_byte(pb, 0); /* sequence type */
86 put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
87 put_byte(pb, 0); /* wave base bit */
88 put_byte(pb, 2); /* time base d */
89 put_byte(pb, 2); /* time base g */
93 mmf->atsqpos = url_ftell(pb);
94 /* Will be filled on close */
95 put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
97 mmf->awapos = start_tag(pb, "Awa\x01");
99 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
101 put_flush_packet(pb);
106 static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
108 ByteIOContext *pb = &s->pb;
109 put_buffer(pb, pkt->data, pkt->size);
113 /* Write a variable-length symbol */
114 static void put_varlength(ByteIOContext *pb, int val)
120 put_byte(pb, 0x80 | val >> 7);
121 put_byte(pb, 0x7f & val);
125 static int mmf_write_trailer(AVFormatContext *s)
127 ByteIOContext *pb = &s->pb;
128 MMFContext *mmf = s->priv_data;
132 if (!url_is_streamed(&s->pb)) {
133 /* Fill in length fields */
134 end_tag_be(pb, mmf->awapos);
135 end_tag_be(pb, mmf->atrpos);
139 size = pos - mmf->awapos;
141 /* Fill Atsq chunk */
142 url_fseek(pb, mmf->atsqpos, SEEK_SET);
145 put_byte(pb, 0); /* start time */
146 put_byte(pb, 1); /* (channel << 6) | wavenum */
147 gatetime = size * 500 / s->streams[0]->codec->sample_rate;
148 put_varlength(pb, gatetime); /* duration */
151 put_varlength(pb, gatetime); /* start time */
152 put_buffer(pb, "\xff\x00", 2); /* nop */
154 /* "end of sequence" */
155 put_buffer(pb, "\x00\x00\x00\x00", 4);
157 url_fseek(pb, pos, SEEK_SET);
159 put_flush_packet(pb);
163 #endif //CONFIG_MUXERS
165 static int mmf_probe(AVProbeData *p)
167 /* check file header */
168 if (p->buf_size <= 32)
170 if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
171 p->buf[2] == 'M' && p->buf[3] == 'D' &&
172 p->buf[8] == 'C' && p->buf[9] == 'N' &&
173 p->buf[10] == 'T' && p->buf[11] == 'I')
174 return AVPROBE_SCORE_MAX;
180 static int mmf_read_header(AVFormatContext *s,
181 AVFormatParameters *ap)
183 MMFContext *mmf = s->priv_data;
185 ByteIOContext *pb = &s->pb;
187 offset_t file_size, size;
191 if (tag != MKTAG('M', 'M', 'M', 'D'))
193 file_size = get_be32(pb);
195 /* Skip some unused chunks that may or may not be present */
196 for(;; url_fseek(pb, size, SEEK_CUR)) {
199 if(tag == MKTAG('C','N','T','I')) continue;
200 if(tag == MKTAG('O','P','D','A')) continue;
204 /* Tag = "ATRx", where "x" = track number */
205 if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
206 av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
209 if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
210 av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
214 get_byte(pb); /* format type */
215 get_byte(pb); /* sequence type */
216 params = get_byte(pb); /* (channel << 7) | (format << 4) | rate */
217 rate = mmf_rate(params & 0x0f);
219 av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
222 get_byte(pb); /* wave base bit */
223 get_byte(pb); /* time base d */
224 get_byte(pb); /* time base g */
226 /* Skip some unused chunks that may or may not be present */
227 for(;; url_fseek(pb, size, SEEK_CUR)) {
230 if(tag == MKTAG('A','t','s','q')) continue;
231 if(tag == MKTAG('A','s','p','I')) continue;
235 /* Make sure it's followed by an Awa chunk, aka wave data */
236 if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
237 av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
240 mmf->data_size = size;
242 st = av_new_stream(s, 0);
244 return AVERROR_NOMEM;
246 st->codec->codec_type = CODEC_TYPE_AUDIO;
247 st->codec->codec_id = CODEC_ID_ADPCM_YAMAHA;
248 st->codec->sample_rate = rate;
249 st->codec->channels = 1;
250 st->codec->bits_per_sample = 4;
251 st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample;
253 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
258 #define MAX_SIZE 4096
260 static int mmf_read_packet(AVFormatContext *s,
263 MMFContext *mmf = s->priv_data;
267 if (url_feof(&s->pb))
272 if(size > mmf->data_size)
273 size = mmf->data_size;
278 if (av_new_packet(pkt, size))
280 pkt->stream_index = 0;
282 ret = get_buffer(&s->pb, pkt->data, pkt->size);
286 mmf->data_size -= ret;
292 static int mmf_read_close(AVFormatContext *s)
297 static int mmf_read_seek(AVFormatContext *s,
298 int stream_index, int64_t timestamp, int flags)
300 return pcm_read_seek(s, stream_index, timestamp, flags);
304 static AVInputFormat mmf_iformat = {
316 static AVOutputFormat mmf_oformat = {
319 "application/vnd.smaf",
322 CODEC_ID_ADPCM_YAMAHA,
328 #endif //CONFIG_MUXERS
330 int ff_mmf_init(void)
332 av_register_input_format(&mmf_iformat);
334 av_register_output_format(&mmf_oformat);
335 #endif //CONFIG_MUXERS