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