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