1 | /*
|
---|
2 | * Yamaha SMAF format
|
---|
3 | * Copyright (c) 2005 Vidar Madsen
|
---|
4 | *
|
---|
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.
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
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
|
---|
18 | */
|
---|
19 | #include "avformat.h"
|
---|
20 | #include "allformats.h"
|
---|
21 | #include "avi.h"
|
---|
22 |
|
---|
23 | typedef struct {
|
---|
24 | offset_t atrpos, atsqpos, awapos;
|
---|
25 | offset_t data_size;
|
---|
26 | } MMFContext;
|
---|
27 |
|
---|
28 | static int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
|
---|
29 |
|
---|
30 | static int mmf_rate_code(int rate)
|
---|
31 | {
|
---|
32 | int i;
|
---|
33 | for(i = 0; i < 5; i++)
|
---|
34 | if(mmf_rates[i] == rate)
|
---|
35 | return i;
|
---|
36 | return -1;
|
---|
37 | }
|
---|
38 |
|
---|
39 | static int mmf_rate(int code)
|
---|
40 | {
|
---|
41 | if((code < 0) || (code > 4))
|
---|
42 | return -1;
|
---|
43 | return mmf_rates[code];
|
---|
44 | }
|
---|
45 |
|
---|
46 | #ifdef CONFIG_MUXERS
|
---|
47 | /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
|
---|
48 | static void end_tag_be(ByteIOContext *pb, offset_t start)
|
---|
49 | {
|
---|
50 | offset_t pos;
|
---|
51 |
|
---|
52 | pos = url_ftell(pb);
|
---|
53 | url_fseek(pb, start - 4, SEEK_SET);
|
---|
54 | put_be32(pb, (uint32_t)(pos - start));
|
---|
55 | url_fseek(pb, pos, SEEK_SET);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static int mmf_write_header(AVFormatContext *s)
|
---|
59 | {
|
---|
60 | MMFContext *mmf = s->priv_data;
|
---|
61 | ByteIOContext *pb = &s->pb;
|
---|
62 | offset_t pos;
|
---|
63 | int rate;
|
---|
64 |
|
---|
65 | rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
|
---|
66 | if(rate < 0) {
|
---|
67 | av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
|
---|
68 | return -1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | put_tag(pb, "MMMD");
|
---|
72 | put_be32(pb, 0);
|
---|
73 | pos = start_tag(pb, "CNTI");
|
---|
74 | put_byte(pb, 0); /* class */
|
---|
75 | put_byte(pb, 0); /* type */
|
---|
76 | put_byte(pb, 0); /* code type */
|
---|
77 | put_byte(pb, 0); /* status */
|
---|
78 | put_byte(pb, 0); /* counts */
|
---|
79 | put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
|
---|
80 | end_tag_be(pb, pos);
|
---|
81 |
|
---|
82 | put_buffer(pb, "ATR\x00", 4);
|
---|
83 | put_be32(pb, 0);
|
---|
84 | mmf->atrpos = url_ftell(pb);
|
---|
85 | put_byte(pb, 0); /* format type */
|
---|
86 | put_byte(pb, 0); /* sequence type */
|
---|
87 | put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
|
---|
88 | put_byte(pb, 0); /* wave base bit */
|
---|
89 | put_byte(pb, 2); /* time base d */
|
---|
90 | put_byte(pb, 2); /* time base g */
|
---|
91 |
|
---|
92 | put_tag(pb, "Atsq");
|
---|
93 | put_be32(pb, 16);
|
---|
94 | mmf->atsqpos = url_ftell(pb);
|
---|
95 | /* Will be filled on close */
|
---|
96 | put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
|
---|
97 |
|
---|
98 | mmf->awapos = start_tag(pb, "Awa\x01");
|
---|
99 |
|
---|
100 | av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
|
---|
101 |
|
---|
102 | put_flush_packet(pb);
|
---|
103 |
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
108 | {
|
---|
109 | ByteIOContext *pb = &s->pb;
|
---|
110 | put_buffer(pb, pkt->data, pkt->size);
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Write a variable-length symbol */
|
---|
115 | static void put_varlength(ByteIOContext *pb, int val)
|
---|
116 | {
|
---|
117 | if(val < 128)
|
---|
118 | put_byte(pb, val);
|
---|
119 | else {
|
---|
120 | val -= 128;
|
---|
121 | put_byte(pb, 0x80 | val >> 7);
|
---|
122 | put_byte(pb, 0x7f & val);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | static int mmf_write_trailer(AVFormatContext *s)
|
---|
127 | {
|
---|
128 | ByteIOContext *pb = &s->pb;
|
---|
129 | MMFContext *mmf = s->priv_data;
|
---|
130 | offset_t pos, size;
|
---|
131 | int gatetime;
|
---|
132 |
|
---|
133 | if (!url_is_streamed(&s->pb)) {
|
---|
134 | /* Fill in length fields */
|
---|
135 | end_tag_be(pb, mmf->awapos);
|
---|
136 | end_tag_be(pb, mmf->atrpos);
|
---|
137 | end_tag_be(pb, 8);
|
---|
138 |
|
---|
139 | pos = url_ftell(pb);
|
---|
140 | size = pos - mmf->awapos;
|
---|
141 |
|
---|
142 | /* Fill Atsq chunk */
|
---|
143 | url_fseek(pb, mmf->atsqpos, SEEK_SET);
|
---|
144 |
|
---|
145 | /* "play wav" */
|
---|
146 | put_byte(pb, 0); /* start time */
|
---|
147 | put_byte(pb, 1); /* (channel << 6) | wavenum */
|
---|
148 | gatetime = size * 500 / s->streams[0]->codec->sample_rate;
|
---|
149 | put_varlength(pb, gatetime); /* duration */
|
---|
150 |
|
---|
151 | /* "nop" */
|
---|
152 | put_varlength(pb, gatetime); /* start time */
|
---|
153 | put_buffer(pb, "\xff\x00", 2); /* nop */
|
---|
154 |
|
---|
155 | /* "end of sequence" */
|
---|
156 | put_buffer(pb, "\x00\x00\x00\x00", 4);
|
---|
157 |
|
---|
158 | url_fseek(pb, pos, SEEK_SET);
|
---|
159 |
|
---|
160 | put_flush_packet(pb);
|
---|
161 | }
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 | #endif //CONFIG_MUXERS
|
---|
165 |
|
---|
166 | static int mmf_probe(AVProbeData *p)
|
---|
167 | {
|
---|
168 | /* check file header */
|
---|
169 | if (p->buf_size <= 32)
|
---|
170 | return 0;
|
---|
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 */
|
---|
181 | static int mmf_read_header(AVFormatContext *s,
|
---|
182 | AVFormatParameters *ap)
|
---|
183 | {
|
---|
184 | MMFContext *mmf = s->priv_data;
|
---|
185 | unsigned int tag;
|
---|
186 | ByteIOContext *pb = &s->pb;
|
---|
187 | AVStream *st;
|
---|
188 | offset_t file_size, size;
|
---|
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 */
|
---|
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 | }
|
---|
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)
|
---|
245 | return AVERROR_NOMEM;
|
---|
246 |
|
---|
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;
|
---|
251 | st->codec->bits_per_sample = 4;
|
---|
252 | st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample;
|
---|
253 |
|
---|
254 | av_set_pts_info(st, 64, 1, st->codec->sample_rate);
|
---|
255 |
|
---|
256 | return 0;
|
---|
257 | }
|
---|
258 |
|
---|
259 | #define MAX_SIZE 4096
|
---|
260 |
|
---|
261 | static int mmf_read_packet(AVFormatContext *s,
|
---|
262 | AVPacket *pkt)
|
---|
263 | {
|
---|
264 | MMFContext *mmf = s->priv_data;
|
---|
265 | AVStream *st;
|
---|
266 | int ret, size;
|
---|
267 |
|
---|
268 | if (url_feof(&s->pb))
|
---|
269 | return AVERROR_IO;
|
---|
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)
|
---|
277 | return AVERROR_IO;
|
---|
278 |
|
---|
279 | if (av_new_packet(pkt, size))
|
---|
280 | return AVERROR_IO;
|
---|
281 | pkt->stream_index = 0;
|
---|
282 |
|
---|
283 | ret = get_buffer(&s->pb, pkt->data, pkt->size);
|
---|
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 |
|
---|
293 | static int mmf_read_close(AVFormatContext *s)
|
---|
294 | {
|
---|
295 | return 0;
|
---|
296 | }
|
---|
297 |
|
---|
298 | static int mmf_read_seek(AVFormatContext *s,
|
---|
299 | int stream_index, int64_t timestamp, int flags)
|
---|
300 | {
|
---|
301 | return pcm_read_seek(s, stream_index, timestamp, flags);
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | static AVInputFormat mmf_demuxer = {
|
---|
306 | "mmf",
|
---|
307 | "mmf format",
|
---|
308 | sizeof(MMFContext),
|
---|
309 | mmf_probe,
|
---|
310 | mmf_read_header,
|
---|
311 | mmf_read_packet,
|
---|
312 | mmf_read_close,
|
---|
313 | mmf_read_seek,
|
---|
314 | };
|
---|
315 |
|
---|
316 | #ifdef CONFIG_MUXERS
|
---|
317 | static AVOutputFormat mmf_muxer = {
|
---|
318 | "mmf",
|
---|
319 | "mmf format",
|
---|
320 | "application/vnd.smaf",
|
---|
321 | "mmf",
|
---|
322 | sizeof(MMFContext),
|
---|
323 | CODEC_ID_ADPCM_YAMAHA,
|
---|
324 | CODEC_ID_NONE,
|
---|
325 | mmf_write_header,
|
---|
326 | mmf_write_packet,
|
---|
327 | mmf_write_trailer,
|
---|
328 | };
|
---|
329 | #endif //CONFIG_MUXERS
|
---|
330 |
|
---|
331 | int ff_mmf_init(void)
|
---|
332 | {
|
---|
333 | av_register_input_format(&mmf_demuxer);
|
---|
334 | #ifdef CONFIG_MUXERS
|
---|
335 | av_register_output_format(&mmf_muxer);
|
---|
336 | #endif //CONFIG_MUXERS
|
---|
337 | return 0;
|
---|
338 | }
|
---|
339 |
|
---|