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
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 (pb->eof_reached)
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 int res;
00073
00074 if (!str) {
00075 avio_skip(s->pb, size);
00076 return;
00077 }
00078
00079 res = avio_read(s->pb, str, size);
00080 if (res < 0)
00081 return;
00082
00083 str[res] = 0;
00084 av_dict_set(&s->metadata, key, str, AV_DICT_DONT_STRDUP_VAL);
00085 }
00086
00087
00088 static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
00089 int size, unsigned version)
00090 {
00091 int exp;
00092 uint64_t val;
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 exp = avio_rb16(pb);
00104 val = avio_rb64(pb);
00105 sample_rate = ldexp(val, exp - 16383 - 63);
00106 codec->sample_rate = sample_rate;
00107 size -= 18;
00108
00109
00110 if (version == AIFF_C_VERSION1) {
00111 codec->codec_tag = avio_rl32(pb);
00112 codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
00113
00114 switch (codec->codec_id) {
00115 case CODEC_ID_PCM_S16BE:
00116 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00117 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00118 break;
00119 case CODEC_ID_ADPCM_IMA_QT:
00120 codec->block_align = 34*codec->channels;
00121 codec->frame_size = 64;
00122 break;
00123 case CODEC_ID_MACE3:
00124 codec->block_align = 2*codec->channels;
00125 codec->frame_size = 6;
00126 break;
00127 case CODEC_ID_MACE6:
00128 codec->block_align = 1*codec->channels;
00129 codec->frame_size = 6;
00130 break;
00131 case CODEC_ID_GSM:
00132 codec->block_align = 33;
00133 codec->frame_size = 160;
00134 break;
00135 case CODEC_ID_QCELP:
00136 codec->block_align = 35;
00137 codec->frame_size= 160;
00138 break;
00139 default:
00140 break;
00141 }
00142 size -= 4;
00143 } else {
00144
00145 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00146 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00147 }
00148
00149
00150
00151 if (!codec->block_align)
00152 codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
00153
00154 codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
00155 codec->sample_rate) * (codec->block_align << 3);
00156
00157
00158 if (size)
00159 avio_skip(pb, size);
00160
00161 return num_frames;
00162 }
00163
00164 static int aiff_probe(AVProbeData *p)
00165 {
00166
00167 if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
00168 p->buf[2] == 'R' && p->buf[3] == 'M' &&
00169 p->buf[8] == 'A' && p->buf[9] == 'I' &&
00170 p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
00171 return AVPROBE_SCORE_MAX;
00172 else
00173 return 0;
00174 }
00175
00176
00177 static int aiff_read_header(AVFormatContext *s,
00178 AVFormatParameters *ap)
00179 {
00180 int size, filesize;
00181 int64_t offset = 0;
00182 uint32_t tag;
00183 unsigned version = AIFF_C_VERSION1;
00184 AVIOContext *pb = s->pb;
00185 AVStream * st;
00186 AIFFInputContext *aiff = s->priv_data;
00187
00188
00189 filesize = get_tag(pb, &tag);
00190 if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
00191 return AVERROR_INVALIDDATA;
00192
00193
00194 tag = avio_rl32(pb);
00195 if (tag == MKTAG('A', 'I', 'F', 'F'))
00196 version = AIFF;
00197 else if (tag != MKTAG('A', 'I', 'F', 'C'))
00198 return AVERROR_INVALIDDATA;
00199
00200 filesize -= 4;
00201
00202 st = avformat_new_stream(s, NULL);
00203 if (!st)
00204 return AVERROR(ENOMEM);
00205
00206 while (filesize > 0) {
00207
00208 size = get_tag(pb, &tag);
00209 if (size < 0)
00210 return size;
00211
00212 filesize -= size + 8;
00213
00214 switch (tag) {
00215 case MKTAG('C', 'O', 'M', 'M'):
00216
00217 st->nb_frames = get_aiff_header(pb, st->codec, size, version);
00218 if (st->nb_frames < 0)
00219 return st->nb_frames;
00220 if (offset > 0)
00221 goto got_sound;
00222 break;
00223 case MKTAG('F', 'V', 'E', 'R'):
00224 version = avio_rb32(pb);
00225 break;
00226 case MKTAG('N', 'A', 'M', 'E'):
00227 get_meta(s, "title" , size);
00228 break;
00229 case MKTAG('A', 'U', 'T', 'H'):
00230 get_meta(s, "author" , size);
00231 break;
00232 case MKTAG('(', 'c', ')', ' '):
00233 get_meta(s, "copyright", size);
00234 break;
00235 case MKTAG('A', 'N', 'N', 'O'):
00236 get_meta(s, "comment" , size);
00237 break;
00238 case MKTAG('S', 'S', 'N', 'D'):
00239 aiff->data_end = avio_tell(pb) + size;
00240 offset = avio_rb32(pb);
00241 avio_rb32(pb);
00242 offset += avio_tell(pb);
00243 if (st->codec->block_align)
00244 goto got_sound;
00245 if (!pb->seekable) {
00246 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
00247 return -1;
00248 }
00249 avio_skip(pb, size - 8);
00250 break;
00251 case MKTAG('w', 'a', 'v', 'e'):
00252 if ((uint64_t)size > (1<<30))
00253 return -1;
00254 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00255 if (!st->codec->extradata)
00256 return AVERROR(ENOMEM);
00257 st->codec->extradata_size = size;
00258 avio_read(pb, st->codec->extradata, size);
00259 break;
00260 default:
00261 if (size & 1)
00262 size++;
00263 avio_skip(pb, size);
00264 }
00265 }
00266
00267 got_sound:
00268 if (!st->codec->block_align) {
00269 av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
00270 return -1;
00271 }
00272
00273
00274 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00275 st->start_time = 0;
00276 st->duration = st->codec->frame_size ?
00277 st->nb_frames * st->codec->frame_size : st->nb_frames;
00278
00279
00280 avio_seek(pb, offset, SEEK_SET);
00281
00282 return 0;
00283 }
00284
00285 #define MAX_SIZE 4096
00286
00287 static int aiff_read_packet(AVFormatContext *s,
00288 AVPacket *pkt)
00289 {
00290 AVStream *st = s->streams[0];
00291 AIFFInputContext *aiff = s->priv_data;
00292 int64_t max_size;
00293 int res, size;
00294
00295
00296 max_size = aiff->data_end - avio_tell(s->pb);
00297 if (max_size <= 0)
00298 return AVERROR_EOF;
00299
00300
00301 if (st->codec->block_align >= 33)
00302 size = st->codec->block_align;
00303 else
00304 size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
00305 size = FFMIN(max_size, size);
00306 res = av_get_packet(s->pb, pkt, size);
00307 if (res < 0)
00308 return res;
00309
00310
00311 pkt->stream_index = 0;
00312 return 0;
00313 }
00314
00315 AVInputFormat ff_aiff_demuxer = {
00316 .name = "aiff",
00317 .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
00318 .priv_data_size = sizeof(AIFFInputContext),
00319 .read_probe = aiff_probe,
00320 .read_header = aiff_read_header,
00321 .read_packet = aiff_read_packet,
00322 .read_seek = pcm_read_seek,
00323 .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
00324 };