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