Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiffdec.c @ e356fc57

History | View | Annotate | Download (9.65 KB)

1 f11288da Michael Niedermayer
/*
2 47b47bbd Justin Ruggles
 * AIFF/AIFF-C demuxer
3 f11288da Michael Niedermayer
 * Copyright (c) 2006  Patrick Guimond
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 f11288da Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 f11288da Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 f11288da Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 f11288da Michael Niedermayer
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21 245976da Diego Biurrun
22
#include "libavutil/intfloat_readwrite.h"
23 f11288da Michael Niedermayer
#include "avformat.h"
24 e94204df Aurelien Jacobs
#include "pcm.h"
25 47b47bbd Justin Ruggles
#include "aiff.h"
26 f11288da Michael Niedermayer
27
#define AIFF                    0
28
#define AIFF_C_VERSION1         0xA2805140
29
30 24c6f152 Justin Ruggles
typedef struct {
31
    int64_t data_end;
32
} AIFFInputContext;
33
34 fb65d2ca Diego Pettenò
static enum CodecID aiff_codec_get_id(int bps)
35 f11288da Michael Niedermayer
{
36
    if (bps <= 8)
37
        return CODEC_ID_PCM_S8;
38
    if (bps <= 16)
39
        return CODEC_ID_PCM_S16BE;
40
    if (bps <= 24)
41
        return CODEC_ID_PCM_S24BE;
42
    if (bps <= 32)
43
        return CODEC_ID_PCM_S32BE;
44
45
    /* bigger than 32 isn't allowed  */
46 fb65d2ca Diego Pettenò
    return CODEC_ID_NONE;
47 f11288da Michael Niedermayer
}
48
49
/* returns the size of the found tag */
50 ae628ec1 Anton Khirnov
static int get_tag(AVIOContext *pb, uint32_t * tag)
51 f11288da Michael Niedermayer
{
52
    int size;
53
54
    if (url_feof(pb))
55 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
56 f11288da Michael Niedermayer
57 b7effd4e Anton Khirnov
    *tag = avio_rl32(pb);
58
    size = avio_rb32(pb);
59 f11288da Michael Niedermayer
60
    if (size < 0)
61
        size = 0x7fffffff;
62
63
    return size;
64
}
65
66
/* Metadata string read */
67 c0f14458 Aurelien Jacobs
static void get_meta(AVFormatContext *s, const char *key, int size)
68 f11288da Michael Niedermayer
{
69 12ad6671 Michael Niedermayer
    uint8_t *str = av_malloc(size+1);
70
    int res;
71
72
    if (!str) {
73 e356fc57 Anton Khirnov
        avio_seek(s->pb, size, SEEK_CUR);
74 12ad6671 Michael Niedermayer
        return;
75
    }
76
77 b7effd4e Anton Khirnov
    res = avio_read(s->pb, str, size);
78 f11288da Michael Niedermayer
    if (res < 0)
79
        return;
80
81
    str[res] = 0;
82 12ad6671 Michael Niedermayer
    av_metadata_set2(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
83 f11288da Michael Niedermayer
}
84
85
/* Returns the number of sound data frames or negative on error */
86 ae628ec1 Anton Khirnov
static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
87 f11288da Michael Niedermayer
                             int size, unsigned version)
88
{
89
    AVExtFloat ext;
90
    double sample_rate;
91
    unsigned int num_frames;
92
93
    if (size & 1)
94
        size++;
95 72415b2a Stefano Sabatini
    codec->codec_type = AVMEDIA_TYPE_AUDIO;
96 b7effd4e Anton Khirnov
    codec->channels = avio_rb16(pb);
97
    num_frames = avio_rb32(pb);
98
    codec->bits_per_coded_sample = avio_rb16(pb);
99 f11288da Michael Niedermayer
100 b7effd4e Anton Khirnov
    avio_read(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
101 f11288da Michael Niedermayer
    sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
102
    codec->sample_rate = sample_rate;
103
    size -= 18;
104
105
    /* Got an AIFF-C? */
106
    if (version == AIFF_C_VERSION1) {
107 b7effd4e Anton Khirnov
        codec->codec_tag = avio_rl32(pb);
108 47b47bbd Justin Ruggles
        codec->codec_id  = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
109 f11288da Michael Niedermayer
110 3a957df4 Baptiste Coudurier
        switch (codec->codec_id) {
111
        case CODEC_ID_PCM_S16BE:
112 dd1c8f3e Luca Abeni
            codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
113
            codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
114 3a957df4 Baptiste Coudurier
            break;
115
        case CODEC_ID_ADPCM_IMA_QT:
116
            codec->block_align = 34*codec->channels;
117 e0130159 Baptiste Coudurier
            codec->frame_size = 64;
118 3a957df4 Baptiste Coudurier
            break;
119 4649fa90 Baptiste Coudurier
        case CODEC_ID_MACE3:
120 112249de Baptiste Coudurier
            codec->block_align = 2*codec->channels;
121
            codec->frame_size = 6;
122
            break;
123 4649fa90 Baptiste Coudurier
        case CODEC_ID_MACE6:
124 112249de Baptiste Coudurier
            codec->block_align = 1*codec->channels;
125 4649fa90 Baptiste Coudurier
            codec->frame_size = 6;
126
            break;
127 5d79f30d Baptiste Coudurier
        case CODEC_ID_GSM:
128
            codec->block_align = 33;
129
            codec->frame_size = 160;
130
            break;
131 cea65433 Vitor Sessak
        case CODEC_ID_QCELP:
132
            codec->block_align = 35;
133
            codec->frame_size= 160;
134
            break;
135 3a957df4 Baptiste Coudurier
        default:
136
            break;
137 f11288da Michael Niedermayer
        }
138
        size -= 4;
139
    } else {
140
        /* Need the codec type */
141 dd1c8f3e Luca Abeni
        codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
142
        codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
143 f11288da Michael Niedermayer
    }
144
145
    /* Block align needs to be computed in all cases, as the definition
146
     * is specific to applications -> here we use the WAVE format definition */
147 3a957df4 Baptiste Coudurier
    if (!codec->block_align)
148 dd1c8f3e Luca Abeni
        codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
149 f11288da Michael Niedermayer
150 28da7981 Baptiste Coudurier
    codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
151
                       codec->sample_rate) * (codec->block_align << 3);
152 f11288da Michael Niedermayer
153
    /* Chunk is over */
154
    if (size)
155 6b4aa5da Anton Khirnov
        avio_seek(pb, size, SEEK_CUR);
156 f11288da Michael Niedermayer
157
    return num_frames;
158
}
159
160
static int aiff_probe(AVProbeData *p)
161
{
162
    /* check file header */
163
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
164
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
165
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
166
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
167
        return AVPROBE_SCORE_MAX;
168
    else
169
        return 0;
170
}
171
172
/* aiff input */
173
static int aiff_read_header(AVFormatContext *s,
174
                            AVFormatParameters *ap)
175
{
176 620d1d78 Baptiste Coudurier
    int size, filesize;
177 bc5c918e Diego Biurrun
    int64_t offset = 0;
178 f11288da Michael Niedermayer
    uint32_t tag;
179
    unsigned version = AIFF_C_VERSION1;
180 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
181 551b41d4 Michael Niedermayer
    AVStream * st;
182 24c6f152 Justin Ruggles
    AIFFInputContext *aiff = s->priv_data;
183 f11288da Michael Niedermayer
184
    /* check FORM header */
185
    filesize = get_tag(pb, &tag);
186
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
187
        return AVERROR_INVALIDDATA;
188
189
    /* AIFF data type */
190 b7effd4e Anton Khirnov
    tag = avio_rl32(pb);
191 f11288da Michael Niedermayer
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
192
        version = AIFF;
193
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
194
        return AVERROR_INVALIDDATA;
195
196
    filesize -= 4;
197
198
    st = av_new_stream(s, 0);
199
    if (!st)
200 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
201 f11288da Michael Niedermayer
202
    while (filesize > 0) {
203
        /* parse different chunks */
204
        size = get_tag(pb, &tag);
205
        if (size < 0)
206
            return size;
207
208
        filesize -= size + 8;
209
210
        switch (tag) {
211 54d8fd20 Baptiste Coudurier
        case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
212
            /* Then for the complete header info */
213 24c1d3b5 Baptiste Coudurier
            st->nb_frames = get_aiff_header(pb, st->codec, size, version);
214 54d8fd20 Baptiste Coudurier
            if (st->nb_frames < 0)
215
                return st->nb_frames;
216
            if (offset > 0) // COMM is after SSND
217
                goto got_sound;
218
            break;
219
        case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
220 b7effd4e Anton Khirnov
            version = avio_rb32(pb);
221 54d8fd20 Baptiste Coudurier
            break;
222
        case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
223 c0f14458 Aurelien Jacobs
            get_meta(s, "title"    , size);
224 54d8fd20 Baptiste Coudurier
            break;
225
        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
226 c0f14458 Aurelien Jacobs
            get_meta(s, "author"   , size);
227 54d8fd20 Baptiste Coudurier
            break;
228
        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
229 c0f14458 Aurelien Jacobs
            get_meta(s, "copyright", size);
230 54d8fd20 Baptiste Coudurier
            break;
231
        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
232 c0f14458 Aurelien Jacobs
            get_meta(s, "comment"  , size);
233 54d8fd20 Baptiste Coudurier
            break;
234
        case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
235 24c6f152 Justin Ruggles
            aiff->data_end = url_ftell(pb) + size;
236 b7effd4e Anton Khirnov
            offset = avio_rb32(pb);      /* Offset of sound data */
237
            avio_rb32(pb);               /* BlockSize... don't care */
238 54d8fd20 Baptiste Coudurier
            offset += url_ftell(pb);    /* Compute absolute data offset */
239 3f60afad Baptiste Coudurier
            if (st->codec->block_align)    /* Assume COMM already parsed */
240 54d8fd20 Baptiste Coudurier
                goto got_sound;
241
            if (url_is_streamed(pb)) {
242
                av_log(s, AV_LOG_ERROR, "file is not seekable\n");
243
                return -1;
244
            }
245 e356fc57 Anton Khirnov
            avio_seek(pb, size - 8, SEEK_CUR);
246 54d8fd20 Baptiste Coudurier
            break;
247 b8d3daca Baptiste Coudurier
        case MKTAG('w', 'a', 'v', 'e'):
248 9c7fd997 Baptiste Coudurier
            if ((uint64_t)size > (1<<30))
249
                return -1;
250 b8d3daca Baptiste Coudurier
            st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
251
            if (!st->codec->extradata)
252
                return AVERROR(ENOMEM);
253
            st->codec->extradata_size = size;
254 b7effd4e Anton Khirnov
            avio_read(pb, st->codec->extradata, size);
255 b8d3daca Baptiste Coudurier
            break;
256 54d8fd20 Baptiste Coudurier
        default: /* Jump */
257
            if (size & 1)   /* Always even aligned */
258
                size++;
259 e356fc57 Anton Khirnov
            avio_seek(pb, size, SEEK_CUR);
260 f11288da Michael Niedermayer
        }
261
    }
262
263 3f60afad Baptiste Coudurier
    if (!st->codec->block_align) {
264
        av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
265
        return -1;
266
    }
267 f11288da Michael Niedermayer
268
got_sound:
269
    /* Now positioned, get the sound data start and end */
270
    if (st->nb_frames)
271
        s->file_size = st->nb_frames * st->codec->block_align;
272
273
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
274
    st->start_time = 0;
275 e0130159 Baptiste Coudurier
    st->duration = st->codec->frame_size ?
276
        st->nb_frames * st->codec->frame_size : st->nb_frames;
277 f11288da Michael Niedermayer
278
    /* Position the stream at the first block */
279 6b4aa5da Anton Khirnov
    avio_seek(pb, offset, SEEK_SET);
280 f11288da Michael Niedermayer
281
    return 0;
282
}
283
284
#define MAX_SIZE 4096
285
286
static int aiff_read_packet(AVFormatContext *s,
287
                            AVPacket *pkt)
288
{
289 a959f0ea Baptiste Coudurier
    AVStream *st = s->streams[0];
290 24c6f152 Justin Ruggles
    AIFFInputContext *aiff = s->priv_data;
291
    int64_t max_size;
292 cea65433 Vitor Sessak
    int res, size;
293 f11288da Michael Niedermayer
294 24c6f152 Justin Ruggles
    /* calculate size of remaining data */
295
    max_size = aiff->data_end - url_ftell(s->pb);
296
    if (max_size <= 0)
297
        return AVERROR_EOF;
298
299 f11288da Michael Niedermayer
    /* Now for that packet */
300 cea65433 Vitor Sessak
    if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
301
        size = st->codec->block_align;
302
    else
303
        size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
304
    size = FFMIN(max_size, size);
305
    res = av_get_packet(s->pb, pkt, size);
306 f11288da Michael Niedermayer
    if (res < 0)
307
        return res;
308
309
    /* Only one stream in an AIFF file */
310
    pkt->stream_index = 0;
311
    return 0;
312
}
313
314 c6610a21 Diego Elio Pettenò
AVInputFormat ff_aiff_demuxer = {
315 f11288da Michael Niedermayer
    "aiff",
316 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Audio IFF"),
317 24c6f152 Justin Ruggles
    sizeof(AIFFInputContext),
318 f11288da Michael Niedermayer
    aiff_probe,
319
    aiff_read_header,
320
    aiff_read_packet,
321 4c638f0c Baptiste Coudurier
    NULL,
322 5256e42c Ramiro Polla
    pcm_read_seek,
323 47b47bbd Justin Ruggles
    .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
324 f11288da Michael Niedermayer
};