00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intfloat_readwrite.h"
00023 #include "libavutil/dict.h"
00024 #include "avformat.h"
00025 #include "pcm.h"
00026 #include "aiff.h"
00027 #include "isom.h"
00028
00029 #define AIFF 0
00030 #define AIFF_C_VERSION1 0xA2805140
00031
00032 typedef struct {
00033 int64_t data_end;
00034 } AIFFInputContext;
00035
00036 static enum CodecID aiff_codec_get_id(int bps)
00037 {
00038 if (bps <= 8)
00039 return CODEC_ID_PCM_S8;
00040 if (bps <= 16)
00041 return CODEC_ID_PCM_S16BE;
00042 if (bps <= 24)
00043 return CODEC_ID_PCM_S24BE;
00044 if (bps <= 32)
00045 return CODEC_ID_PCM_S32BE;
00046
00047
00048 return CODEC_ID_NONE;
00049 }
00050
00051
00052 static int get_tag(AVIOContext *pb, uint32_t * tag)
00053 {
00054 int size;
00055
00056 if (url_feof(pb))
00057 return AVERROR(EIO);
00058
00059 *tag = avio_rl32(pb);
00060 size = avio_rb32(pb);
00061
00062 if (size < 0)
00063 size = 0x7fffffff;
00064
00065 return size;
00066 }
00067
00068
00069 static void get_meta(AVFormatContext *s, const char *key, int size)
00070 {
00071 uint8_t *str = av_malloc(size+1);
00072
00073 if (str) {
00074 int res = avio_read(s->pb, str, size);
00075 if (res < 0){
00076 av_free(str);
00077 return;
00078 }
00079 size += (size&1)-res;
00080 str[res] = 0;
00081 av_dict_set(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
00082 }else
00083 size+= size&1;
00084
00085 avio_skip(s->pb, size);
00086 }
00087
00088
00089 static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
00090 int size, unsigned version)
00091 {
00092 AVExtFloat ext;
00093 double sample_rate;
00094 unsigned int num_frames;
00095
00096 if (size & 1)
00097 size++;
00098 codec->codec_type = AVMEDIA_TYPE_AUDIO;
00099 codec->channels = avio_rb16(pb);
00100 num_frames = avio_rb32(pb);
00101 codec->bits_per_coded_sample = avio_rb16(pb);
00102
00103 avio_read(pb, (uint8_t*)&ext, sizeof(ext));
00104 sample_rate = av_ext2dbl(ext);
00105 codec->sample_rate = sample_rate;
00106 size -= 18;
00107
00108
00109 if (version == AIFF_C_VERSION1) {
00110 codec->codec_tag = avio_rl32(pb);
00111 codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
00112
00113 switch (codec->codec_id) {
00114 case CODEC_ID_PCM_S16BE:
00115 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00116 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00117 break;
00118 case CODEC_ID_ADPCM_IMA_QT:
00119 codec->block_align = 34*codec->channels;
00120 codec->frame_size = 64;
00121 break;
00122 case CODEC_ID_MACE3:
00123 codec->block_align = 2*codec->channels;
00124 codec->frame_size = 6;
00125 break;
00126 case CODEC_ID_MACE6:
00127 codec->block_align = 1*codec->channels;
00128 codec->frame_size = 6;
00129 break;
00130 case CODEC_ID_GSM:
00131 codec->block_align = 33;
00132 codec->frame_size = 160;
00133 break;
00134 case CODEC_ID_QCELP:
00135 codec->block_align = 35;
00136 codec->frame_size= 160;
00137 break;
00138 default:
00139 break;
00140 }
00141 size -= 4;
00142 } else {
00143
00144 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00145 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00146 }
00147
00148
00149
00150 if (!codec->block_align)
00151 codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
00152
00153 codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
00154 codec->sample_rate) * (codec->block_align << 3);
00155
00156
00157 if (size)
00158 avio_skip(pb, size);
00159
00160 return num_frames;
00161 }
00162
00163 static int aiff_probe(AVProbeData *p)
00164 {
00165
00166 if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
00167 p->buf[2] == 'R' && p->buf[3] == 'M' &&
00168 p->buf[8] == 'A' && p->buf[9] == 'I' &&
00169 p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
00170 return AVPROBE_SCORE_MAX;
00171 else
00172 return 0;
00173 }
00174
00175
00176 static int aiff_read_header(AVFormatContext *s,
00177 AVFormatParameters *ap)
00178 {
00179 int size, filesize;
00180 int64_t offset = 0;
00181 uint32_t tag;
00182 unsigned version = AIFF_C_VERSION1;
00183 AVIOContext *pb = s->pb;
00184 AVStream * st;
00185 AIFFInputContext *aiff = s->priv_data;
00186
00187
00188 filesize = get_tag(pb, &tag);
00189 if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
00190 return AVERROR_INVALIDDATA;
00191
00192
00193 tag = avio_rl32(pb);
00194 if (tag == MKTAG('A', 'I', 'F', 'F'))
00195 version = AIFF;
00196 else if (tag != MKTAG('A', 'I', 'F', 'C'))
00197 return AVERROR_INVALIDDATA;
00198
00199 filesize -= 4;
00200
00201 st = av_new_stream(s, 0);
00202 if (!st)
00203 return AVERROR(ENOMEM);
00204
00205 while (filesize > 0) {
00206
00207 size = get_tag(pb, &tag);
00208 if (size < 0)
00209 return size;
00210
00211 filesize -= size + 8;
00212
00213 switch (tag) {
00214 case MKTAG('C', 'O', 'M', 'M'):
00215
00216 st->nb_frames = get_aiff_header(pb, st->codec, size, version);
00217 if (st->nb_frames < 0)
00218 return st->nb_frames;
00219 if (offset > 0)
00220 goto got_sound;
00221 break;
00222 case MKTAG('F', 'V', 'E', 'R'):
00223 version = avio_rb32(pb);
00224 break;
00225 case MKTAG('N', 'A', 'M', 'E'):
00226 get_meta(s, "title" , size);
00227 break;
00228 case MKTAG('A', 'U', 'T', 'H'):
00229 get_meta(s, "author" , size);
00230 break;
00231 case MKTAG('(', 'c', ')', ' '):
00232 get_meta(s, "copyright", size);
00233 break;
00234 case MKTAG('A', 'N', 'N', 'O'):
00235 get_meta(s, "comment" , size);
00236 break;
00237 case MKTAG('S', 'S', 'N', 'D'):
00238 aiff->data_end = avio_tell(pb) + size;
00239 offset = avio_rb32(pb);
00240 avio_rb32(pb);
00241 offset += avio_tell(pb);
00242 if (st->codec->block_align)
00243 goto got_sound;
00244 if (!pb->seekable) {
00245 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
00246 return -1;
00247 }
00248 avio_skip(pb, size - 8);
00249 break;
00250 case MKTAG('w', 'a', 'v', 'e'):
00251 if ((uint64_t)size > (1<<30))
00252 return -1;
00253 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00254 if (!st->codec->extradata)
00255 return AVERROR(ENOMEM);
00256 st->codec->extradata_size = size;
00257 avio_read(pb, st->codec->extradata, size);
00258 break;
00259 case MKTAG('C','H','A','N'):
00260 if (size < 12)
00261 return AVERROR_INVALIDDATA;
00262 ff_mov_read_chan(s, size, st->codec);
00263 break;
00264 default:
00265 if (size & 1)
00266 size++;
00267 avio_skip(pb, size);
00268 }
00269 }
00270
00271 if (!st->codec->block_align) {
00272 av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
00273 return -1;
00274 }
00275
00276 got_sound:
00277
00278 if (st->nb_frames)
00279 s->file_size = st->nb_frames * st->codec->block_align;
00280
00281 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00282 st->start_time = 0;
00283 st->duration = st->codec->frame_size ?
00284 st->nb_frames * st->codec->frame_size : st->nb_frames;
00285
00286
00287 avio_seek(pb, offset, SEEK_SET);
00288
00289 return 0;
00290 }
00291
00292 #define MAX_SIZE 4096
00293
00294 static int aiff_read_packet(AVFormatContext *s,
00295 AVPacket *pkt)
00296 {
00297 AVStream *st = s->streams[0];
00298 AIFFInputContext *aiff = s->priv_data;
00299 int64_t max_size;
00300 int res, size;
00301
00302
00303 max_size = aiff->data_end - avio_tell(s->pb);
00304 if (max_size <= 0)
00305 return AVERROR_EOF;
00306
00307
00308 if (st->codec->block_align >= 33)
00309 size = st->codec->block_align;
00310 else
00311 size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
00312 size = FFMIN(max_size, size);
00313 res = av_get_packet(s->pb, pkt, size);
00314 if (res < 0)
00315 return res;
00316
00317
00318 pkt->stream_index = 0;
00319 return 0;
00320 }
00321
00322 AVInputFormat ff_aiff_demuxer = {
00323 "aiff",
00324 NULL_IF_CONFIG_SMALL("Audio IFF"),
00325 sizeof(AIFFInputContext),
00326 aiff_probe,
00327 aiff_read_header,
00328 aiff_read_packet,
00329 NULL,
00330 pcm_read_seek,
00331 .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
00332 };