• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/au.c

Go to the documentation of this file.
00001 /*
00002  * AU muxer and demuxer
00003  * Copyright (c) 2001 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /*
00023  * First version by Francois Revol revol@free.fr
00024  *
00025  * Reference documents:
00026  * http://www.opengroup.org/public/pubs/external/auformat.html
00027  * http://www.goice.co.jp/member/mo/formats/au.html
00028  */
00029 
00030 #include "avformat.h"
00031 #include "internal.h"
00032 #include "avio_internal.h"
00033 #include "pcm.h"
00034 #include "riff.h"
00035 
00036 /* if we don't know the size in advance */
00037 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
00038 
00039 /* The libavcodec codecs we support, and the IDs they have in the file */
00040 static const AVCodecTag codec_au_tags[] = {
00041     { CODEC_ID_PCM_MULAW, 1 },
00042     { CODEC_ID_PCM_S8, 2 },
00043     { CODEC_ID_PCM_S16BE, 3 },
00044     { CODEC_ID_PCM_S24BE, 4 },
00045     { CODEC_ID_PCM_S32BE, 5 },
00046     { CODEC_ID_PCM_F32BE, 6 },
00047     { CODEC_ID_PCM_F64BE, 7 },
00048     { CODEC_ID_PCM_ALAW, 27 },
00049     { CODEC_ID_NONE, 0 },
00050 };
00051 
00052 #if CONFIG_AU_MUXER
00053 /* AUDIO_FILE header */
00054 static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
00055 {
00056     if(!enc->codec_tag)
00057         return -1;
00058     ffio_wfourcc(pb, ".snd");    /* magic number */
00059     avio_wb32(pb, 24);           /* header size */
00060     avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
00061     avio_wb32(pb, (uint32_t)enc->codec_tag);     /* codec ID */
00062     avio_wb32(pb, enc->sample_rate);
00063     avio_wb32(pb, (uint32_t)enc->channels);
00064     return 0;
00065 }
00066 
00067 static int au_write_header(AVFormatContext *s)
00068 {
00069     AVIOContext *pb = s->pb;
00070 
00071     s->priv_data = NULL;
00072 
00073     /* format header */
00074     if (put_au_header(pb, s->streams[0]->codec) < 0) {
00075         return -1;
00076     }
00077 
00078     avio_flush(pb);
00079 
00080     return 0;
00081 }
00082 
00083 static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
00084 {
00085     AVIOContext *pb = s->pb;
00086     avio_write(pb, pkt->data, pkt->size);
00087     return 0;
00088 }
00089 
00090 static int au_write_trailer(AVFormatContext *s)
00091 {
00092     AVIOContext *pb = s->pb;
00093     int64_t file_size;
00094 
00095     if (s->pb->seekable) {
00096 
00097         /* update file size */
00098         file_size = avio_tell(pb);
00099         avio_seek(pb, 8, SEEK_SET);
00100         avio_wb32(pb, (uint32_t)(file_size - 24));
00101         avio_seek(pb, file_size, SEEK_SET);
00102 
00103         avio_flush(pb);
00104     }
00105 
00106     return 0;
00107 }
00108 #endif /* CONFIG_AU_MUXER */
00109 
00110 static int au_probe(AVProbeData *p)
00111 {
00112     /* check file header */
00113     if (p->buf[0] == '.' && p->buf[1] == 's' &&
00114         p->buf[2] == 'n' && p->buf[3] == 'd')
00115         return AVPROBE_SCORE_MAX;
00116     else
00117         return 0;
00118 }
00119 
00120 /* au input */
00121 static int au_read_header(AVFormatContext *s,
00122                           AVFormatParameters *ap)
00123 {
00124     int size;
00125     unsigned int tag;
00126     AVIOContext *pb = s->pb;
00127     unsigned int id, channels, rate;
00128     enum CodecID codec;
00129     AVStream *st;
00130 
00131     /* check ".snd" header */
00132     tag = avio_rl32(pb);
00133     if (tag != MKTAG('.', 's', 'n', 'd'))
00134         return -1;
00135     size = avio_rb32(pb); /* header size */
00136     avio_rb32(pb); /* data size */
00137 
00138     id = avio_rb32(pb);
00139     rate = avio_rb32(pb);
00140     channels = avio_rb32(pb);
00141 
00142     codec = ff_codec_get_id(codec_au_tags, id);
00143 
00144     if (!av_get_bits_per_sample(codec)) {
00145         av_log_ask_for_sample(s, "could not determine bits per sample\n");
00146         return AVERROR_INVALIDDATA;
00147     }
00148 
00149     if (size >= 24) {
00150         /* skip unused data */
00151         avio_skip(pb, size - 24);
00152     }
00153 
00154     /* now we are ready: build format streams */
00155     st = avformat_new_stream(s, NULL);
00156     if (!st)
00157         return -1;
00158     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00159     st->codec->codec_tag = id;
00160     st->codec->codec_id = codec;
00161     st->codec->channels = channels;
00162     st->codec->sample_rate = rate;
00163     avpriv_set_pts_info(st, 64, 1, rate);
00164     return 0;
00165 }
00166 
00167 #define BLOCK_SIZE 1024
00168 
00169 static int au_read_packet(AVFormatContext *s,
00170                           AVPacket *pkt)
00171 {
00172     int ret;
00173 
00174     ret= av_get_packet(s->pb, pkt, BLOCK_SIZE *
00175                        s->streams[0]->codec->channels *
00176                        av_get_bits_per_sample(s->streams[0]->codec->codec_id) >> 3);
00177     if (ret < 0)
00178         return ret;
00179     pkt->stream_index = 0;
00180 
00181     /* note: we need to modify the packet size here to handle the last
00182        packet */
00183     pkt->size = ret;
00184     return 0;
00185 }
00186 
00187 #if CONFIG_AU_DEMUXER
00188 AVInputFormat ff_au_demuxer = {
00189     .name           = "au",
00190     .long_name      = NULL_IF_CONFIG_SMALL("SUN AU format"),
00191     .read_probe     = au_probe,
00192     .read_header    = au_read_header,
00193     .read_packet    = au_read_packet,
00194     .read_seek      = pcm_read_seek,
00195     .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
00196 };
00197 #endif
00198 
00199 #if CONFIG_AU_MUXER
00200 AVOutputFormat ff_au_muxer = {
00201     .name              = "au",
00202     .long_name         = NULL_IF_CONFIG_SMALL("SUN AU format"),
00203     .mime_type         = "audio/basic",
00204     .extensions        = "au",
00205     .audio_codec       = CODEC_ID_PCM_S16BE,
00206     .video_codec       = CODEC_ID_NONE,
00207     .write_header      = au_write_header,
00208     .write_packet      = au_write_packet,
00209     .write_trailer     = au_write_trailer,
00210     .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
00211 };
00212 #endif //CONFIG_AU_MUXER
Generated on Sat Mar 17 2012 12:57:53 for Libav by doxygen 1.7.1