00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "avformat.h"
00031 #include "raw.h"
00032 #include "riff.h"
00033
00034
00035 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
00036
00037
00038 static const AVCodecTag codec_au_tags[] = {
00039 { CODEC_ID_PCM_MULAW, 1 },
00040 { CODEC_ID_PCM_S8, 2 },
00041 { CODEC_ID_PCM_S16BE, 3 },
00042 { CODEC_ID_PCM_ALAW, 27 },
00043 { 0, 0 },
00044 };
00045
00046 #ifdef CONFIG_MUXERS
00047
00048 static int put_au_header(ByteIOContext *pb, AVCodecContext *enc)
00049 {
00050 if(!enc->codec_tag)
00051 return -1;
00052 put_tag(pb, ".snd");
00053 put_be32(pb, 24);
00054 put_be32(pb, AU_UNKNOWN_SIZE);
00055 put_be32(pb, (uint32_t)enc->codec_tag);
00056 put_be32(pb, enc->sample_rate);
00057 put_be32(pb, (uint32_t)enc->channels);
00058 return 0;
00059 }
00060
00061 static int au_write_header(AVFormatContext *s)
00062 {
00063 ByteIOContext *pb = s->pb;
00064
00065 s->priv_data = NULL;
00066
00067
00068 if (put_au_header(pb, s->streams[0]->codec) < 0) {
00069 return -1;
00070 }
00071
00072 put_flush_packet(pb);
00073
00074 return 0;
00075 }
00076
00077 static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
00078 {
00079 ByteIOContext *pb = s->pb;
00080 put_buffer(pb, pkt->data, pkt->size);
00081 return 0;
00082 }
00083
00084 static int au_write_trailer(AVFormatContext *s)
00085 {
00086 ByteIOContext *pb = s->pb;
00087 offset_t file_size;
00088
00089 if (!url_is_streamed(s->pb)) {
00090
00091
00092 file_size = url_ftell(pb);
00093 url_fseek(pb, 8, SEEK_SET);
00094 put_be32(pb, (uint32_t)(file_size - 24));
00095 url_fseek(pb, file_size, SEEK_SET);
00096
00097 put_flush_packet(pb);
00098 }
00099
00100 return 0;
00101 }
00102 #endif //CONFIG_MUXERS
00103
00104 static int au_probe(AVProbeData *p)
00105 {
00106
00107 if (p->buf[0] == '.' && p->buf[1] == 's' &&
00108 p->buf[2] == 'n' && p->buf[3] == 'd')
00109 return AVPROBE_SCORE_MAX;
00110 else
00111 return 0;
00112 }
00113
00114
00115 static int au_read_header(AVFormatContext *s,
00116 AVFormatParameters *ap)
00117 {
00118 int size;
00119 unsigned int tag;
00120 ByteIOContext *pb = s->pb;
00121 unsigned int id, codec, channels, rate;
00122 AVStream *st;
00123
00124
00125 tag = get_le32(pb);
00126 if (tag != MKTAG('.', 's', 'n', 'd'))
00127 return -1;
00128 size = get_be32(pb);
00129 get_be32(pb);
00130
00131 id = get_be32(pb);
00132 rate = get_be32(pb);
00133 channels = get_be32(pb);
00134
00135 codec = codec_get_id(codec_au_tags, id);
00136
00137 if (size >= 24) {
00138
00139 url_fseek(pb, size - 24, SEEK_CUR);
00140 }
00141
00142
00143 st = av_new_stream(s, 0);
00144 if (!st)
00145 return -1;
00146 st->codec->codec_type = CODEC_TYPE_AUDIO;
00147 st->codec->codec_tag = id;
00148 st->codec->codec_id = codec;
00149 st->codec->channels = channels;
00150 st->codec->sample_rate = rate;
00151 av_set_pts_info(st, 64, 1, rate);
00152 return 0;
00153 }
00154
00155 #define MAX_SIZE 4096
00156
00157 static int au_read_packet(AVFormatContext *s,
00158 AVPacket *pkt)
00159 {
00160 int ret;
00161
00162 if (url_feof(s->pb))
00163 return AVERROR(EIO);
00164 ret= av_get_packet(s->pb, pkt, MAX_SIZE);
00165 if (ret < 0)
00166 return AVERROR(EIO);
00167 pkt->stream_index = 0;
00168
00169
00170
00171 pkt->size = ret;
00172 return 0;
00173 }
00174
00175 static int au_read_close(AVFormatContext *s)
00176 {
00177 return 0;
00178 }
00179
00180 #ifdef CONFIG_AU_DEMUXER
00181 AVInputFormat au_demuxer = {
00182 "au",
00183 "SUN AU Format",
00184 0,
00185 au_probe,
00186 au_read_header,
00187 au_read_packet,
00188 au_read_close,
00189 pcm_read_seek,
00190 .codec_tag= (const AVCodecTag*[]){codec_au_tags, 0},
00191 };
00192 #endif
00193
00194 #ifdef CONFIG_AU_MUXER
00195 AVOutputFormat au_muxer = {
00196 "au",
00197 "SUN AU Format",
00198 "audio/basic",
00199 "au",
00200 0,
00201 CODEC_ID_PCM_S16BE,
00202 CODEC_ID_NONE,
00203 au_write_header,
00204 au_write_packet,
00205 au_write_trailer,
00206 .codec_tag= (const AVCodecTag*[]){codec_au_tags, 0},
00207 };
00208 #endif //CONFIG_AU_MUXER