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 "avformat.h"
00024 #include "raw.h"
00025 #include "riff.h"
00026
00027 static const AVCodecTag codec_aiff_tags[] = {
00028 { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
00029 { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
00030 { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
00031 { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
00032 { CODEC_ID_PCM_F32BE, MKTAG('f','l','3','2') },
00033 { CODEC_ID_PCM_F64BE, MKTAG('f','l','6','4') },
00034 { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
00035 { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
00036 { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
00037 { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
00038 { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
00039 { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
00040 { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
00041 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
00042 { CODEC_ID_QDM2, MKTAG('Q','D','M','2') },
00043 { 0, 0 },
00044 };
00045
00046 #define AIFF 0
00047 #define AIFF_C_VERSION1 0xA2805140
00048
00049 static enum CodecID aiff_codec_get_id(int bps)
00050 {
00051 if (bps <= 8)
00052 return CODEC_ID_PCM_S8;
00053 if (bps <= 16)
00054 return CODEC_ID_PCM_S16BE;
00055 if (bps <= 24)
00056 return CODEC_ID_PCM_S24BE;
00057 if (bps <= 32)
00058 return CODEC_ID_PCM_S32BE;
00059
00060
00061 return CODEC_ID_NONE;
00062 }
00063
00064
00065 static int get_tag(ByteIOContext *pb, uint32_t * tag)
00066 {
00067 int size;
00068
00069 if (url_feof(pb))
00070 return AVERROR(EIO);
00071
00072 *tag = get_le32(pb);
00073 size = get_be32(pb);
00074
00075 if (size < 0)
00076 size = 0x7fffffff;
00077
00078 return size;
00079 }
00080
00081
00082 static void get_meta(AVFormatContext *s, const char *key, int size)
00083 {
00084 uint8_t str[1024];
00085 int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
00086 if (res < 0)
00087 return;
00088
00089 str[res] = 0;
00090 if (size & 1)
00091 size++;
00092 size -= res;
00093 if (size)
00094 url_fskip(s->pb, size);
00095
00096 av_metadata_set(&s->metadata, key, str);
00097 }
00098
00099
00100 static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
00101 int size, unsigned version)
00102 {
00103 AVExtFloat ext;
00104 double sample_rate;
00105 unsigned int num_frames;
00106
00107 if (size & 1)
00108 size++;
00109 codec->codec_type = CODEC_TYPE_AUDIO;
00110 codec->channels = get_be16(pb);
00111 num_frames = get_be32(pb);
00112 codec->bits_per_coded_sample = get_be16(pb);
00113
00114 get_buffer(pb, (uint8_t*)&ext, sizeof(ext));
00115 sample_rate = av_ext2dbl(ext);
00116 codec->sample_rate = sample_rate;
00117 size -= 18;
00118
00119
00120 if (version == AIFF_C_VERSION1) {
00121 codec->codec_tag = get_le32(pb);
00122 codec->codec_id = codec_get_id(codec_aiff_tags, codec->codec_tag);
00123
00124 switch (codec->codec_id) {
00125 case CODEC_ID_PCM_S16BE:
00126 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00127 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00128 break;
00129 case CODEC_ID_ADPCM_IMA_QT:
00130 codec->block_align = 34*codec->channels;
00131 codec->frame_size = 64;
00132 break;
00133 case CODEC_ID_MACE3:
00134 codec->block_align = 2*codec->channels;
00135 codec->frame_size = 6;
00136 break;
00137 case CODEC_ID_MACE6:
00138 codec->block_align = 1*codec->channels;
00139 codec->frame_size = 6;
00140 break;
00141 case CODEC_ID_GSM:
00142 codec->block_align = 33;
00143 codec->frame_size = 160;
00144 break;
00145 default:
00146 break;
00147 }
00148 size -= 4;
00149 } else {
00150
00151 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00152 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00153 }
00154
00155
00156
00157 if (!codec->block_align)
00158 codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
00159
00160 codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
00161 codec->sample_rate) * (codec->block_align << 3);
00162
00163
00164 if (size)
00165 url_fseek(pb, size, SEEK_CUR);
00166
00167 return num_frames;
00168 }
00169
00170 #if CONFIG_AIFF_MUXER
00171 typedef struct {
00172 int64_t form;
00173 int64_t frames;
00174 int64_t ssnd;
00175 } AIFFOutputContext;
00176
00177 static int aiff_write_header(AVFormatContext *s)
00178 {
00179 AIFFOutputContext *aiff = s->priv_data;
00180 ByteIOContext *pb = s->pb;
00181 AVCodecContext *enc = s->streams[0]->codec;
00182 AVExtFloat sample_rate;
00183 int aifc = 0;
00184
00185
00186 if (!enc->codec_tag)
00187 return -1;
00188 if (enc->codec_tag != MKTAG('N','O','N','E'))
00189 aifc = 1;
00190
00191
00192 put_tag(pb, "FORM");
00193 aiff->form = url_ftell(pb);
00194 put_be32(pb, 0);
00195 put_tag(pb, aifc ? "AIFC" : "AIFF");
00196
00197 if (aifc) {
00198 enc->bits_per_coded_sample = 16;
00199 if (!enc->block_align) {
00200 av_log(s, AV_LOG_ERROR, "block align not set\n");
00201 return -1;
00202 }
00203
00204 put_tag(pb, "FVER");
00205 put_be32(pb, 4);
00206 put_be32(pb, 0xA2805140);
00207 }
00208
00209
00210 put_tag(pb, "COMM");
00211 put_be32(pb, aifc ? 24 : 18);
00212 put_be16(pb, enc->channels);
00213
00214 aiff->frames = url_ftell(pb);
00215 put_be32(pb, 0);
00216
00217 if (!enc->bits_per_coded_sample)
00218 enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
00219 if (!enc->bits_per_coded_sample) {
00220 av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
00221 return -1;
00222 }
00223 if (!enc->block_align)
00224 enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
00225
00226 put_be16(pb, enc->bits_per_coded_sample);
00227
00228 sample_rate = av_dbl2ext((double)enc->sample_rate);
00229 put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
00230
00231 if (aifc) {
00232 put_le32(pb, enc->codec_tag);
00233 put_be16(pb, 0);
00234 }
00235
00236
00237 put_tag(pb, "SSND");
00238 aiff->ssnd = url_ftell(pb);
00239 put_be32(pb, 0);
00240 put_be32(pb, 0);
00241 put_be32(pb, 0);
00242
00243 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00244
00245
00246 put_flush_packet(pb);
00247
00248 return 0;
00249 }
00250
00251 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
00252 {
00253 ByteIOContext *pb = s->pb;
00254 put_buffer(pb, pkt->data, pkt->size);
00255 return 0;
00256 }
00257
00258 static int aiff_write_trailer(AVFormatContext *s)
00259 {
00260 ByteIOContext *pb = s->pb;
00261 AIFFOutputContext *aiff = s->priv_data;
00262 AVCodecContext *enc = s->streams[0]->codec;
00263
00264
00265 int64_t file_size, end_size;
00266 end_size = file_size = url_ftell(pb);
00267 if (file_size & 1) {
00268 put_byte(pb, 0);
00269 end_size++;
00270 }
00271
00272 if (!url_is_streamed(s->pb)) {
00273
00274 url_fseek(pb, aiff->form, SEEK_SET);
00275 put_be32(pb, file_size - aiff->form - 4);
00276
00277
00278 url_fseek(pb, aiff->frames, SEEK_SET);
00279 put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
00280
00281
00282 url_fseek(pb, aiff->ssnd, SEEK_SET);
00283 put_be32(pb, file_size - aiff->ssnd - 4);
00284
00285
00286 url_fseek(pb, end_size, SEEK_SET);
00287
00288 put_flush_packet(pb);
00289 }
00290
00291 return 0;
00292 }
00293 #endif
00294
00295 static int aiff_probe(AVProbeData *p)
00296 {
00297
00298 if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
00299 p->buf[2] == 'R' && p->buf[3] == 'M' &&
00300 p->buf[8] == 'A' && p->buf[9] == 'I' &&
00301 p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
00302 return AVPROBE_SCORE_MAX;
00303 else
00304 return 0;
00305 }
00306
00307
00308 static int aiff_read_header(AVFormatContext *s,
00309 AVFormatParameters *ap)
00310 {
00311 int size, filesize;
00312 int64_t offset = 0;
00313 uint32_t tag;
00314 unsigned version = AIFF_C_VERSION1;
00315 ByteIOContext *pb = s->pb;
00316 AVStream * st = s->streams[0];
00317
00318
00319 filesize = get_tag(pb, &tag);
00320 if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
00321 return AVERROR_INVALIDDATA;
00322
00323
00324 tag = get_le32(pb);
00325 if (tag == MKTAG('A', 'I', 'F', 'F'))
00326 version = AIFF;
00327 else if (tag != MKTAG('A', 'I', 'F', 'C'))
00328 return AVERROR_INVALIDDATA;
00329
00330 filesize -= 4;
00331
00332 st = av_new_stream(s, 0);
00333 if (!st)
00334 return AVERROR(ENOMEM);
00335
00336 while (filesize > 0) {
00337
00338 size = get_tag(pb, &tag);
00339 if (size < 0)
00340 return size;
00341
00342 filesize -= size + 8;
00343
00344 switch (tag) {
00345 case MKTAG('C', 'O', 'M', 'M'):
00346
00347 st->nb_frames = get_aiff_header(pb, st->codec, size, version);
00348 if (st->nb_frames < 0)
00349 return st->nb_frames;
00350 if (offset > 0)
00351 goto got_sound;
00352 break;
00353 case MKTAG('F', 'V', 'E', 'R'):
00354 version = get_be32(pb);
00355 break;
00356 case MKTAG('N', 'A', 'M', 'E'):
00357 get_meta(s, "title" , size);
00358 break;
00359 case MKTAG('A', 'U', 'T', 'H'):
00360 get_meta(s, "author" , size);
00361 break;
00362 case MKTAG('(', 'c', ')', ' '):
00363 get_meta(s, "copyright", size);
00364 break;
00365 case MKTAG('A', 'N', 'N', 'O'):
00366 get_meta(s, "comment" , size);
00367 break;
00368 case MKTAG('S', 'S', 'N', 'D'):
00369 offset = get_be32(pb);
00370 get_be32(pb);
00371 offset += url_ftell(pb);
00372 if (st->codec->block_align)
00373 goto got_sound;
00374 if (url_is_streamed(pb)) {
00375 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
00376 return -1;
00377 }
00378 url_fskip(pb, size - 8);
00379 break;
00380 case MKTAG('w', 'a', 'v', 'e'):
00381 if ((uint64_t)size > (1<<30))
00382 return -1;
00383 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00384 if (!st->codec->extradata)
00385 return AVERROR(ENOMEM);
00386 st->codec->extradata_size = size;
00387 get_buffer(pb, st->codec->extradata, size);
00388 break;
00389 default:
00390 if (size & 1)
00391 size++;
00392 url_fskip (pb, size);
00393 }
00394 }
00395
00396 if (!st->codec->block_align) {
00397 av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
00398 return -1;
00399 }
00400
00401 got_sound:
00402
00403 if (st->nb_frames)
00404 s->file_size = st->nb_frames * st->codec->block_align;
00405
00406 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00407 st->start_time = 0;
00408 st->duration = st->codec->frame_size ?
00409 st->nb_frames * st->codec->frame_size : st->nb_frames;
00410
00411
00412 url_fseek(pb, offset, SEEK_SET);
00413
00414 return 0;
00415 }
00416
00417 #define MAX_SIZE 4096
00418
00419 static int aiff_read_packet(AVFormatContext *s,
00420 AVPacket *pkt)
00421 {
00422 AVStream *st = s->streams[0];
00423 int res;
00424
00425
00426 if (url_feof(s->pb))
00427 return AVERROR(EIO);
00428
00429
00430 res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
00431 if (res < 0)
00432 return res;
00433
00434
00435 pkt->stream_index = 0;
00436 return 0;
00437 }
00438
00439 #if CONFIG_AIFF_DEMUXER
00440 AVInputFormat aiff_demuxer = {
00441 "aiff",
00442 NULL_IF_CONFIG_SMALL("Audio IFF"),
00443 0,
00444 aiff_probe,
00445 aiff_read_header,
00446 aiff_read_packet,
00447 NULL,
00448 pcm_read_seek,
00449 .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
00450 };
00451 #endif
00452
00453 #if CONFIG_AIFF_MUXER
00454 AVOutputFormat aiff_muxer = {
00455 "aiff",
00456 NULL_IF_CONFIG_SMALL("Audio IFF"),
00457 "audio/aiff",
00458 "aif,aiff,afc,aifc",
00459 sizeof(AIFFOutputContext),
00460 CODEC_ID_PCM_S16BE,
00461 CODEC_ID_NONE,
00462 aiff_write_header,
00463 aiff_write_packet,
00464 aiff_write_trailer,
00465 .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
00466 };
00467 #endif