]> git.sesse.net Git - ffmpeg/blob - libavformat/mmf.c
oggdec: free the ogg streams on read_header failure
[ffmpeg] / libavformat / mmf.c
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
22 #include "libavutil/channel_layout.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "avio_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", s->streams[0]->codec->sample_rate);
74         return -1;
75     }
76
77     ffio_wfourcc(pb, "MMMD");
78     avio_wb32(pb, 0);
79     pos = ff_start_tag(pb, "CNTI");
80     avio_w8(pb, 0); /* class */
81     avio_w8(pb, 0); /* type */
82     avio_w8(pb, 0); /* code type */
83     avio_w8(pb, 0); /* status */
84     avio_w8(pb, 0); /* counts */
85     avio_write(pb, "VN:libavcodec,", sizeof("VN:libavcodec,") -1); /* metadata ("ST:songtitle,VN:version,...") */
86     end_tag_be(pb, pos);
87
88     avio_write(pb, "ATR\x00", 4);
89     avio_wb32(pb, 0);
90     mmf->atrpos = avio_tell(pb);
91     avio_w8(pb, 0); /* format type */
92     avio_w8(pb, 0); /* sequence type */
93     avio_w8(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
94     avio_w8(pb, 0); /* wave base bit */
95     avio_w8(pb, 2); /* time base d */
96     avio_w8(pb, 2); /* time base g */
97
98     ffio_wfourcc(pb, "Atsq");
99     avio_wb32(pb, 16);
100     mmf->atsqpos = avio_tell(pb);
101     /* Will be filled on close */
102     avio_write(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
103
104     mmf->awapos = ff_start_tag(pb, "Awa\x01");
105
106     avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
107
108     avio_flush(pb);
109
110     return 0;
111 }
112
113 static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
114 {
115     AVIOContext *pb = s->pb;
116     avio_write(pb, pkt->data, pkt->size);
117     return 0;
118 }
119
120 /* Write a variable-length symbol */
121 static void put_varlength(AVIOContext *pb, int val)
122 {
123     if(val < 128)
124         avio_w8(pb, val);
125     else {
126         val -= 128;
127         avio_w8(pb, 0x80 | val >> 7);
128         avio_w8(pb, 0x7f & val);
129     }
130 }
131
132 static int mmf_write_trailer(AVFormatContext *s)
133 {
134     AVIOContext *pb = s->pb;
135     MMFContext *mmf = s->priv_data;
136     int64_t pos, size;
137     int gatetime;
138
139     if (s->pb->seekable) {
140         /* Fill in length fields */
141         end_tag_be(pb, mmf->awapos);
142         end_tag_be(pb, mmf->atrpos);
143         end_tag_be(pb, 8);
144
145         pos = avio_tell(pb);
146         size = pos - mmf->awapos;
147
148         /* Fill Atsq chunk */
149         avio_seek(pb, mmf->atsqpos, SEEK_SET);
150
151         /* "play wav" */
152         avio_w8(pb, 0); /* start time */
153         avio_w8(pb, 1); /* (channel << 6) | wavenum */
154         gatetime = size * 500 / s->streams[0]->codec->sample_rate;
155         put_varlength(pb, gatetime); /* duration */
156
157         /* "nop" */
158         put_varlength(pb, gatetime); /* start time */
159         avio_write(pb, "\xff\x00", 2); /* nop */
160
161         /* "end of sequence" */
162         avio_write(pb, "\x00\x00\x00\x00", 4);
163
164         avio_seek(pb, pos, SEEK_SET);
165
166         avio_flush(pb);
167     }
168     return 0;
169 }
170 #endif /* CONFIG_MMF_MUXER */
171
172 static int mmf_probe(AVProbeData *p)
173 {
174     /* check file header */
175     if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
176         p->buf[2] == 'M' && p->buf[3] == 'D' &&
177         p->buf[8] == 'C' && p->buf[9] == 'N' &&
178         p->buf[10] == 'T' && p->buf[11] == 'I')
179         return AVPROBE_SCORE_MAX;
180     else
181         return 0;
182 }
183
184 /* mmf input */
185 static int mmf_read_header(AVFormatContext *s)
186 {
187     MMFContext *mmf = s->priv_data;
188     unsigned int tag;
189     AVIOContext *pb = s->pb;
190     AVStream *st;
191     int64_t size;
192     int rate, params;
193
194     tag = avio_rl32(pb);
195     if (tag != MKTAG('M', 'M', 'M', 'D'))
196         return -1;
197     avio_skip(pb, 4); /* file_size */
198
199     /* Skip some unused chunks that may or may not be present */
200     for(;; avio_skip(pb, size)) {
201         tag = avio_rl32(pb);
202         size = avio_rb32(pb);
203         if(tag == MKTAG('C','N','T','I')) continue;
204         if(tag == MKTAG('O','P','D','A')) continue;
205         break;
206     }
207
208     /* Tag = "ATRx", where "x" = track number */
209     if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
210         av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
211         return -1;
212     }
213     if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
214         av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
215         return -1;
216     }
217
218     avio_r8(pb); /* format type */
219     avio_r8(pb); /* sequence type */
220     params = avio_r8(pb); /* (channel << 7) | (format << 4) | rate */
221     rate = mmf_rate(params & 0x0f);
222     if(rate  < 0) {
223         av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
224         return -1;
225     }
226     avio_r8(pb); /* wave base bit */
227     avio_r8(pb); /* time base d */
228     avio_r8(pb); /* time base g */
229
230     /* Skip some unused chunks that may or may not be present */
231     for(;; avio_skip(pb, size)) {
232         tag = avio_rl32(pb);
233         size = avio_rb32(pb);
234         if(tag == MKTAG('A','t','s','q')) continue;
235         if(tag == MKTAG('A','s','p','I')) continue;
236         break;
237     }
238
239     /* Make sure it's followed by an Awa chunk, aka wave data */
240     if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
241         av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
242         return -1;
243     }
244     mmf->data_size = size;
245
246     st = avformat_new_stream(s, NULL);
247     if (!st)
248         return AVERROR(ENOMEM);
249
250     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
251     st->codec->codec_id = AV_CODEC_ID_ADPCM_YAMAHA;
252     st->codec->sample_rate = rate;
253     st->codec->channels = 1;
254     st->codec->channel_layout = AV_CH_LAYOUT_MONO;
255     st->codec->bits_per_coded_sample = 4;
256     st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample;
257
258     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
259
260     return 0;
261 }
262
263 #define MAX_SIZE 4096
264
265 static int mmf_read_packet(AVFormatContext *s,
266                            AVPacket *pkt)
267 {
268     MMFContext *mmf = s->priv_data;
269     int ret, size;
270
271     if (s->pb->eof_reached)
272         return AVERROR(EIO);
273
274     size = MAX_SIZE;
275     if(size > mmf->data_size)
276         size = mmf->data_size;
277
278     if(!size)
279         return AVERROR(EIO);
280
281     if (av_new_packet(pkt, size))
282         return AVERROR(EIO);
283     pkt->stream_index = 0;
284
285     ret = avio_read(s->pb, pkt->data, pkt->size);
286     if (ret < 0)
287         av_free_packet(pkt);
288
289     mmf->data_size -= ret;
290
291     pkt->size = ret;
292     return ret;
293 }
294
295 #if CONFIG_MMF_DEMUXER
296 AVInputFormat ff_mmf_demuxer = {
297     .name           = "mmf",
298     .long_name      = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
299     .priv_data_size = sizeof(MMFContext),
300     .read_probe     = mmf_probe,
301     .read_header    = mmf_read_header,
302     .read_packet    = mmf_read_packet,
303     .read_seek      = ff_pcm_read_seek,
304 };
305 #endif
306 #if CONFIG_MMF_MUXER
307 AVOutputFormat ff_mmf_muxer = {
308     .name              = "mmf",
309     .long_name         = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
310     .mime_type         = "application/vnd.smaf",
311     .extensions        = "mmf",
312     .priv_data_size    = sizeof(MMFContext),
313     .audio_codec       = AV_CODEC_ID_ADPCM_YAMAHA,
314     .video_codec       = AV_CODEC_ID_NONE,
315     .write_header      = mmf_write_header,
316     .write_packet      = mmf_write_packet,
317     .write_trailer     = mmf_write_trailer,
318 };
319 #endif