aiff.c 13.6 KB
Newer Older
1
/*
2
 * AIFF/AIFF-C muxer and demuxer
3 4
 * Copyright (c) 2006  Patrick Guimond
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
21 22

#include "libavutil/intfloat_readwrite.h"
23
#include "avformat.h"
24
#include "raw.h"
25
#include "riff.h"
26

27
static const AVCodecTag codec_aiff_tags[] = {
28 29 30 31
    { CODEC_ID_PCM_S16BE,    MKTAG('N','O','N','E') },
    { CODEC_ID_PCM_S8,       MKTAG('N','O','N','E') },
    { CODEC_ID_PCM_S24BE,    MKTAG('N','O','N','E') },
    { CODEC_ID_PCM_S32BE,    MKTAG('N','O','N','E') },
32 33
    { CODEC_ID_PCM_F32BE,    MKTAG('f','l','3','2') },
    { CODEC_ID_PCM_F64BE,    MKTAG('f','l','6','4') },
34 35 36 37 38 39 40
    { CODEC_ID_PCM_ALAW,     MKTAG('a','l','a','w') },
    { CODEC_ID_PCM_MULAW,    MKTAG('u','l','a','w') },
    { CODEC_ID_MACE3,        MKTAG('M','A','C','3') },
    { CODEC_ID_MACE6,        MKTAG('M','A','C','6') },
    { CODEC_ID_GSM,          MKTAG('G','S','M',' ') },
    { CODEC_ID_ADPCM_G726,   MKTAG('G','7','2','6') },
    { CODEC_ID_PCM_S16LE,    MKTAG('s','o','w','t') },
41
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
42
    { CODEC_ID_QDM2,         MKTAG('Q','D','M','2') },
43 44 45 46 47 48
    { 0, 0 },
};

#define AIFF                    0
#define AIFF_C_VERSION1         0xA2805140

Baptiste Coudurier's avatar
Baptiste Coudurier committed
49
static int aiff_codec_get_id(int bps)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
{
    if (bps <= 8)
        return CODEC_ID_PCM_S8;
    if (bps <= 16)
        return CODEC_ID_PCM_S16BE;
    if (bps <= 24)
        return CODEC_ID_PCM_S24BE;
    if (bps <= 32)
        return CODEC_ID_PCM_S32BE;

    /* bigger than 32 isn't allowed  */
    return 0;
}

/* returns the size of the found tag */
static int get_tag(ByteIOContext *pb, uint32_t * tag)
{
    int size;

    if (url_feof(pb))
70
        return AVERROR(EIO);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    *tag = get_le32(pb);
    size = get_be32(pb);

    if (size < 0)
        size = 0x7fffffff;

    return size;
}

/* Metadata string read */
static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
{
    int res;

    if (size > strsize-1)
        res = get_buffer(pb, (uint8_t*)str, strsize-1);
    else
        res = get_buffer(pb, (uint8_t*)str, size);

    if (res < 0)
        return;

    str[res] = 0;
    if (size & 1)
        size++;
    size -= res;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
98
    if (size)
99 100 101 102
        url_fskip(pb, size);
}

/* Returns the number of sound data frames or negative on error */
103
static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                             int size, unsigned version)
{
    AVExtFloat ext;
    double sample_rate;
    unsigned int num_frames;

    if (size & 1)
        size++;
    codec->codec_type = CODEC_TYPE_AUDIO;
    codec->channels = get_be16(pb);
    num_frames = get_be32(pb);
    codec->bits_per_sample = get_be16(pb);

    get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
    sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
    codec->sample_rate = sample_rate;
    size -= 18;

    /* Got an AIFF-C? */
    if (version == AIFF_C_VERSION1) {
        codec->codec_tag = get_le32(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
125
        codec->codec_id  = codec_get_id(codec_aiff_tags, codec->codec_tag);
126

127 128
        switch (codec->codec_id) {
        case CODEC_ID_PCM_S16BE:
Baptiste Coudurier's avatar
Baptiste Coudurier committed
129
            codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
130
            codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
131 132 133
            break;
        case CODEC_ID_ADPCM_IMA_QT:
            codec->block_align = 34*codec->channels;
134
            codec->frame_size = 64;
135
            break;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
136
        case CODEC_ID_MACE3:
137 138 139
            codec->block_align = 2*codec->channels;
            codec->frame_size = 6;
            break;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
140
        case CODEC_ID_MACE6:
141
            codec->block_align = 1*codec->channels;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
142 143
            codec->frame_size = 6;
            break;
144 145
        default:
            break;
146 147 148 149
        }
        size -= 4;
    } else {
        /* Need the codec type */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
150
        codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
151
        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
152 153 154 155
    }

    /* Block align needs to be computed in all cases, as the definition
     * is specific to applications -> here we use the WAVE format definition */
156 157
    if (!codec->block_align)
        codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
158

159 160
    codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
                       codec->sample_rate) * (codec->block_align << 3);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    /* Chunk is over */
    if (size)
        url_fseek(pb, size, SEEK_CUR);

    return num_frames;
}

#ifdef CONFIG_MUXERS
typedef struct {
    offset_t form;
    offset_t frames;
    offset_t ssnd;
} AIFFOutputContext;

static int aiff_write_header(AVFormatContext *s)
{
    AIFFOutputContext *aiff = s->priv_data;
179
    ByteIOContext *pb = s->pb;
180 181
    AVCodecContext *enc = s->streams[0]->codec;
    AVExtFloat sample_rate;
182
    int aifc = 0;
183 184

    /* First verify if format is ok */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
185
    if (!enc->codec_tag)
186
        return -1;
187 188 189
    if (enc->codec_tag != MKTAG('N','O','N','E'))
        aifc = 1;

190 191 192 193
    /* FORM AIFF header */
    put_tag(pb, "FORM");
    aiff->form = url_ftell(pb);
    put_be32(pb, 0);                    /* file length */
194
    put_tag(pb, aifc ? "AIFC" : "AIFF");
195

196 197 198 199 200 201
    if (aifc) { // compressed audio
        enc->bits_per_sample = 16;
        if (!enc->block_align) {
            av_log(s, AV_LOG_ERROR, "block align not set\n");
            return -1;
        }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
202 203 204 205
        /* Version chunk */
        put_tag(pb, "FVER");
        put_be32(pb, 4);
        put_be32(pb, 0xA2805140);
206
    }
207 208 209

    /* Common chunk */
    put_tag(pb, "COMM");
210
    put_be32(pb, aifc ? 24 : 18); /* size */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
211
    put_be16(pb, enc->channels);  /* Number of channels */
212 213

    aiff->frames = url_ftell(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
214
    put_be32(pb, 0);              /* Number of frames */
215 216

    if (!enc->bits_per_sample)
217 218 219 220 221 222 223 224
        enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
    if (!enc->bits_per_sample) {
        av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
        return -1;
    }
    if (!enc->block_align)
        enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;

225 226 227 228 229
    put_be16(pb, enc->bits_per_sample); /* Sample size */

    sample_rate = av_dbl2ext((double)enc->sample_rate);
    put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));

230
    if (aifc) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
231 232
        put_le32(pb, enc->codec_tag);
        put_be16(pb, 0);
233
    }
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

    /* Sound data chunk */
    put_tag(pb, "SSND");
    aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
    put_be32(pb, 0);                    /* Sound samples data size */
    put_be32(pb, 0);                    /* Data offset */
    put_be32(pb, 0);                    /* Block-size (block align) */

    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);

    /* Data is starting here */
    put_flush_packet(pb);

    return 0;
}

static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
{
252
    ByteIOContext *pb = s->pb;
253 254 255 256 257 258
    put_buffer(pb, pkt->data, pkt->size);
    return 0;
}

static int aiff_write_trailer(AVFormatContext *s)
{
259
    ByteIOContext *pb = s->pb;
260 261 262 263 264 265 266 267 268 269 270
    AIFFOutputContext *aiff = s->priv_data;
    AVCodecContext *enc = s->streams[0]->codec;

    /* Chunks sizes must be even */
    offset_t file_size, end_size;
    end_size = file_size = url_ftell(pb);
    if (file_size & 1) {
        put_byte(pb, 0);
        end_size++;
    }

271
    if (!url_is_streamed(s->pb)) {
272 273
        /* File length */
        url_fseek(pb, aiff->form, SEEK_SET);
274
        put_be32(pb, file_size - aiff->form - 4);
275 276 277

        /* Number of sample frames */
        url_fseek(pb, aiff->frames, SEEK_SET);
278
        put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
279 280 281

        /* Sound Data chunk size */
        url_fseek(pb, aiff->ssnd, SEEK_SET);
282
        put_be32(pb, file_size - aiff->ssnd - 4);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

        /* return to the end */
        url_fseek(pb, end_size, SEEK_SET);

        put_flush_packet(pb);
    }

    return 0;
}
#endif //CONFIG_MUXERS

static int aiff_probe(AVProbeData *p)
{
    /* check file header */
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
        return AVPROBE_SCORE_MAX;
    else
        return 0;
}

/* aiff input */
static int aiff_read_header(AVFormatContext *s,
                            AVFormatParameters *ap)
{
310 311
    int size, filesize;
    offset_t offset = 0;
312 313
    uint32_t tag;
    unsigned version = AIFF_C_VERSION1;
314
    ByteIOContext *pb = s->pb;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    AVStream * st = s->streams[0];

    /* check FORM header */
    filesize = get_tag(pb, &tag);
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
        return AVERROR_INVALIDDATA;

    /* AIFF data type */
    tag = get_le32(pb);
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
        version = AIFF;
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
        return AVERROR_INVALIDDATA;

    filesize -= 4;

    st = av_new_stream(s, 0);
    if (!st)
333
        return AVERROR(ENOMEM);
334 335 336 337 338 339 340 341 342 343

    while (filesize > 0) {
        /* parse different chunks */
        size = get_tag(pb, &tag);
        if (size < 0)
            return size;

        filesize -= size + 8;

        switch (tag) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
344 345
        case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
            /* Then for the complete header info */
346
            st->nb_frames = get_aiff_header(pb, st->codec, size, version);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
347 348 349 350 351 352 353 354 355
            if (st->nb_frames < 0)
                return st->nb_frames;
            if (offset > 0) // COMM is after SSND
                goto got_sound;
            break;
        case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
            version = get_be32(pb);
            break;
        case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
356
            get_meta(pb, s->title, sizeof(s->title), size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
357 358
            break;
        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
359
            get_meta(pb, s->author, sizeof(s->author), size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
360 361
            break;
        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
362
            get_meta(pb, s->copyright, sizeof(s->copyright), size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
363 364
            break;
        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
365
            get_meta(pb, s->comment, sizeof(s->comment), size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
366 367 368 369 370
            break;
        case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
            offset = get_be32(pb);      /* Offset of sound data */
            get_be32(pb);               /* BlockSize... don't care */
            offset += url_ftell(pb);    /* Compute absolute data offset */
371
            if (st->codec->block_align)    /* Assume COMM already parsed */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
372 373 374 375 376 377 378
                goto got_sound;
            if (url_is_streamed(pb)) {
                av_log(s, AV_LOG_ERROR, "file is not seekable\n");
                return -1;
            }
            url_fskip(pb, size - 8);
            break;
379
        case MKTAG('w', 'a', 'v', 'e'):
380 381
            if ((uint64_t)size > (1<<30))
                return -1;
382 383 384 385 386 387
            st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
            if (!st->codec->extradata)
                return AVERROR(ENOMEM);
            st->codec->extradata_size = size;
            get_buffer(pb, st->codec->extradata, size);
            break;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
388 389 390 391
        default: /* Jump */
            if (size & 1)   /* Always even aligned */
                size++;
            url_fskip (pb, size);
392 393 394
        }
    }

395 396 397 398
    if (!st->codec->block_align) {
        av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
        return -1;
    }
399 400 401 402 403 404 405 406

got_sound:
    /* Now positioned, get the sound data start and end */
    if (st->nb_frames)
        s->file_size = st->nb_frames * st->codec->block_align;

    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
    st->start_time = 0;
407 408
    st->duration = st->codec->frame_size ?
        st->nb_frames * st->codec->frame_size : st->nb_frames;
409 410

    /* Position the stream at the first block */
411
    url_fseek(pb, offset, SEEK_SET);
412 413 414 415 416 417 418 419 420

    return 0;
}

#define MAX_SIZE 4096

static int aiff_read_packet(AVFormatContext *s,
                            AVPacket *pkt)
{
421 422
    AVStream *st = s->streams[0];
    int res;
423 424

    /* End of stream may be reached */
425
    if (url_feof(s->pb))
426
        return AVERROR(EIO);
427 428

    /* Now for that packet */
429
    res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
430 431 432 433 434 435 436 437
    if (res < 0)
        return res;

    /* Only one stream in an AIFF file */
    pkt->stream_index = 0;
    return 0;
}

438 439
#ifdef CONFIG_AIFF_DEMUXER
AVInputFormat aiff_demuxer = {
440
    "aiff",
441
    NULL_IF_CONFIG_SMALL("Audio IFF"),
442 443 444 445
    0,
    aiff_probe,
    aiff_read_header,
    aiff_read_packet,
446
    NULL,
447
    pcm_read_seek,
448
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
449
};
450
#endif
451

452
#ifdef CONFIG_AIFF_MUXER
453
AVOutputFormat aiff_muxer = {
454
    "aiff",
455
    NULL_IF_CONFIG_SMALL("Audio IFF"),
456 457 458 459 460 461 462 463
    "audio/aiff",
    "aif,aiff,afc,aifc",
    sizeof(AIFFOutputContext),
    CODEC_ID_PCM_S16BE,
    CODEC_ID_NONE,
    aiff_write_header,
    aiff_write_packet,
    aiff_write_trailer,
464
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
465
};
466
#endif