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