]> git.sesse.net Git - ffmpeg/blob - libavformat/au.c
avcodec/dvbsubdec: prefer to use variable instead of type for sizeof
[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 /* the specification requires an annotation field of at least eight bytes */
39 #define AU_DEFAULT_HEADER_SIZE (24+8)
40
41 static const AVCodecTag codec_au_tags[] = {
42     { AV_CODEC_ID_PCM_MULAW,  1 },
43     { AV_CODEC_ID_PCM_S8,     2 },
44     { AV_CODEC_ID_PCM_S16BE,  3 },
45     { AV_CODEC_ID_PCM_S24BE,  4 },
46     { AV_CODEC_ID_PCM_S32BE,  5 },
47     { AV_CODEC_ID_PCM_F32BE,  6 },
48     { AV_CODEC_ID_PCM_F64BE,  7 },
49     { AV_CODEC_ID_ADPCM_G726LE, 23 },
50     { AV_CODEC_ID_ADPCM_G722,24 },
51     { AV_CODEC_ID_ADPCM_G726LE, 25 },
52     { AV_CODEC_ID_ADPCM_G726LE, 26 },
53     { AV_CODEC_ID_PCM_ALAW,  27 },
54     { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
55     { AV_CODEC_ID_NONE,       0 },
56 };
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[] = {
72         "title",
73         "artist",
74         "album",
75         "track",
76         "genre",
77         NULL };
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         c = avio_r8(pb);
90         switch(state) {
91         case PARSE_KEY:
92             if (c == '\0') {
93                 state = PARSE_FINISHED;
94             } else if (c == '=') {
95                 ret = av_bprint_finalize(&bprint, &key);
96                 if (ret < 0)
97                     return ret;
98                 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
99                 state = PARSE_VALUE;
100             } else {
101                 av_bprint_chars(&bprint, c, 1);
102             }
103             break;
104         case PARSE_VALUE:
105             if (c == '\0' || c == '\n') {
106                 if (av_bprint_finalize(&bprint, &value) != 0) {
107                     av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
108                 } else {
109                     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
110                     for (i = 0; keys[i] != NULL && key != NULL; i++) {
111                         if (av_strcasecmp(keys[i], key) == 0) {
112                             av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
113                             av_freep(&key);
114                             value = NULL;
115                         }
116                     }
117                 }
118                 av_freep(&key);
119                 av_freep(&value);
120                 state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
121             } else {
122                 av_bprint_chars(&bprint, c, 1);
123             }
124             break;
125         case PARSE_FINISHED:
126             break;
127         default:
128             /* should never happen */
129             av_assert0(0);
130         }
131     }
132     av_bprint_finalize(&bprint, NULL);
133     av_freep(&key);
134     return 0;
135 }
136
137 #define BLOCK_SIZE 1024
138
139 static int au_read_header(AVFormatContext *s)
140 {
141     int size, data_size = 0;
142     unsigned int tag;
143     AVIOContext *pb = s->pb;
144     unsigned int id, channels, rate;
145     int bps, ba = 0;
146     enum AVCodecID codec;
147     AVStream *st;
148     int ret;
149
150     tag = avio_rl32(pb);
151     if (tag != MKTAG('.', 's', 'n', 'd'))
152         return AVERROR_INVALIDDATA;
153     size = avio_rb32(pb); /* header size */
154     data_size = avio_rb32(pb); /* data size in bytes */
155
156     if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
157         av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
158         return AVERROR_INVALIDDATA;
159     }
160
161     id       = avio_rb32(pb);
162     rate     = avio_rb32(pb);
163     channels = avio_rb32(pb);
164
165     if (size > 24) {
166         /* parse annotation field to get metadata */
167         ret = au_read_annotation(s, size - 24);
168         if (ret < 0)
169             return ret;
170     }
171
172     codec = ff_codec_get_id(codec_au_tags, id);
173
174     if (codec == AV_CODEC_ID_NONE) {
175         avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
176         return AVERROR_PATCHWELCOME;
177     }
178
179     bps = av_get_bits_per_sample(codec);
180     if (codec == AV_CODEC_ID_ADPCM_G726LE) {
181         if (id == MKBETAG('7','2','6','2')) {
182             bps = 2;
183         } else {
184             const uint8_t bpcss[] = {4, 0, 3, 5};
185             av_assert0(id >= 23 && id < 23 + 4);
186             ba = bpcss[id - 23];
187             bps = bpcss[id - 23];
188         }
189     } else if (!bps) {
190         avpriv_request_sample(s, "Unknown bits per sample");
191         return AVERROR_PATCHWELCOME;
192     }
193
194     if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
195         av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
196         return AVERROR_INVALIDDATA;
197     }
198
199     if (rate == 0 || rate > INT_MAX) {
200         av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
201         return AVERROR_INVALIDDATA;
202     }
203
204     st = avformat_new_stream(s, NULL);
205     if (!st)
206         return AVERROR(ENOMEM);
207     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
208     st->codecpar->codec_tag   = id;
209     st->codecpar->codec_id    = codec;
210     st->codecpar->channels    = channels;
211     st->codecpar->sample_rate = rate;
212     st->codecpar->bits_per_coded_sample = bps;
213     st->codecpar->bit_rate    = channels * rate * bps;
214     st->codecpar->block_align = ba ? ba : FFMAX(bps * st->codecpar->channels / 8, 1);
215     if (data_size != AU_UNKNOWN_SIZE)
216         st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
217
218     st->start_time = 0;
219     avpriv_set_pts_info(st, 64, 1, rate);
220
221     return 0;
222 }
223
224 AVInputFormat ff_au_demuxer = {
225     .name        = "au",
226     .long_name   = NULL_IF_CONFIG_SMALL("Sun AU"),
227     .read_probe  = au_probe,
228     .read_header = au_read_header,
229     .read_packet = ff_pcm_read_packet,
230     .read_seek   = ff_pcm_read_seek,
231     .codec_tag   = (const AVCodecTag* const []) { codec_au_tags, 0 },
232 };
233
234 #endif /* CONFIG_AU_DEMUXER */
235
236 #if CONFIG_AU_MUXER
237
238 typedef struct AUContext {
239     uint32_t header_size;
240 } AUContext;
241
242 #include "rawenc.h"
243
244 static int au_get_annotations(AVFormatContext *s, char **buffer)
245 {
246     static const char * keys[] = {
247         "Title",
248         "Artist",
249         "Album",
250         "Track",
251         "Genre",
252         NULL };
253     int i;
254     int cnt = 0;
255     AVDictionary *m = s->metadata;
256     AVDictionaryEntry *t = NULL;
257     AVBPrint bprint;
258
259     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
260
261     for (i = 0; keys[i] != NULL; i++) {
262         t = av_dict_get(m, keys[i], NULL, 0);
263         if (t != NULL) {
264             if (cnt++)
265                 av_bprint_chars(&bprint, '\n', 1);
266             av_bprint_append_data(&bprint, keys[i], strlen(keys[i]));
267             av_bprint_chars(&bprint, '=', 1);
268             av_bprint_append_data(&bprint, t->value, strlen(t->value));
269         }
270     }
271     /* pad with 0's */
272     av_bprint_append_data(&bprint, "\0\0\0\0\0\0\0\0", 8);
273     return av_bprint_finalize(&bprint, buffer);
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     char *annotations = NULL;
283
284     au->header_size = AU_DEFAULT_HEADER_SIZE;
285
286     if (s->nb_streams != 1) {
287         av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
288         return AVERROR(EINVAL);
289     }
290
291     par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
292     if (!par->codec_tag) {
293         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
294         return AVERROR(EINVAL);
295     }
296
297     if (av_dict_count(s->metadata) > 0) {
298         ret = au_get_annotations(s, &annotations);
299         if (ret < 0)
300             return ret;
301         if (annotations != NULL) {
302             au->header_size = (24 + strlen(annotations) + 8) & ~7;
303             if (au->header_size < AU_DEFAULT_HEADER_SIZE)
304                 au->header_size = AU_DEFAULT_HEADER_SIZE;
305         }
306     }
307     ffio_wfourcc(pb, ".snd");                   /* magic number */
308     avio_wb32(pb, au->header_size);             /* header size */
309     avio_wb32(pb, AU_UNKNOWN_SIZE);             /* data size */
310     avio_wb32(pb, par->codec_tag);              /* codec ID */
311     avio_wb32(pb, par->sample_rate);
312     avio_wb32(pb, par->channels);
313     if (annotations != NULL) {
314         avio_write(pb, annotations, au->header_size - 24);
315         av_freep(&annotations);
316     } else {
317         avio_wb64(pb, 0); /* annotation field */
318     }
319
320     return 0;
321 }
322
323 static int au_write_trailer(AVFormatContext *s)
324 {
325     AVIOContext *pb = s->pb;
326     AUContext *au = s->priv_data;
327     int64_t file_size = avio_tell(pb);
328
329     if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
330         /* update file size */
331         avio_seek(pb, 8, SEEK_SET);
332         avio_wb32(pb, (uint32_t)(file_size - au->header_size));
333         avio_seek(pb, file_size, SEEK_SET);
334     }
335
336     return 0;
337 }
338
339 AVOutputFormat ff_au_muxer = {
340     .name          = "au",
341     .long_name     = NULL_IF_CONFIG_SMALL("Sun AU"),
342     .mime_type     = "audio/basic",
343     .extensions    = "au",
344     .priv_data_size = sizeof(AUContext),
345     .audio_codec   = AV_CODEC_ID_PCM_S16BE,
346     .video_codec   = AV_CODEC_ID_NONE,
347     .write_header  = au_write_header,
348     .write_packet  = ff_raw_write_packet,
349     .write_trailer = au_write_trailer,
350     .codec_tag     = (const AVCodecTag* const []) { codec_au_tags, 0 },
351     .flags         = AVFMT_NOTIMESTAMPS,
352 };
353
354 #endif /* CONFIG_AU_MUXER */