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