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 "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 } AIFFInputContext;
00036
00037 static enum CodecID aiff_codec_get_id(int bps)
00038 {
00039 if (bps <= 8)
00040 return CODEC_ID_PCM_S8;
00041 if (bps <= 16)
00042 return CODEC_ID_PCM_S16BE;
00043 if (bps <= 24)
00044 return CODEC_ID_PCM_S24BE;
00045 if (bps <= 32)
00046 return CODEC_ID_PCM_S32BE;
00047
00048
00049 return CODEC_ID_NONE;
00050 }
00051
00052
00053 static int get_tag(AVIOContext *pb, uint32_t * tag)
00054 {
00055 int size;
00056
00057 if (url_feof(pb))
00058 return AVERROR(EIO);
00059
00060 *tag = avio_rl32(pb);
00061 size = avio_rb32(pb);
00062
00063 if (size < 0)
00064 size = 0x7fffffff;
00065
00066 return size;
00067 }
00068
00069
00070 static void get_meta(AVFormatContext *s, const char *key, int size)
00071 {
00072 uint8_t *str = av_malloc(size+1);
00073
00074 if (str) {
00075 int res = avio_read(s->pb, str, size);
00076 if (res < 0){
00077 av_free(str);
00078 return;
00079 }
00080 size += (size&1)-res;
00081 str[res] = 0;
00082 av_dict_set(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
00083 }else
00084 size+= size&1;
00085
00086 avio_skip(s->pb, size);
00087 }
00088
00089
00090 static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
00091 int size, unsigned version)
00092 {
00093 AVExtFloat ext;
00094 double sample_rate;
00095 unsigned int num_frames;
00096
00097 if (size & 1)
00098 size++;
00099 codec->codec_type = AVMEDIA_TYPE_AUDIO;
00100 codec->channels = avio_rb16(pb);
00101 num_frames = avio_rb32(pb);
00102 codec->bits_per_coded_sample = avio_rb16(pb);
00103
00104 avio_read(pb, (uint8_t*)&ext, sizeof(ext));
00105 sample_rate = av_ext2dbl(ext);
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 case MKTAG('C','H','A','N'):
00261 if (size < 12)
00262 return AVERROR_INVALIDDATA;
00263 ff_mov_read_chan(s, size, st->codec);
00264 break;
00265 default:
00266 if (size & 1)
00267 size++;
00268 avio_skip(pb, size);
00269 }
00270 }
00271
00272 if (!st->codec->block_align) {
00273 av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
00274 return -1;
00275 }
00276
00277 got_sound:
00278
00279 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00280 st->start_time = 0;
00281 st->duration = st->codec->frame_size ?
00282 st->nb_frames * st->codec->frame_size : st->nb_frames;
00283
00284
00285 avio_seek(pb, offset, SEEK_SET);
00286
00287 return 0;
00288 }
00289
00290 #define MAX_SIZE 4096
00291
00292 static int aiff_read_packet(AVFormatContext *s,
00293 AVPacket *pkt)
00294 {
00295 AVStream *st = s->streams[0];
00296 AIFFInputContext *aiff = s->priv_data;
00297 int64_t max_size;
00298 int res, size;
00299
00300
00301 max_size = aiff->data_end - avio_tell(s->pb);
00302 if (max_size <= 0)
00303 return AVERROR_EOF;
00304
00305
00306 if (st->codec->block_align >= 33)
00307 size = st->codec->block_align;
00308 else
00309 size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
00310 size = FFMIN(max_size, size);
00311 res = av_get_packet(s->pb, pkt, size);
00312 if (res < 0)
00313 return res;
00314
00315
00316 pkt->stream_index = 0;
00317 return 0;
00318 }
00319
00320 AVInputFormat ff_aiff_demuxer = {
00321 .name = "aiff",
00322 .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
00323 .priv_data_size = sizeof(AIFFInputContext),
00324 .read_probe = aiff_probe,
00325 .read_header = aiff_read_header,
00326 .read_packet = aiff_read_packet,
00327 .read_seek = pcm_read_seek,
00328 .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
00329 };