Use more descriptive format long_names.
[libav.git] / libavformat / mmf.c
CommitLineData
115329f1 1/*
93a23627
VM
2 * Yamaha SMAF format
3 * Copyright (c) 2005 Vidar Madsen
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
93a23627
VM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
93a23627 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
93a23627
VM
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
93a23627
VM
20 */
21#include "avformat.h"
7241a6bb 22#include "raw.h"
9d9f4119 23#include "riff.h"
93a23627 24
93a23627 25typedef struct {
bc5c918e
DB
26 int64_t atrpos, atsqpos, awapos;
27 int64_t data_size;
93a23627
VM
28} MMFContext;
29
aecf157e 30static const int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
93a23627 31
37673b1b
DB
32static int mmf_rate(int code)
33{
34 if((code < 0) || (code > 4))
35 return -1;
36 return mmf_rates[code];
37}
38
b250f9c6 39#if CONFIG_MMF_MUXER
93a23627
VM
40static int mmf_rate_code(int rate)
41{
42 int i;
43 for(i = 0; i < 5; i++)
44 if(mmf_rates[i] == rate)
45 return i;
46 return -1;
47}
48
93a23627 49/* Copy of end_tag() from avienc.c, but for big-endian chunk size */
bc5c918e 50static void end_tag_be(ByteIOContext *pb, int64_t start)
93a23627 51{
bc5c918e 52 int64_t pos;
93a23627
VM
53
54 pos = url_ftell(pb);
55 url_fseek(pb, start - 4, SEEK_SET);
56 put_be32(pb, (uint32_t)(pos - start));
57 url_fseek(pb, pos, SEEK_SET);
58}
59
60static int mmf_write_header(AVFormatContext *s)
61{
62 MMFContext *mmf = s->priv_data;
899681cd 63 ByteIOContext *pb = s->pb;
bc5c918e 64 int64_t pos;
93a23627
VM
65 int rate;
66
01f4895c 67 rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
93a23627 68 if(rate < 0) {
01f4895c 69 av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
93a23627
VM
70 return -1;
71 }
115329f1 72
93a23627
VM
73 put_tag(pb, "MMMD");
74 put_be32(pb, 0);
75 pos = start_tag(pb, "CNTI");
76 put_byte(pb, 0); /* class */
77 put_byte(pb, 0); /* type */
78 put_byte(pb, 0); /* code type */
79 put_byte(pb, 0); /* status */
80 put_byte(pb, 0); /* counts */
81 put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
82 end_tag_be(pb, pos);
83
84 put_buffer(pb, "ATR\x00", 4);
85 put_be32(pb, 0);
86 mmf->atrpos = url_ftell(pb);
87 put_byte(pb, 0); /* format type */
88 put_byte(pb, 0); /* sequence type */
89 put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
90 put_byte(pb, 0); /* wave base bit */
91 put_byte(pb, 2); /* time base d */
92 put_byte(pb, 2); /* time base g */
93
94 put_tag(pb, "Atsq");
95 put_be32(pb, 16);
96 mmf->atsqpos = url_ftell(pb);
97 /* Will be filled on close */
98 put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
99
100 mmf->awapos = start_tag(pb, "Awa\x01");
101
01f4895c 102 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
93a23627
VM
103
104 put_flush_packet(pb);
105
106 return 0;
107}
108
109static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
110{
899681cd 111 ByteIOContext *pb = s->pb;
93a23627
VM
112 put_buffer(pb, pkt->data, pkt->size);
113 return 0;
114}
115
116/* Write a variable-length symbol */
117static void put_varlength(ByteIOContext *pb, int val)
118{
119 if(val < 128)
120 put_byte(pb, val);
121 else {
122 val -= 128;
123 put_byte(pb, 0x80 | val >> 7);
124 put_byte(pb, 0x7f & val);
125 }
126}
127
128static int mmf_write_trailer(AVFormatContext *s)
129{
899681cd 130 ByteIOContext *pb = s->pb;
93a23627 131 MMFContext *mmf = s->priv_data;
bc5c918e 132 int64_t pos, size;
93a23627
VM
133 int gatetime;
134
899681cd 135 if (!url_is_streamed(s->pb)) {
93a23627
VM
136 /* Fill in length fields */
137 end_tag_be(pb, mmf->awapos);
138 end_tag_be(pb, mmf->atrpos);
139 end_tag_be(pb, 8);
140
141 pos = url_ftell(pb);
142 size = pos - mmf->awapos;
143
144 /* Fill Atsq chunk */
145 url_fseek(pb, mmf->atsqpos, SEEK_SET);
146
147 /* "play wav" */
148 put_byte(pb, 0); /* start time */
149 put_byte(pb, 1); /* (channel << 6) | wavenum */
01f4895c 150 gatetime = size * 500 / s->streams[0]->codec->sample_rate;
93a23627
VM
151 put_varlength(pb, gatetime); /* duration */
152
153 /* "nop" */
154 put_varlength(pb, gatetime); /* start time */
155 put_buffer(pb, "\xff\x00", 2); /* nop */
156
157 /* "end of sequence" */
158 put_buffer(pb, "\x00\x00\x00\x00", 4);
159
160 url_fseek(pb, pos, SEEK_SET);
161
162 put_flush_packet(pb);
163 }
164 return 0;
165}
8212568a 166#endif /* CONFIG_MMF_MUXER */
93a23627
VM
167
168static int mmf_probe(AVProbeData *p)
169{
170 /* check file header */
93a23627
VM
171 if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
172 p->buf[2] == 'M' && p->buf[3] == 'D' &&
173 p->buf[8] == 'C' && p->buf[9] == 'N' &&
174 p->buf[10] == 'T' && p->buf[11] == 'I')
175 return AVPROBE_SCORE_MAX;
176 else
177 return 0;
178}
179
180/* mmf input */
181static int mmf_read_header(AVFormatContext *s,
182 AVFormatParameters *ap)
183{
184 MMFContext *mmf = s->priv_data;
185 unsigned int tag;
899681cd 186 ByteIOContext *pb = s->pb;
93a23627 187 AVStream *st;
bc5c918e 188 int64_t file_size, size;
93a23627
VM
189 int rate, params;
190
191 tag = get_le32(pb);
192 if (tag != MKTAG('M', 'M', 'M', 'D'))
193 return -1;
194 file_size = get_be32(pb);
195
196 /* Skip some unused chunks that may or may not be present */
197 for(;; url_fseek(pb, size, SEEK_CUR)) {
198 tag = get_le32(pb);
199 size = get_be32(pb);
200 if(tag == MKTAG('C','N','T','I')) continue;
201 if(tag == MKTAG('O','P','D','A')) continue;
202 break;
203 }
204
205 /* Tag = "ATRx", where "x" = track number */
c2ea5f06
AB
206 if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
207 av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
208 return -1;
209 }
93a23627
VM
210 if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
211 av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
212 return -1;
213 }
214
215 get_byte(pb); /* format type */
216 get_byte(pb); /* sequence type */
217 params = get_byte(pb); /* (channel << 7) | (format << 4) | rate */
218 rate = mmf_rate(params & 0x0f);
219 if(rate < 0) {
220 av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
221 return -1;
222 }
223 get_byte(pb); /* wave base bit */
224 get_byte(pb); /* time base d */
225 get_byte(pb); /* time base g */
226
227 /* Skip some unused chunks that may or may not be present */
228 for(;; url_fseek(pb, size, SEEK_CUR)) {
229 tag = get_le32(pb);
230 size = get_be32(pb);
231 if(tag == MKTAG('A','t','s','q')) continue;
232 if(tag == MKTAG('A','s','p','I')) continue;
233 break;
234 }
235
236 /* Make sure it's followed by an Awa chunk, aka wave data */
237 if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
238 av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
239 return -1;
240 }
241 mmf->data_size = size;
242
243 st = av_new_stream(s, 0);
244 if (!st)
769e10f0 245 return AVERROR(ENOMEM);
93a23627 246
01f4895c
MN
247 st->codec->codec_type = CODEC_TYPE_AUDIO;
248 st->codec->codec_id = CODEC_ID_ADPCM_YAMAHA;
249 st->codec->sample_rate = rate;
250 st->codec->channels = 1;
dd1c8f3e
LA
251 st->codec->bits_per_coded_sample = 4;
252 st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample;
93a23627 253
01f4895c 254 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
93a23627
VM
255
256 return 0;
257}
258
259#define MAX_SIZE 4096
260
261static int mmf_read_packet(AVFormatContext *s,
262 AVPacket *pkt)
263{
264 MMFContext *mmf = s->priv_data;
265 AVStream *st;
266 int ret, size;
267
899681cd 268 if (url_feof(s->pb))
6f3e0b21 269 return AVERROR(EIO);
93a23627
VM
270 st = s->streams[0];
271
272 size = MAX_SIZE;
273 if(size > mmf->data_size)
274 size = mmf->data_size;
275
276 if(!size)
6f3e0b21 277 return AVERROR(EIO);
115329f1 278
93a23627 279 if (av_new_packet(pkt, size))
6f3e0b21 280 return AVERROR(EIO);
93a23627
VM
281 pkt->stream_index = 0;
282
899681cd 283 ret = get_buffer(s->pb, pkt->data, pkt->size);
93a23627
VM
284 if (ret < 0)
285 av_free_packet(pkt);
286
287 mmf->data_size -= ret;
288
289 pkt->size = ret;
290 return ret;
291}
292
b250f9c6 293#if CONFIG_MMF_DEMUXER
ff70e601 294AVInputFormat mmf_demuxer = {
93a23627 295 "mmf",
517ac243 296 NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
93a23627
VM
297 sizeof(MMFContext),
298 mmf_probe,
299 mmf_read_header,
300 mmf_read_packet,
e7fd7b9a 301 NULL,
15d856e8 302 pcm_read_seek,
93a23627 303};
ff70e601 304#endif
b250f9c6 305#if CONFIG_MMF_MUXER
ff70e601 306AVOutputFormat mmf_muxer = {
93a23627 307 "mmf",
517ac243 308 NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
93a23627
VM
309 "application/vnd.smaf",
310 "mmf",
311 sizeof(MMFContext),
312 CODEC_ID_ADPCM_YAMAHA,
313 CODEC_ID_NONE,
314 mmf_write_header,
315 mmf_write_packet,
316 mmf_write_trailer,
317};
ff70e601 318#endif