1 | /*
|
---|
2 | * AIFF/AIFF-C encoder and decoder
|
---|
3 | * Copyright (c) 2006 Patrick Guimond
|
---|
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 | #include "intfloat_readwrite.h"
|
---|
23 |
|
---|
24 | const CodecTag codec_aiff_tags[] = {
|
---|
25 | { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
|
---|
26 | { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
|
---|
27 | { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
|
---|
28 | { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
|
---|
29 | { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
|
---|
30 | { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
|
---|
31 | { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
|
---|
32 | { CODEC_ID_PCM_MULAW, MKTAG('U','L','A','W') },
|
---|
33 | { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
|
---|
34 | { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
|
---|
35 | { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
|
---|
36 | { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
|
---|
37 | { 0, 0 },
|
---|
38 | };
|
---|
39 |
|
---|
40 | #define AIFF 0
|
---|
41 | #define AIFF_C_VERSION1 0xA2805140
|
---|
42 |
|
---|
43 | static int aiff_codec_get_id (int bps)
|
---|
44 | {
|
---|
45 | if (bps <= 8)
|
---|
46 | return CODEC_ID_PCM_S8;
|
---|
47 | if (bps <= 16)
|
---|
48 | return CODEC_ID_PCM_S16BE;
|
---|
49 | if (bps <= 24)
|
---|
50 | return CODEC_ID_PCM_S24BE;
|
---|
51 | if (bps <= 32)
|
---|
52 | return CODEC_ID_PCM_S32BE;
|
---|
53 |
|
---|
54 | /* bigger than 32 isn't allowed */
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /* returns the size of the found tag */
|
---|
59 | static int get_tag(ByteIOContext *pb, uint32_t * tag)
|
---|
60 | {
|
---|
61 | int size;
|
---|
62 |
|
---|
63 | if (url_feof(pb))
|
---|
64 | return AVERROR_IO;
|
---|
65 |
|
---|
66 | *tag = get_le32(pb);
|
---|
67 | size = get_be32(pb);
|
---|
68 |
|
---|
69 | if (size < 0)
|
---|
70 | size = 0x7fffffff;
|
---|
71 |
|
---|
72 | return size;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Metadata string read */
|
---|
76 | static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
|
---|
77 | {
|
---|
78 | int res;
|
---|
79 |
|
---|
80 | if (size > strsize-1)
|
---|
81 | res = get_buffer(pb, (uint8_t*)str, strsize-1);
|
---|
82 | else
|
---|
83 | res = get_buffer(pb, (uint8_t*)str, size);
|
---|
84 |
|
---|
85 | if (res < 0)
|
---|
86 | return;
|
---|
87 |
|
---|
88 | str[res] = 0;
|
---|
89 | if (size & 1)
|
---|
90 | size++;
|
---|
91 | size -= res;
|
---|
92 | if (size);
|
---|
93 | url_fskip(pb, size);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Returns the number of bits per second */
|
---|
97 | static int fix_bps(int codec_id)
|
---|
98 | {
|
---|
99 | switch (codec_id) {
|
---|
100 | case CODEC_ID_PCM_S8:
|
---|
101 | return 8;
|
---|
102 | case CODEC_ID_PCM_S16BE:
|
---|
103 | return 16;
|
---|
104 | case CODEC_ID_PCM_S24BE:
|
---|
105 | return 24;
|
---|
106 | case CODEC_ID_PCM_S32BE:
|
---|
107 | return 32;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return -1;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* Returns the number of sound data frames or negative on error */
|
---|
114 | static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
|
---|
115 | int size, unsigned version)
|
---|
116 | {
|
---|
117 | AVExtFloat ext;
|
---|
118 | double sample_rate;
|
---|
119 | unsigned int num_frames;
|
---|
120 |
|
---|
121 |
|
---|
122 | if (size & 1)
|
---|
123 | size++;
|
---|
124 |
|
---|
125 | codec->codec_type = CODEC_TYPE_AUDIO;
|
---|
126 | codec->channels = get_be16(pb);
|
---|
127 | num_frames = get_be32(pb);
|
---|
128 | codec->bits_per_sample = get_be16(pb);
|
---|
129 |
|
---|
130 | get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
|
---|
131 | sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
|
---|
132 | codec->sample_rate = sample_rate;
|
---|
133 | size -= 18;
|
---|
134 |
|
---|
135 | /* Got an AIFF-C? */
|
---|
136 | if (version == AIFF_C_VERSION1) {
|
---|
137 | codec->codec_tag = get_le32(pb);
|
---|
138 | codec->codec_id = codec_get_id (codec_aiff_tags, codec->codec_tag);
|
---|
139 |
|
---|
140 | if (codec->codec_id == CODEC_ID_PCM_S16BE) {
|
---|
141 | codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
|
---|
142 | codec->bits_per_sample = fix_bps(codec->codec_id);
|
---|
143 | }
|
---|
144 |
|
---|
145 | size -= 4;
|
---|
146 | } else {
|
---|
147 | /* Need the codec type */
|
---|
148 | codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
|
---|
149 | codec->bits_per_sample = fix_bps(codec->codec_id);
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (!codec->codec_id)
|
---|
153 | return AVERROR_INVALIDDATA;
|
---|
154 |
|
---|
155 | /* Block align needs to be computed in all cases, as the definition
|
---|
156 | * is specific to applications -> here we use the WAVE format definition */
|
---|
157 | codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
|
---|
158 |
|
---|
159 | codec->bit_rate = codec->sample_rate * codec->block_align;
|
---|
160 |
|
---|
161 | /* Chunk is over */
|
---|
162 | if (size)
|
---|
163 | url_fseek(pb, size, SEEK_CUR);
|
---|
164 |
|
---|
165 | return num_frames;
|
---|
166 | }
|
---|
167 |
|
---|
168 | #ifdef CONFIG_MUXERS
|
---|
169 | typedef struct {
|
---|
170 | offset_t form;
|
---|
171 | offset_t frames;
|
---|
172 | offset_t ssnd;
|
---|
173 | } AIFFOutputContext;
|
---|
174 |
|
---|
175 | static int aiff_write_header(AVFormatContext *s)
|
---|
176 | {
|
---|
177 | AIFFOutputContext *aiff = s->priv_data;
|
---|
178 | ByteIOContext *pb = &s->pb;
|
---|
179 | AVCodecContext *enc = s->streams[0]->codec;
|
---|
180 | AVExtFloat sample_rate;
|
---|
181 | int coder_len;
|
---|
182 |
|
---|
183 | /* First verify if format is ok */
|
---|
184 | enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
|
---|
185 | if (!enc->codec_tag) {
|
---|
186 | av_free(aiff);
|
---|
187 | return -1;
|
---|
188 | }
|
---|
189 |
|
---|
190 | coder_len = strlen(enc->codec->name);
|
---|
191 |
|
---|
192 | /* FORM AIFF header */
|
---|
193 | put_tag(pb, "FORM");
|
---|
194 | aiff->form = url_ftell(pb);
|
---|
195 | put_be32(pb, 0); /* file length */
|
---|
196 | put_tag(pb, "AIFC");
|
---|
197 |
|
---|
198 | /* Version chunk */
|
---|
199 | put_tag(pb, "FVER");
|
---|
200 | put_be32(pb, 4);
|
---|
201 | put_be32(pb, 0xA2805140);
|
---|
202 |
|
---|
203 | /* Common chunk */
|
---|
204 | put_tag(pb, "COMM");
|
---|
205 | if (coder_len & 1) /* Common chunk is of var size */
|
---|
206 | put_be32(pb, 23+coder_len);
|
---|
207 | else
|
---|
208 | put_be32(pb, 24+coder_len);
|
---|
209 | put_be16(pb, enc->channels); /* Number of channels */
|
---|
210 |
|
---|
211 | aiff->frames = url_ftell(pb);
|
---|
212 | put_be32(pb, 0); /* Number of frames */
|
---|
213 |
|
---|
214 | if (!enc->bits_per_sample)
|
---|
215 | enc->bits_per_sample = (enc->block_align<<3) / enc->channels;
|
---|
216 | put_be16(pb, enc->bits_per_sample); /* Sample size */
|
---|
217 |
|
---|
218 | sample_rate = av_dbl2ext((double)enc->sample_rate);
|
---|
219 | put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
|
---|
220 |
|
---|
221 | put_le32(pb, enc->codec_tag);
|
---|
222 | if (coder_len & 1) {
|
---|
223 | put_byte(pb, coder_len);
|
---|
224 | put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
|
---|
225 | } else {
|
---|
226 | put_byte(pb, coder_len+1);
|
---|
227 | put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
|
---|
228 | put_byte(pb, 0);
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* Sound data chunk */
|
---|
232 | put_tag(pb, "SSND");
|
---|
233 | aiff->ssnd = url_ftell(pb); /* Sound chunk size */
|
---|
234 | put_be32(pb, 0); /* Sound samples data size */
|
---|
235 | put_be32(pb, 0); /* Data offset */
|
---|
236 | put_be32(pb, 0); /* Block-size (block align) */
|
---|
237 |
|
---|
238 | av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
|
---|
239 |
|
---|
240 | /* Data is starting here */
|
---|
241 | put_flush_packet(pb);
|
---|
242 |
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
247 | {
|
---|
248 | ByteIOContext *pb = &s->pb;
|
---|
249 | put_buffer(pb, pkt->data, pkt->size);
|
---|
250 | return 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | static int aiff_write_trailer(AVFormatContext *s)
|
---|
254 | {
|
---|
255 | ByteIOContext *pb = &s->pb;
|
---|
256 | AIFFOutputContext *aiff = s->priv_data;
|
---|
257 | AVCodecContext *enc = s->streams[0]->codec;
|
---|
258 |
|
---|
259 | /* Chunks sizes must be even */
|
---|
260 | offset_t file_size, end_size;
|
---|
261 | end_size = file_size = url_ftell(pb);
|
---|
262 | if (file_size & 1) {
|
---|
263 | put_byte(pb, 0);
|
---|
264 | end_size++;
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (!url_is_streamed(&s->pb)) {
|
---|
268 | /* File length */
|
---|
269 | url_fseek(pb, aiff->form, SEEK_SET);
|
---|
270 | put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
|
---|
271 |
|
---|
272 | /* Number of sample frames */
|
---|
273 | url_fseek(pb, aiff->frames, SEEK_SET);
|
---|
274 | put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
|
---|
275 |
|
---|
276 | /* Sound Data chunk size */
|
---|
277 | url_fseek(pb, aiff->ssnd, SEEK_SET);
|
---|
278 | put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
|
---|
279 |
|
---|
280 | /* return to the end */
|
---|
281 | url_fseek(pb, end_size, SEEK_SET);
|
---|
282 |
|
---|
283 | put_flush_packet(pb);
|
---|
284 | }
|
---|
285 |
|
---|
286 | return 0;
|
---|
287 | }
|
---|
288 | #endif //CONFIG_MUXERS
|
---|
289 |
|
---|
290 | static int aiff_probe(AVProbeData *p)
|
---|
291 | {
|
---|
292 | /* check file header */
|
---|
293 | if (p->buf_size < 16)
|
---|
294 | return 0;
|
---|
295 | if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
|
---|
296 | p->buf[2] == 'R' && p->buf[3] == 'M' &&
|
---|
297 | p->buf[8] == 'A' && p->buf[9] == 'I' &&
|
---|
298 | p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
|
---|
299 | return AVPROBE_SCORE_MAX;
|
---|
300 | else
|
---|
301 | return 0;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* aiff input */
|
---|
305 | static int aiff_read_header(AVFormatContext *s,
|
---|
306 | AVFormatParameters *ap)
|
---|
307 | {
|
---|
308 | int size, filesize, offset;
|
---|
309 | uint32_t tag;
|
---|
310 | unsigned version = AIFF_C_VERSION1;
|
---|
311 | ByteIOContext *pb = &s->pb;
|
---|
312 | AVStream * st = s->streams[0];
|
---|
313 |
|
---|
314 | /* check FORM header */
|
---|
315 | filesize = get_tag(pb, &tag);
|
---|
316 | if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
|
---|
317 | return AVERROR_INVALIDDATA;
|
---|
318 |
|
---|
319 | /* AIFF data type */
|
---|
320 | tag = get_le32(pb);
|
---|
321 | if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
|
---|
322 | version = AIFF;
|
---|
323 | else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
|
---|
324 | return AVERROR_INVALIDDATA;
|
---|
325 |
|
---|
326 | filesize -= 4;
|
---|
327 |
|
---|
328 | st = av_new_stream(s, 0);
|
---|
329 | if (!st)
|
---|
330 | return AVERROR_NOMEM;
|
---|
331 |
|
---|
332 | while (filesize > 0) {
|
---|
333 | /* parse different chunks */
|
---|
334 | size = get_tag(pb, &tag);
|
---|
335 | if (size < 0)
|
---|
336 | return size;
|
---|
337 |
|
---|
338 | filesize -= size + 8;
|
---|
339 |
|
---|
340 | switch (tag) {
|
---|
341 | case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
|
---|
342 | /* Then for the complete header info */
|
---|
343 | st->nb_frames = get_aiff_header (pb, st->codec, size, version);
|
---|
344 | if (st->nb_frames < 0)
|
---|
345 | return st->nb_frames;
|
---|
346 | break;
|
---|
347 |
|
---|
348 | case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
|
---|
349 | version = get_be32(pb);
|
---|
350 | break;
|
---|
351 |
|
---|
352 | case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
|
---|
353 | get_meta (pb, s->title, sizeof(s->title), size);
|
---|
354 | break;
|
---|
355 |
|
---|
356 | case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
|
---|
357 | get_meta (pb, s->author, sizeof(s->author), size);
|
---|
358 | break;
|
---|
359 |
|
---|
360 | case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
|
---|
361 | get_meta (pb, s->copyright, sizeof(s->copyright), size);
|
---|
362 | break;
|
---|
363 |
|
---|
364 | case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
|
---|
365 | get_meta (pb, s->comment, sizeof(s->comment), size);
|
---|
366 | break;
|
---|
367 |
|
---|
368 | case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
|
---|
369 | get_be32(pb); /* Block align... don't care */
|
---|
370 | offset = get_be32(pb); /* Offset of sound data */
|
---|
371 | goto got_sound;
|
---|
372 |
|
---|
373 | default: /* Jump */
|
---|
374 | if (size & 1) /* Always even aligned */
|
---|
375 | size++;
|
---|
376 | url_fskip (pb, size);
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /* End of loop and didn't get sound */
|
---|
381 | return AVERROR_INVALIDDATA;
|
---|
382 |
|
---|
383 | got_sound:
|
---|
384 | /* Now positioned, get the sound data start and end */
|
---|
385 | if (st->nb_frames)
|
---|
386 | s->file_size = st->nb_frames * st->codec->block_align;
|
---|
387 |
|
---|
388 | av_set_pts_info(st, 64, 1, st->codec->sample_rate);
|
---|
389 | st->start_time = 0;
|
---|
390 | st->duration = st->nb_frames;
|
---|
391 |
|
---|
392 | /* Position the stream at the first block */
|
---|
393 | url_fskip(pb, offset);
|
---|
394 |
|
---|
395 | return 0;
|
---|
396 | }
|
---|
397 |
|
---|
398 | #define MAX_SIZE 4096
|
---|
399 |
|
---|
400 | static int aiff_read_packet(AVFormatContext *s,
|
---|
401 | AVPacket *pkt)
|
---|
402 | {
|
---|
403 | AVStream *st = s->streams[0];
|
---|
404 | int res;
|
---|
405 |
|
---|
406 | /* End of stream may be reached */
|
---|
407 | if (url_feof(&s->pb))
|
---|
408 | return AVERROR_IO;
|
---|
409 |
|
---|
410 | /* Now for that packet */
|
---|
411 | res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
|
---|
412 | if (res < 0)
|
---|
413 | return res;
|
---|
414 |
|
---|
415 | /* Only one stream in an AIFF file */
|
---|
416 | pkt->stream_index = 0;
|
---|
417 | return 0;
|
---|
418 | }
|
---|
419 |
|
---|
420 | static int aiff_read_close(AVFormatContext *s)
|
---|
421 | {
|
---|
422 | return 0;
|
---|
423 | }
|
---|
424 |
|
---|
425 | static int aiff_read_seek(AVFormatContext *s,
|
---|
426 | int stream_index, int64_t timestamp, int flags)
|
---|
427 | {
|
---|
428 | return pcm_read_seek(s, stream_index, timestamp, flags);
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | static AVInputFormat aiff_demuxer = {
|
---|
433 | "aiff",
|
---|
434 | "Audio IFF",
|
---|
435 | 0,
|
---|
436 | aiff_probe,
|
---|
437 | aiff_read_header,
|
---|
438 | aiff_read_packet,
|
---|
439 | aiff_read_close,
|
---|
440 | aiff_read_seek,
|
---|
441 | };
|
---|
442 |
|
---|
443 | #ifdef CONFIG_MUXERS
|
---|
444 | static AVOutputFormat aiff_muxer = {
|
---|
445 | "aiff",
|
---|
446 | "Audio IFF",
|
---|
447 | "audio/aiff",
|
---|
448 | "aif,aiff,afc,aifc",
|
---|
449 | sizeof(AIFFOutputContext),
|
---|
450 | CODEC_ID_PCM_S16BE,
|
---|
451 | CODEC_ID_NONE,
|
---|
452 | aiff_write_header,
|
---|
453 | aiff_write_packet,
|
---|
454 | aiff_write_trailer,
|
---|
455 | };
|
---|
456 | #endif //CONFIG_MUXERS
|
---|
457 |
|
---|
458 | int ff_aiff_init(void)
|
---|
459 | {
|
---|
460 | av_register_input_format(&aiff_demuxer);
|
---|
461 | #ifdef CONFIG_MUXERS
|
---|
462 | av_register_output_format(&aiff_muxer);
|
---|
463 | #endif //CONFIG_MUXERS
|
---|
464 | return 0;
|
---|
465 | }
|
---|
466 |
|
---|