libavformat/au.c
115329f1
 /*
7fbde343
  * AU muxer and demuxer
406792e7
  * Copyright (c) 2001 Fabrice Bellard
6cea494e
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
19720f15
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
6cea494e
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
6cea494e
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
6cea494e
  *
19720f15
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6cea494e
  */
 
 /*
  * First version by Francois Revol revol@free.fr
  *
  * Reference documents:
  * http://www.opengroup.org/public/pubs/external/auformat.html
  * http://www.goice.co.jp/member/mo/formats/au.html
  */
 
 #include "avformat.h"
0abdb293
 #include "avio_internal.h"
e94204df
 #include "pcm.h"
9d9f4119
 #include "riff.h"
6cea494e
 
 /* if we don't know the size in advance */
9ff85412
 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
6cea494e
 
 /* The ffmpeg codecs we support, and the IDs they have in the file */
7caf0cc6
 static const AVCodecTag codec_au_tags[] = {
6cea494e
     { CODEC_ID_PCM_MULAW, 1 },
c5adfd64
     { CODEC_ID_PCM_S8, 2 },
6cea494e
     { CODEC_ID_PCM_S16BE, 3 },
7b21690a
     { CODEC_ID_PCM_S24BE, 4 },
     { CODEC_ID_PCM_S32BE, 5 },
249f3243
     { CODEC_ID_PCM_F32BE, 6 },
7b21690a
     { CODEC_ID_PCM_F64BE, 7 },
6cea494e
     { CODEC_ID_PCM_ALAW, 27 },
bc2d2a07
     { CODEC_ID_NONE, 0 },
6cea494e
 };
 
b250f9c6
 #if CONFIG_AU_MUXER
6cea494e
 /* AUDIO_FILE header */
ae628ec1
 static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
6cea494e
 {
bd5a6020
     if(!enc->codec_tag)
6cea494e
         return -1;
0abdb293
     ffio_wfourcc(pb, ".snd");    /* magic number */
77eb5504
     avio_wb32(pb, 24);           /* header size */
     avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
     avio_wb32(pb, (uint32_t)enc->codec_tag);     /* codec ID */
     avio_wb32(pb, enc->sample_rate);
     avio_wb32(pb, (uint32_t)enc->channels);
6cea494e
     return 0;
 }
 
 static int au_write_header(AVFormatContext *s)
 {
ae628ec1
     AVIOContext *pb = s->pb;
6cea494e
 
     s->priv_data = NULL;
 
     /* format header */
01f4895c
     if (put_au_header(pb, s->streams[0]->codec) < 0) {
6cea494e
         return -1;
     }
 
b7f2fdde
     avio_flush(pb);
6cea494e
 
     return 0;
 }
 
e928649b
 static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
6cea494e
 {
ae628ec1
     AVIOContext *pb = s->pb;
77eb5504
     avio_write(pb, pkt->data, pkt->size);
6cea494e
     return 0;
 }
 
 static int au_write_trailer(AVFormatContext *s)
 {
ae628ec1
     AVIOContext *pb = s->pb;
bc5c918e
     int64_t file_size;
6cea494e
 
8978feda
     if (s->pb->seekable) {
6cea494e
 
         /* update file size */
a2704c97
         file_size = avio_tell(pb);
6b4aa5da
         avio_seek(pb, 8, SEEK_SET);
77eb5504
         avio_wb32(pb, (uint32_t)(file_size - 24));
6b4aa5da
         avio_seek(pb, file_size, SEEK_SET);
6cea494e
 
b7f2fdde
         avio_flush(pb);
6cea494e
     }
 
     return 0;
 }
8212568a
 #endif /* CONFIG_AU_MUXER */
6cea494e
 
c9a65ca8
 static int au_probe(AVProbeData *p)
 {
     /* check file header */
     if (p->buf[0] == '.' && p->buf[1] == 's' &&
         p->buf[2] == 'n' && p->buf[3] == 'd')
         return AVPROBE_SCORE_MAX;
     else
         return 0;
 }
 
6cea494e
 /* au input */
 static int au_read_header(AVFormatContext *s,
4986a429
                           AVFormatParameters *ap)
6cea494e
 {
     int size;
     unsigned int tag;
ae628ec1
     AVIOContext *pb = s->pb;
fb65d2ca
     unsigned int id, channels, rate;
     enum CodecID codec;
6cea494e
     AVStream *st;
 
     /* check ".snd" header */
b7effd4e
     tag = avio_rl32(pb);
6cea494e
     if (tag != MKTAG('.', 's', 'n', 'd'))
         return -1;
b7effd4e
     size = avio_rb32(pb); /* header size */
     avio_rb32(pb); /* data size */
115329f1
 
b7effd4e
     id = avio_rb32(pb);
     rate = avio_rb32(pb);
     channels = avio_rb32(pb);
115329f1
 
1a40491e
     codec = ff_codec_get_id(codec_au_tags, id);
6cea494e
 
0a624147
     if (!av_get_bits_per_sample(codec)) {
         av_log_ask_for_sample(s, "could not determine bits per sample\n");
         return AVERROR_INVALIDDATA;
     }
 
6cea494e
     if (size >= 24) {
         /* skip unused data */
45a8a02a
         avio_skip(pb, size - 24);
6cea494e
     }
 
     /* now we are ready: build format streams */
fa26a29d
     st = av_new_stream(s, 0);
6cea494e
     if (!st)
         return -1;
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01f4895c
     st->codec->codec_tag = id;
     st->codec->codec_id = codec;
     st->codec->channels = channels;
     st->codec->sample_rate = rate;
595bf4ef
     av_set_pts_info(st, 64, 1, rate);
6cea494e
     return 0;
 }
 
4da715cb
 #define BLOCK_SIZE 1024
6cea494e
 
 static int au_read_packet(AVFormatContext *s,
94ef6864
                           AVPacket *pkt)
6cea494e
 {
94ef6864
     int ret;
6cea494e
 
4da715cb
     ret= av_get_packet(s->pb, pkt, BLOCK_SIZE *
                        s->streams[0]->codec->channels *
                        av_get_bits_per_sample(s->streams[0]->codec->codec_id) >> 3);
2692067a
     if (ret < 0)
b46c98bf
         return ret;
6cea494e
     pkt->stream_index = 0;
 
     /* note: we need to modify the packet size here to handle the last
        packet */
     pkt->size = ret;
94ef6864
     return 0;
6cea494e
 }
 
b250f9c6
 #if CONFIG_AU_DEMUXER
c6610a21
 AVInputFormat ff_au_demuxer = {
c9a65ca8
     "au",
bde15e74
     NULL_IF_CONFIG_SMALL("SUN AU format"),
c9a65ca8
     0,
     au_probe,
     au_read_header,
     au_read_packet,
9b64a036
     NULL,
4986a429
     pcm_read_seek,
c1854592
     .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
c9a65ca8
 };
ff70e601
 #endif
c9a65ca8
 
b250f9c6
 #if CONFIG_AU_MUXER
c6610a21
 AVOutputFormat ff_au_muxer = {
6cea494e
     "au",
bde15e74
     NULL_IF_CONFIG_SMALL("SUN AU format"),
6cea494e
     "audio/basic",
     "au",
c9a65ca8
     0,
6cea494e
     CODEC_ID_PCM_S16BE,
     CODEC_ID_NONE,
     au_write_header,
     au_write_packet,
     au_write_trailer,
c1854592
     .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
6cea494e
 };
ff70e601
 #endif //CONFIG_AU_MUXER