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 "internal.h"
00032 #include "avio_internal.h"
00033 #include "pcm.h"
00034 #include "riff.h"
00035
00036
00037 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
00038
00039
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
00054 static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
00055 {
00056 if(!enc->codec_tag)
00057 return -1;
00058 ffio_wfourcc(pb, ".snd");
00059 avio_wb32(pb, 24);
00060 avio_wb32(pb, AU_UNKNOWN_SIZE);
00061 avio_wb32(pb, (uint32_t)enc->codec_tag);
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
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
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
00109
00110 static int au_probe(AVProbeData *p)
00111 {
00112
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
00121 static int au_read_header(AVFormatContext *s,
00122 AVFormatParameters *ap)
00123 {
00124 int size, bps, data_size = 0;
00125 unsigned int tag;
00126 AVIOContext *pb = s->pb;
00127 unsigned int id, channels, rate;
00128 enum CodecID codec;
00129 AVStream *st;
00130
00131
00132 tag = avio_rl32(pb);
00133 if (tag != MKTAG('.', 's', 'n', 'd'))
00134 return -1;
00135 size = avio_rb32(pb);
00136 data_size = avio_rb32(pb);
00137
00138 if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
00139 av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
00140 return AVERROR_INVALIDDATA;
00141 }
00142
00143 id = avio_rb32(pb);
00144 rate = avio_rb32(pb);
00145 channels = avio_rb32(pb);
00146
00147 codec = ff_codec_get_id(codec_au_tags, id);
00148
00149 if (!(bps = av_get_bits_per_sample(codec))) {
00150 av_log_ask_for_sample(s, "could not determine bits per sample\n");
00151 return AVERROR_INVALIDDATA;
00152 }
00153
00154 if (size >= 24) {
00155
00156 avio_skip(pb, size - 24);
00157 }
00158
00159
00160 st = avformat_new_stream(s, NULL);
00161 if (!st)
00162 return -1;
00163 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00164 st->codec->codec_tag = id;
00165 st->codec->codec_id = codec;
00166 st->codec->channels = channels;
00167 st->codec->sample_rate = rate;
00168 if (data_size != AU_UNKNOWN_SIZE)
00169 st->duration = (((int64_t)data_size)<<3) / (st->codec->channels * bps);
00170 avpriv_set_pts_info(st, 64, 1, rate);
00171 return 0;
00172 }
00173
00174 #define BLOCK_SIZE 1024
00175
00176 static int au_read_packet(AVFormatContext *s,
00177 AVPacket *pkt)
00178 {
00179 int ret;
00180
00181 ret= av_get_packet(s->pb, pkt, BLOCK_SIZE *
00182 s->streams[0]->codec->channels *
00183 av_get_bits_per_sample(s->streams[0]->codec->codec_id) >> 3);
00184 if (ret < 0)
00185 return ret;
00186 pkt->stream_index = 0;
00187
00188
00189
00190 pkt->size = ret;
00191 return 0;
00192 }
00193
00194 #if CONFIG_AU_DEMUXER
00195 AVInputFormat ff_au_demuxer = {
00196 .name = "au",
00197 .long_name = NULL_IF_CONFIG_SMALL("SUN AU format"),
00198 .read_probe = au_probe,
00199 .read_header = au_read_header,
00200 .read_packet = au_read_packet,
00201 .read_seek = pcm_read_seek,
00202 .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
00203 };
00204 #endif
00205
00206 #if CONFIG_AU_MUXER
00207 AVOutputFormat ff_au_muxer = {
00208 .name = "au",
00209 .long_name = NULL_IF_CONFIG_SMALL("SUN AU format"),
00210 .mime_type = "audio/basic",
00211 .extensions = "au",
00212 .audio_codec = CODEC_ID_PCM_S16BE,
00213 .video_codec = CODEC_ID_NONE,
00214 .write_header = au_write_header,
00215 .write_packet = au_write_packet,
00216 .write_trailer = au_write_trailer,
00217 .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
00218 };
00219 #endif //CONFIG_AU_MUXER