]> git.sesse.net Git - ffmpeg/blob - libavformat/au.c
avcodec/mjpegdec: fix SOF check in EOI
[ffmpeg] / libavformat / au.c
1 /*
2  * AU muxer and demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * first version by Francois Revol <revol@free.fr>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /*
25  * Reference documents:
26  * http://www.opengroup.org/public/pubs/external/auformat.html
27  * http://www.goice.co.jp/member/mo/formats/au.html
28  */
29
30 #include "avformat.h"
31 #include "internal.h"
32 #include "avio_internal.h"
33 #include "pcm.h"
34 #include "libavutil/avassert.h"
35
36 /* if we don't know the size in advance */
37 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
38
39 static const AVCodecTag codec_au_tags[] = {
40     { AV_CODEC_ID_PCM_MULAW,  1 },
41     { AV_CODEC_ID_PCM_S8,     2 },
42     { AV_CODEC_ID_PCM_S16BE,  3 },
43     { AV_CODEC_ID_PCM_S24BE,  4 },
44     { AV_CODEC_ID_PCM_S32BE,  5 },
45     { AV_CODEC_ID_PCM_F32BE,  6 },
46     { AV_CODEC_ID_PCM_F64BE,  7 },
47     { AV_CODEC_ID_ADPCM_G726LE, 23 },
48     { AV_CODEC_ID_ADPCM_G722,24 },
49     { AV_CODEC_ID_ADPCM_G726LE, 25 },
50     { AV_CODEC_ID_ADPCM_G726LE, 26 },
51     { AV_CODEC_ID_PCM_ALAW,  27 },
52     { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
53     { AV_CODEC_ID_NONE,       0 },
54 };
55
56 static const AVCodecTag *const au_codec_tags[] = { codec_au_tags, NULL };
57
58 #if CONFIG_AU_DEMUXER
59
60 static int au_probe(const AVProbeData *p)
61 {
62     if (p->buf[0] == '.' && p->buf[1] == 's' &&
63         p->buf[2] == 'n' && p->buf[3] == 'd')
64         return AVPROBE_SCORE_MAX;
65     else
66         return 0;
67 }
68
69 static int au_read_annotation(AVFormatContext *s, int size)
70 {
71     static const char keys[][7] = {
72         "title",
73         "artist",
74         "album",
75         "track",
76         "genre",
77     };
78     AVIOContext *pb = s->pb;
79     enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
80     char c;
81     AVBPrint bprint;
82     char * key = NULL;
83     char * value = NULL;
84     int ret, i;
85
86     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
87
88     while (size-- > 0) {
89         if (avio_feof(pb)) {
90             av_bprint_finalize(&bprint, NULL);
91             av_freep(&key);
92             return AVERROR_EOF;
93         }
94         c = avio_r8(pb);
95         switch(state) {
96         case PARSE_KEY:
97             if (c == '\0') {
98                 state = PARSE_FINISHED;
99             } else if (c == '=') {
100                 ret = av_bprint_finalize(&bprint, &key);
101                 if (ret < 0)
102                     return ret;
103                 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
104                 state = PARSE_VALUE;
105             } else {
106                 av_bprint_chars(&bprint, c, 1);
107             }
108             break;
109         case PARSE_VALUE:
110             if (c == '\0' || c == '\n') {
111                 if (av_bprint_finalize(&bprint, &value) != 0) {
112                     av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
113                 } else {
114                     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
115                     for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
116                         if (av_strcasecmp(keys[i], key) == 0) {
117                             av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
118                             value = NULL;
119                             break;
120                         }
121                     }
122                 }
123                 av_freep(&key);
124                 av_freep(&value);
125                 state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
126             } else {
127                 av_bprint_chars(&bprint, c, 1);
128             }
129             break;
130         case PARSE_FINISHED:
131             break;
132         default:
133             /* should never happen */
134             av_assert0(0);
135         }
136     }
137     av_bprint_finalize(&bprint, NULL);
138     av_freep(&key);
139     return 0;
140 }
141
142 #define BLOCK_SIZE 1024
143
144 static int au_read_header(AVFormatContext *s)
145 {
146     int size, data_size = 0;
147     unsigned int tag;
148     AVIOContext *pb = s->pb;
149     unsigned int id, channels, rate;
150     int bps, ba = 0;
151     enum AVCodecID codec;
152     AVStream *st;
153     int ret;
154
155     tag = avio_rl32(pb);
156     if (tag != MKTAG('.', 's', 'n', 'd'))
157         return AVERROR_INVALIDDATA;
158     size = avio_rb32(pb); /* header size */
159     data_size = avio_rb32(pb); /* data size in bytes */
160
161     if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
162         av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
163         return AVERROR_INVALIDDATA;
164     }
165
166     id       = avio_rb32(pb);
167     rate     = avio_rb32(pb);
168     channels = avio_rb32(pb);
169
170     if (size > 24) {
171         /* parse annotation field to get metadata */
172         ret = au_read_annotation(s, size - 24);
173         if (ret < 0)
174             return ret;
175     }
176
177     codec = ff_codec_get_id(codec_au_tags, id);
178
179     if (codec == AV_CODEC_ID_NONE) {
180         avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
181         return AVERROR_PATCHWELCOME;
182     }
183
184     bps = av_get_bits_per_sample(codec);
185     if (codec == AV_CODEC_ID_ADPCM_G726LE) {
186         if (id == MKBETAG('7','2','6','2')) {
187             bps = 2;
188         } else {
189             const uint8_t bpcss[] = {4, 0, 3, 5};
190             av_assert0(id >= 23 && id < 23 + 4);
191             ba = bpcss[id - 23];
192             bps = bpcss[id - 23];
193         }
194     } else if (!bps) {
195         avpriv_request_sample(s, "Unknown bits per sample");
196         return AVERROR_PATCHWELCOME;
197     }
198
199     if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
200         av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
201         return AVERROR_INVALIDDATA;
202     }
203
204     if (rate == 0 || rate > INT_MAX) {
205         av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
206         return AVERROR_INVALIDDATA;
207     }
208
209     st = avformat_new_stream(s, NULL);
210     if (!st)
211         return AVERROR(ENOMEM);
212     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
213     st->codecpar->codec_tag   = id;
214     st->codecpar->codec_id    = codec;
215     st->codecpar->channels    = channels;
216     st->codecpar->sample_rate = rate;
217     st->codecpar->bits_per_coded_sample = bps;
218     st->codecpar->bit_rate    = channels * rate * bps;
219     st->codecpar->block_align = ba ? ba : FFMAX(bps * st->codecpar->channels / 8, 1);
220     if (data_size != AU_UNKNOWN_SIZE)
221         st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
222
223     st->start_time = 0;
224     avpriv_set_pts_info(st, 64, 1, rate);
225
226     return 0;
227 }
228
229 const AVInputFormat ff_au_demuxer = {
230     .name        = "au",
231     .long_name   = NULL_IF_CONFIG_SMALL("Sun AU"),
232     .read_probe  = au_probe,
233     .read_header = au_read_header,
234     .read_packet = ff_pcm_read_packet,
235     .read_seek   = ff_pcm_read_seek,
236     .codec_tag   = au_codec_tags,
237 };
238
239 #endif /* CONFIG_AU_DEMUXER */
240
241 #if CONFIG_AU_MUXER
242
243 typedef struct AUContext {
244     uint32_t header_size;
245 } AUContext;
246
247 #include "rawenc.h"
248
249 static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations)
250 {
251     static const char keys[][7] = {
252         "Title",
253         "Artist",
254         "Album",
255         "Track",
256         "Genre",
257     };
258     int cnt = 0;
259     AVDictionary *m = s->metadata;
260     AVDictionaryEntry *t = NULL;
261
262     for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
263         t = av_dict_get(m, keys[i], NULL, 0);
264         if (t != NULL) {
265             if (cnt++)
266                 av_bprint_chars(annotations, '\n', 1);
267             av_bprintf(annotations, "%s=%s", keys[i], t->value);
268         }
269     }
270     /* The specification requires the annotation field to be zero-terminated
271      * and its length to be a multiple of eight, so pad with 0's */
272     av_bprint_chars(annotations, '\0', 8);
273     return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM);
274 }
275
276 static int au_write_header(AVFormatContext *s)
277 {
278     int ret;
279     AUContext *au = s->priv_data;
280     AVIOContext *pb = s->pb;
281     AVCodecParameters *par = s->streams[0]->codecpar;
282     AVBPrint annotations;
283
284     if (s->nb_streams != 1) {
285         av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
286         return AVERROR(EINVAL);
287     }
288
289     par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
290     if (!par->codec_tag) {
291         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
292         return AVERROR(EINVAL);
293     }
294
295     av_bprint_init(&annotations, 0, INT_MAX - 24);
296     ret = au_get_annotations(s, &annotations);
297     if (ret < 0)
298         goto fail;
299     au->header_size = 24 + annotations.len & ~7;
300
301     ffio_wfourcc(pb, ".snd");                   /* magic number */
302     avio_wb32(pb, au->header_size);             /* header size */
303     avio_wb32(pb, AU_UNKNOWN_SIZE);             /* data size */
304     avio_wb32(pb, par->codec_tag);              /* codec ID */
305     avio_wb32(pb, par->sample_rate);
306     avio_wb32(pb, par->channels);
307     avio_write(pb, annotations.str, annotations.len & ~7);
308
309 fail:
310     av_bprint_finalize(&annotations, NULL);
311
312     return ret;
313 }
314
315 static int au_write_trailer(AVFormatContext *s)
316 {
317     AVIOContext *pb = s->pb;
318     AUContext *au = s->priv_data;
319     int64_t file_size = avio_tell(pb);
320
321     if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
322         /* update file size */
323         avio_seek(pb, 8, SEEK_SET);
324         avio_wb32(pb, (uint32_t)(file_size - au->header_size));
325         avio_seek(pb, file_size, SEEK_SET);
326     }
327
328     return 0;
329 }
330
331 const AVOutputFormat ff_au_muxer = {
332     .name          = "au",
333     .long_name     = NULL_IF_CONFIG_SMALL("Sun AU"),
334     .mime_type     = "audio/basic",
335     .extensions    = "au",
336     .priv_data_size = sizeof(AUContext),
337     .audio_codec   = AV_CODEC_ID_PCM_S16BE,
338     .video_codec   = AV_CODEC_ID_NONE,
339     .write_header  = au_write_header,
340     .write_packet  = ff_raw_write_packet,
341     .write_trailer = au_write_trailer,
342     .codec_tag     = au_codec_tags,
343     .flags         = AVFMT_NOTIMESTAMPS,
344 };
345
346 #endif /* CONFIG_AU_MUXER */