Browse code

Core Audio Format demuxer

Originally committed as revision 19879 to svn://svn.ffmpeg.org/ffmpeg/trunk

Peter Ross authored on 2009/09/16 21:26:59
Showing 7 changed files
... ...
@@ -37,6 +37,7 @@ version <next>:
37 37
 - Bluray (PGS) subtitle decoder
38 38
 - LPCM support in MPEG-TS (HDMV RID as found on Blu-ray disks)
39 39
 - Wmapro decoder
40
+- Core Audio Format demuxer
40 41
 
41 42
 
42 43
 
... ...
@@ -63,6 +63,8 @@ library:
63 63
     @tab Used in the game Cyberia from Interplay.
64 64
 @item Delphine Software International CIN @tab   @tab X
65 65
     @tab Multimedia format used by Delphine Software games.
66
+@item Core Audio Format         @tab   @tab X
67
+    @tab Apple Core Audio Format
66 68
 @item CRC testing format        @tab X @tab
67 69
 @item Creative Voice            @tab X @tab X
68 70
     @tab Created for the Sound Blaster Pro.
... ...
@@ -32,6 +32,7 @@ OBJS-$(CONFIG_AVS_DEMUXER)               += avs.o vocdec.o voc.o
32 32
 OBJS-$(CONFIG_BETHSOFTVID_DEMUXER)       += bethsoftvid.o
33 33
 OBJS-$(CONFIG_BFI_DEMUXER)               += bfi.o
34 34
 OBJS-$(CONFIG_C93_DEMUXER)               += c93.o vocdec.o voc.o
35
+OBJS-$(CONFIG_CAF_DEMUXER)               += cafdec.o caf.o mov.o riff.o isom.o
35 36
 OBJS-$(CONFIG_CAVSVIDEO_DEMUXER)         += raw.o
36 37
 OBJS-$(CONFIG_CRC_MUXER)                 += crcenc.o
37 38
 OBJS-$(CONFIG_DAUD_DEMUXER)              += daud.o
... ...
@@ -65,6 +65,7 @@ void av_register_all(void)
65 65
     REGISTER_DEMUXER  (BETHSOFTVID, bethsoftvid);
66 66
     REGISTER_DEMUXER  (BFI, bfi);
67 67
     REGISTER_DEMUXER  (C93, c93);
68
+    REGISTER_DEMUXER  (CAF, caf);
68 69
     REGISTER_DEMUXER  (CAVSVIDEO, cavsvideo);
69 70
     REGISTER_MUXER    (CRC, crc);
70 71
     REGISTER_MUXDEMUX (DAUD, daud);
71 72
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+/*
1
+ * CAF common code
2
+ * Copyright (c) 2007  Justin Ruggles
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file libavformat/caf.c
23
+ * CAF common code
24
+ */
25
+
26
+#include "avformat.h"
27
+#include "riff.h"
28
+#include "caf.h"
29
+
30
+/**
31
+ * Known codec tags for CAF
32
+ */
33
+const AVCodecTag ff_codec_caf_tags[] = {
34
+    { CODEC_ID_AAC,             MKBETAG('a','a','c',' ') },
35
+    { CODEC_ID_AC3,             MKBETAG('a','c','-','3') },
36
+    { CODEC_ID_ALAC,            MKBETAG('a','l','a','c') },
37
+  /* FIXME: use DV demuxer, as done in MOV */
38
+  /*{ CODEC_ID_DVAUDIO,         MKBETAG('v','d','v','a') },*/
39
+  /*{ CODEC_ID_DVAUDIO,         MKBETAG('d','v','c','a') },*/
40
+    { CODEC_ID_ADPCM_IMA_QT,    MKBETAG('i','m','a','4') },
41
+    { CODEC_ID_MACE3,           MKBETAG('M','A','C','3') },
42
+    { CODEC_ID_MACE6,           MKBETAG('M','A','C','6') },
43
+    { CODEC_ID_MP3,             MKBETAG('.','m','p','3') },
44
+    { CODEC_ID_MP2,             MKBETAG('.','m','p','2') },
45
+    { CODEC_ID_MP1,             MKBETAG('.','m','p','1') },
46
+    { CODEC_ID_PCM_ALAW,        MKBETAG('a','l','a','w') },
47
+    { CODEC_ID_PCM_MULAW,       MKBETAG('u','l','a','w') },
48
+    { CODEC_ID_QCELP,           MKBETAG('Q','c','l','p') },
49
+    { CODEC_ID_QDM2,            MKBETAG('Q','D','M','2') },
50
+    { CODEC_ID_QDM2,            MKBETAG('Q','D','M','C') },
51
+  /* currently unsupported codecs */
52
+  /*{ AC-3 over S/PDIF          MKBETAG('c','a','c','3') },*/
53
+  /*{ MPEG4CELP                 MKBETAG('c','e','l','p') },*/
54
+  /*{ MPEG4HVXC                 MKBETAG('h','v','x','c') },*/
55
+  /*{ MPEG4TwinVQ               MKBETAG('t','w','v','q') },*/
56
+    { 0, 0 },
57
+};
0 58
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+/*
1
+ * CAF common code
2
+ * Copyright (c) 2007  Justin Ruggles
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file libavformat/caf.h
23
+ * CAF common code
24
+ */
25
+
26
+#ifndef AVFORMAT_CAF_H
27
+#define AVFORMAT_CAF_H
28
+
29
+#include "avformat.h"
30
+
31
+extern const AVCodecTag ff_codec_caf_tags[];
32
+
33
+#endif /* AVFORMAT_CAF_H */
0 34
new file mode 100644
... ...
@@ -0,0 +1,379 @@
0
+/*
1
+ * Core Audio Format demuxer
2
+ * Copyright (c) 2007 Justin Ruggles
3
+ * Copyright (c) 2009 Peter Ross
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg 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
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file libavformat/cafdec.c
24
+ * Core Audio Format demuxer
25
+ */
26
+
27
+#include "avformat.h"
28
+#include "riff.h"
29
+#include "isom.h"
30
+#include "libavutil/intreadwrite.h"
31
+#include "caf.h"
32
+
33
+typedef struct {
34
+    int bytes_per_packet;           ///< bytes in a packet, or 0 if variable
35
+    int frames_per_packet;          ///< frames in a packet, or 0 if variable
36
+    int64_t num_bytes;              ///< total number of bytes in stream
37
+
38
+    int64_t packet_cnt;             ///< packet counter
39
+    int64_t frame_cnt;              ///< frame counter
40
+
41
+    int64_t data_start;             ///< data start position, in bytes
42
+    int64_t data_size;              ///< raw data size, in bytes
43
+} CaffContext;
44
+
45
+static int probe(AVProbeData *p)
46
+{
47
+    if (AV_RB32(p->buf) == MKBETAG('c','a','f','f') && AV_RB16(&p->buf[4]) == 1)
48
+        return AVPROBE_SCORE_MAX;
49
+    return 0;
50
+}
51
+
52
+/** Read audio description chunk */
53
+static int read_desc_chunk(AVFormatContext *s)
54
+{
55
+    ByteIOContext *pb = s->pb;
56
+    CaffContext *caf  = s->priv_data;
57
+    AVStream *st;
58
+    int flags;
59
+
60
+    /* new audio stream */
61
+    st = av_new_stream(s, 0);
62
+    if (!st)
63
+        return AVERROR_NOMEM;
64
+
65
+    /* parse format description */
66
+    st->codec->codec_type  = CODEC_TYPE_AUDIO;
67
+    st->codec->sample_rate = av_int2dbl(get_be64(pb));
68
+    st->codec->codec_tag   = get_be32(pb);
69
+    flags = get_be32(pb);
70
+    caf->bytes_per_packet  = get_be32(pb);
71
+    st->codec->block_align = caf->bytes_per_packet;
72
+    caf->frames_per_packet = get_be32(pb);
73
+    st->codec->channels    = get_be32(pb);
74
+    st->codec->bits_per_coded_sample = get_be32(pb);
75
+
76
+    /* calculate bit rate for constant size packets */
77
+    if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
78
+        st->codec->bit_rate = (uint64_t)st->codec->sample_rate * (uint64_t)caf->bytes_per_packet * 8
79
+                              / (uint64_t)caf->frames_per_packet;
80
+    } else {
81
+        st->codec->bit_rate = 0;
82
+    }
83
+
84
+    /* determine codec */
85
+    if (st->codec->codec_tag == MKBETAG('l','p','c','m'))
86
+        st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, (flags ^ 0x2) | 0x4);
87
+    else
88
+        st->codec->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codec->codec_tag);
89
+    return 0;
90
+}
91
+
92
+/** Read magic cookie chunk */
93
+static int read_kuki_chunk(AVFormatContext *s, int64_t size)
94
+{
95
+    ByteIOContext *pb = s->pb;
96
+    AVStream *st      = s->streams[0];
97
+
98
+    if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
99
+        return -1;
100
+
101
+    if (st->codec->codec_id == CODEC_ID_AAC) {
102
+        /* The magic cookie format for AAC is an mp4 esds atom.
103
+           The lavc AAC decoder requires the data from the codec specific
104
+           description as extradata input. */
105
+        int strt, skip;
106
+        MOVAtom atom;
107
+
108
+        strt = url_ftell(pb);
109
+        ff_mov_read_esds(s, pb, atom);
110
+        skip = size - (url_ftell(pb) - strt);
111
+        if (skip < 0 || !st->codec->extradata ||
112
+            st->codec->codec_id != CODEC_ID_AAC) {
113
+            av_log(s, AV_LOG_ERROR, "invalid AAC magic cookie\n");
114
+            return AVERROR_INVALIDDATA;
115
+        }
116
+        url_fskip(pb, skip);
117
+    } else {
118
+        st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
119
+        if (!st->codec->extradata)
120
+            return AVERROR(ENOMEM);
121
+        get_buffer(pb, st->codec->extradata, size);
122
+        st->codec->extradata_size = size;
123
+    }
124
+
125
+    return 0;
126
+}
127
+
128
+/** Read packet table chunk */
129
+static int read_pakt_chunk(AVFormatContext *s, int64_t size)
130
+{
131
+    ByteIOContext *pb = s->pb;
132
+    AVStream *st      = s->streams[0];
133
+    CaffContext *caf  = s->priv_data;
134
+    int64_t pos = 0, ccount;
135
+    int num_packets, i;
136
+
137
+    ccount = url_ftell(pb);
138
+
139
+    num_packets = get_be64(pb);
140
+    if (num_packets < 0 || INT32_MAX / sizeof(AVIndexEntry) < num_packets)
141
+        return AVERROR_INVALIDDATA;
142
+
143
+    st->nb_frames  = get_be64(pb); /* valid frames */
144
+    st->nb_frames += get_be32(pb); /* priming frames */
145
+    st->nb_frames += get_be32(pb); /* remainder frames */
146
+
147
+    st->duration = 0;
148
+    for (i = 0; i < num_packets; i++) {
149
+        av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
150
+        pos += caf->bytes_per_packet ? caf->bytes_per_packet : ff_mp4_read_descr_len(pb);
151
+        st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb);
152
+    }
153
+
154
+    if (url_ftell(pb) - ccount != size) {
155
+        av_log(s, AV_LOG_ERROR, "error reading packet table\n");
156
+        return -1;
157
+    }
158
+
159
+    caf->num_bytes = pos;
160
+    return 0;
161
+}
162
+
163
+/** Read information chunk */
164
+static void read_info_chunk(AVFormatContext *s, int64_t size)
165
+{
166
+    ByteIOContext *pb = s->pb;
167
+    unsigned int i;
168
+    unsigned int nb_entries = get_be32(pb);
169
+    for (i = 0; i < nb_entries; i++) {
170
+        char key[32];
171
+        char value[1024];
172
+        get_strz(pb, key, sizeof(key));
173
+        get_strz(pb, value, sizeof(value));
174
+        av_metadata_set(&s->metadata, key, value);
175
+    }
176
+}
177
+
178
+static int read_header(AVFormatContext *s,
179
+                       AVFormatParameters *ap)
180
+{
181
+    ByteIOContext *pb = s->pb;
182
+    CaffContext *caf  = s->priv_data;
183
+    AVStream *st;
184
+    uint32_t tag = 0;
185
+    int found_data, ret;
186
+    int64_t size;
187
+
188
+    url_fskip(pb, 8); /* magic, version, file flags */
189
+
190
+    /* audio description chunk */
191
+    if (get_be32(pb) != MKBETAG('d','e','s','c')) {
192
+        av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
193
+        return AVERROR_INVALIDDATA;
194
+    }
195
+    size = get_be64(pb);
196
+    if (size != 32)
197
+        return AVERROR_INVALIDDATA;
198
+
199
+    ret = read_desc_chunk(s);
200
+    if (ret)
201
+        return ret;
202
+    st = s->streams[0];
203
+
204
+    /* parse each chunk */
205
+    found_data = 0;
206
+    while (!url_feof(pb)) {
207
+
208
+        /* stop at data chunk if seeking is not supported or
209
+           data chunk size is unknown */
210
+        if (found_data && (caf->data_size < 0 || url_is_streamed(pb)))
211
+            break;
212
+
213
+        tag  = get_be32(pb);
214
+        size = get_be64(pb);
215
+        if (url_feof(pb))
216
+            break;
217
+
218
+        switch (tag) {
219
+        case MKBETAG('d','a','t','a'):
220
+            url_fskip(pb, 4); /* edit count */
221
+            caf->data_start = url_ftell(pb);
222
+            caf->data_size  = size < 0 ? -1 : size - 4;
223
+            if (caf->data_size > 0 && !url_is_streamed(pb))
224
+                url_fskip(pb, caf->data_size);
225
+            found_data = 1;
226
+            break;
227
+
228
+        /* magic cookie chunk */
229
+        case MKBETAG('k','u','k','i'):
230
+            if (read_kuki_chunk(s, size))
231
+                return AVERROR_INVALIDDATA;
232
+            break;
233
+
234
+        /* packet table chunk */
235
+        case MKBETAG('p','a','k','t'):
236
+            if (read_pakt_chunk(s, size))
237
+                return AVERROR_INVALIDDATA;
238
+            break;
239
+
240
+        case MKBETAG('i','n','f','o'):
241
+            read_info_chunk(s, size);
242
+            break;
243
+
244
+        default:
245
+#define _(x) ((x) >= ' ' ? (x) : ' ')
246
+            av_log(s, AV_LOG_WARNING, "skipping CAF chunk: %08X (%c%c%c%c)\n",
247
+                tag, _(tag>>24), _((tag>>16)&0xFF), _((tag>>8)&0xFF), _(tag&0xFF));
248
+#undef _
249
+        case MKBETAG('f','r','e','e'):
250
+            if (size < 0)
251
+                return AVERROR_INVALIDDATA;
252
+            url_fskip(pb, size);
253
+            break;
254
+        }
255
+    }
256
+
257
+    if (!found_data)
258
+        return AVERROR_INVALIDDATA;
259
+
260
+    if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
261
+        if (caf->data_size > 0)
262
+            st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
263
+    } else if (st->nb_index_entries) {
264
+        st->codec->bit_rate = st->codec->sample_rate * caf->data_size * 8 /
265
+                              st->duration;
266
+    } else {
267
+        av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
268
+                                "block size or frame size are variable.\n");
269
+        return AVERROR_INVALIDDATA;
270
+    }
271
+    s->file_size = url_fsize(pb);
272
+    s->file_size = FFMAX(0, s->file_size);
273
+
274
+    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
275
+    st->start_time = 0;
276
+
277
+    /* position the stream at the start of data */
278
+    if (caf->data_size >= 0)
279
+        url_fseek(pb, caf->data_start, SEEK_SET);
280
+
281
+    return 0;
282
+}
283
+
284
+#define CAF_MAX_PKT_SIZE 4096
285
+
286
+static int read_packet(AVFormatContext *s, AVPacket *pkt)
287
+{
288
+    ByteIOContext *pb = s->pb;
289
+    AVStream *st      = s->streams[0];
290
+    CaffContext *caf  = s->priv_data;
291
+    int res, pkt_size = 0, pkt_frames = 0;
292
+    int64_t left      = CAF_MAX_PKT_SIZE;
293
+
294
+    if (url_feof(pb))
295
+        return AVERROR_IO;
296
+
297
+    /* don't read past end of data chunk */
298
+    if (caf->data_size > 0) {
299
+        left = (caf->data_start + caf->data_size) - url_ftell(pb);
300
+        if (left <= 0)
301
+            return AVERROR_IO;
302
+    }
303
+
304
+    pkt_frames = caf->frames_per_packet;
305
+    pkt_size   = caf->bytes_per_packet;
306
+
307
+    if (pkt_size > 0 && pkt_frames == 1) {
308
+        pkt_size   = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
309
+        pkt_size   = FFMIN(pkt_size, left);
310
+        pkt_frames = pkt_size / caf->bytes_per_packet;
311
+    } else if (st->nb_index_entries) {
312
+        if (caf->packet_cnt < st->nb_index_entries - 1) {
313
+            pkt_size   = st->index_entries[caf->packet_cnt + 1].pos       - st->index_entries[caf->packet_cnt].pos;
314
+            pkt_frames = st->index_entries[caf->packet_cnt + 1].timestamp - st->index_entries[caf->packet_cnt].timestamp;
315
+        } else if (caf->packet_cnt == st->nb_index_entries - 1) {
316
+            pkt_size   = caf->num_bytes - st->index_entries[caf->packet_cnt].pos;
317
+            pkt_frames = st->duration   - st->index_entries[caf->packet_cnt].timestamp;
318
+        } else {
319
+            return AVERROR_IO;
320
+        }
321
+    }
322
+
323
+    if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
324
+        return AVERROR_IO;
325
+
326
+    res = av_get_packet(pb, pkt, pkt_size);
327
+    if (res < 0)
328
+        return res;
329
+
330
+    pkt->size           = res;
331
+    pkt->stream_index   = 0;
332
+    pkt->dts = pkt->pts = caf->frame_cnt;
333
+
334
+    caf->packet_cnt++;
335
+    caf->frame_cnt += pkt_frames;
336
+
337
+    return 0;
338
+}
339
+
340
+static int read_seek(AVFormatContext *s, int stream_index,
341
+                     int64_t timestamp, int flags)
342
+{
343
+    AVStream *st = s->streams[0];
344
+    CaffContext *caf = s->priv_data;
345
+    int64_t pos;
346
+
347
+    timestamp = FFMAX(timestamp, 0);
348
+
349
+    if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
350
+        /* calculate new byte position based on target frame position */
351
+        pos = caf->bytes_per_packet * timestamp / caf->frames_per_packet;
352
+        if (caf->data_size > 0)
353
+            pos = FFMIN(pos, caf->data_size);
354
+        caf->packet_cnt = pos / caf->bytes_per_packet;
355
+        caf->frame_cnt  = caf->frames_per_packet * caf->packet_cnt;
356
+    } else if (st->nb_index_entries) {
357
+        caf->packet_cnt = av_index_search_timestamp(st, timestamp, flags);
358
+        caf->frame_cnt  = st->index_entries[caf->packet_cnt].timestamp;
359
+        pos             = st->index_entries[caf->packet_cnt].pos;
360
+    } else {
361
+        return -1;
362
+    }
363
+
364
+    url_fseek(s->pb, pos + caf->data_start, SEEK_SET);
365
+    return 0;
366
+}
367
+
368
+AVInputFormat caf_demuxer = {
369
+    "caf",
370
+    NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
371
+    sizeof(CaffContext),
372
+    probe,
373
+    read_header,
374
+    read_packet,
375
+    NULL,
376
+    read_seek,
377
+    .codec_tag = (const AVCodecTag*[]){ff_codec_caf_tags, 0},
378
+};