00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "avformat.h"
00029 #include "internal.h"
00030 #include "riff.h"
00031 #include "isom.h"
00032 #include "libavutil/intreadwrite.h"
00033 #include "libavutil/intfloat.h"
00034 #include "libavutil/dict.h"
00035 #include "caf.h"
00036
00037 typedef struct {
00038 int bytes_per_packet;
00039 int frames_per_packet;
00040 int64_t num_bytes;
00041
00042 int64_t packet_cnt;
00043 int64_t frame_cnt;
00044
00045 int64_t data_start;
00046 int64_t data_size;
00047 } CaffContext;
00048
00049 static int probe(AVProbeData *p)
00050 {
00051 if (AV_RB32(p->buf) == MKBETAG('c','a','f','f') && AV_RB16(&p->buf[4]) == 1)
00052 return AVPROBE_SCORE_MAX;
00053 return 0;
00054 }
00055
00057 static int read_desc_chunk(AVFormatContext *s)
00058 {
00059 AVIOContext *pb = s->pb;
00060 CaffContext *caf = s->priv_data;
00061 AVStream *st;
00062 int flags;
00063
00064
00065 st = avformat_new_stream(s, NULL);
00066 if (!st)
00067 return AVERROR(ENOMEM);
00068
00069
00070 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00071 st->codec->sample_rate = av_int2double(avio_rb64(pb));
00072 st->codec->codec_tag = avio_rl32(pb);
00073 flags = avio_rb32(pb);
00074 caf->bytes_per_packet = avio_rb32(pb);
00075 st->codec->block_align = caf->bytes_per_packet;
00076 caf->frames_per_packet = avio_rb32(pb);
00077 st->codec->channels = avio_rb32(pb);
00078 st->codec->bits_per_coded_sample = avio_rb32(pb);
00079
00080
00081 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
00082 st->codec->bit_rate = (uint64_t)st->codec->sample_rate * (uint64_t)caf->bytes_per_packet * 8
00083 / (uint64_t)caf->frames_per_packet;
00084 } else {
00085 st->codec->bit_rate = 0;
00086 }
00087
00088
00089 if (st->codec->codec_tag == MKTAG('l','p','c','m'))
00090 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, (flags ^ 0x2) | 0x4);
00091 else
00092 st->codec->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codec->codec_tag);
00093 return 0;
00094 }
00095
00097 static int read_kuki_chunk(AVFormatContext *s, int64_t size)
00098 {
00099 AVIOContext *pb = s->pb;
00100 AVStream *st = s->streams[0];
00101
00102 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
00103 return -1;
00104
00105 if (st->codec->codec_id == CODEC_ID_AAC) {
00106
00107
00108
00109 int strt, skip;
00110 MOVAtom atom;
00111
00112 strt = avio_tell(pb);
00113 ff_mov_read_esds(s, pb, atom);
00114 skip = size - (avio_tell(pb) - strt);
00115 if (skip < 0 || !st->codec->extradata ||
00116 st->codec->codec_id != CODEC_ID_AAC) {
00117 av_log(s, AV_LOG_ERROR, "invalid AAC magic cookie\n");
00118 return AVERROR_INVALIDDATA;
00119 }
00120 avio_skip(pb, skip);
00121 } else if (st->codec->codec_id == CODEC_ID_ALAC) {
00122 #define ALAC_PREAMBLE 12
00123 #define ALAC_HEADER 36
00124 #define ALAC_NEW_KUKI 24
00125 if (size == ALAC_NEW_KUKI) {
00126 st->codec->extradata = av_mallocz(ALAC_HEADER + FF_INPUT_BUFFER_PADDING_SIZE);
00127 if (!st->codec->extradata)
00128 return AVERROR(ENOMEM);
00129 memcpy(st->codec->extradata, "\0\0\0\24alac", 8);
00130 avio_read(pb, st->codec->extradata + ALAC_HEADER - ALAC_NEW_KUKI, ALAC_NEW_KUKI);
00131 st->codec->extradata_size = ALAC_HEADER;
00132 } else {
00133 if (size < ALAC_PREAMBLE + ALAC_HEADER) {
00134 av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
00135 avio_skip(pb, size);
00136 return AVERROR_INVALIDDATA;
00137 }
00138 avio_skip(pb, ALAC_PREAMBLE);
00139 st->codec->extradata = av_mallocz(ALAC_HEADER + FF_INPUT_BUFFER_PADDING_SIZE);
00140 if (!st->codec->extradata)
00141 return AVERROR(ENOMEM);
00142 avio_read(pb, st->codec->extradata, ALAC_HEADER);
00143 st->codec->extradata_size = ALAC_HEADER;
00144 avio_skip(pb, size - ALAC_PREAMBLE - ALAC_HEADER);
00145 }
00146 } else {
00147 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00148 if (!st->codec->extradata)
00149 return AVERROR(ENOMEM);
00150 avio_read(pb, st->codec->extradata, size);
00151 st->codec->extradata_size = size;
00152 }
00153
00154 return 0;
00155 }
00156
00158 static int read_pakt_chunk(AVFormatContext *s, int64_t size)
00159 {
00160 AVIOContext *pb = s->pb;
00161 AVStream *st = s->streams[0];
00162 CaffContext *caf = s->priv_data;
00163 int64_t pos = 0, ccount;
00164 int num_packets, i;
00165
00166 ccount = avio_tell(pb);
00167
00168 num_packets = avio_rb64(pb);
00169 if (num_packets < 0 || INT32_MAX / sizeof(AVIndexEntry) < num_packets)
00170 return AVERROR_INVALIDDATA;
00171
00172 st->nb_frames = avio_rb64(pb);
00173 st->nb_frames += avio_rb32(pb);
00174 st->nb_frames += avio_rb32(pb);
00175
00176 st->duration = 0;
00177 for (i = 0; i < num_packets; i++) {
00178 av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
00179 pos += caf->bytes_per_packet ? caf->bytes_per_packet : ff_mp4_read_descr_len(pb);
00180 st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb);
00181 }
00182
00183 if (avio_tell(pb) - ccount != size) {
00184 av_log(s, AV_LOG_ERROR, "error reading packet table\n");
00185 return -1;
00186 }
00187
00188 caf->num_bytes = pos;
00189 return 0;
00190 }
00191
00193 static void read_info_chunk(AVFormatContext *s, int64_t size)
00194 {
00195 AVIOContext *pb = s->pb;
00196 unsigned int i;
00197 unsigned int nb_entries = avio_rb32(pb);
00198 for (i = 0; i < nb_entries; i++) {
00199 char key[32];
00200 char value[1024];
00201 avio_get_str(pb, INT_MAX, key, sizeof(key));
00202 avio_get_str(pb, INT_MAX, value, sizeof(value));
00203 av_dict_set(&s->metadata, key, value, 0);
00204 }
00205 }
00206
00207 static int read_header(AVFormatContext *s,
00208 AVFormatParameters *ap)
00209 {
00210 AVIOContext *pb = s->pb;
00211 CaffContext *caf = s->priv_data;
00212 AVStream *st;
00213 uint32_t tag = 0;
00214 int found_data, ret;
00215 int64_t size;
00216
00217 avio_skip(pb, 8);
00218
00219
00220 if (avio_rb32(pb) != MKBETAG('d','e','s','c')) {
00221 av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
00222 return AVERROR_INVALIDDATA;
00223 }
00224 size = avio_rb64(pb);
00225 if (size != 32)
00226 return AVERROR_INVALIDDATA;
00227
00228 ret = read_desc_chunk(s);
00229 if (ret)
00230 return ret;
00231 st = s->streams[0];
00232
00233
00234 found_data = 0;
00235 while (!url_feof(pb)) {
00236
00237
00238
00239 if (found_data && (caf->data_size < 0 || !pb->seekable))
00240 break;
00241
00242 tag = avio_rb32(pb);
00243 size = avio_rb64(pb);
00244 if (url_feof(pb))
00245 break;
00246
00247 switch (tag) {
00248 case MKBETAG('d','a','t','a'):
00249 avio_skip(pb, 4);
00250 caf->data_start = avio_tell(pb);
00251 caf->data_size = size < 0 ? -1 : size - 4;
00252 if (caf->data_size > 0 && pb->seekable)
00253 avio_skip(pb, caf->data_size);
00254 found_data = 1;
00255 break;
00256
00257
00258 case MKBETAG('k','u','k','i'):
00259 if (read_kuki_chunk(s, size))
00260 return AVERROR_INVALIDDATA;
00261 break;
00262
00263
00264 case MKBETAG('p','a','k','t'):
00265 if (read_pakt_chunk(s, size))
00266 return AVERROR_INVALIDDATA;
00267 break;
00268
00269 case MKBETAG('i','n','f','o'):
00270 read_info_chunk(s, size);
00271 break;
00272
00273 case MKBETAG('c','h','a','n'):
00274 if (size < 12)
00275 return AVERROR_INVALIDDATA;
00276 ff_mov_read_chan(s, size, st->codec);
00277 break;
00278
00279 default:
00280 #define _(x) ((x) >= ' ' ? (x) : ' ')
00281 av_log(s, AV_LOG_WARNING, "skipping CAF chunk: %08X (%c%c%c%c), size %"PRId64"\n",
00282 tag, _(tag>>24), _((tag>>16)&0xFF), _((tag>>8)&0xFF), _(tag&0xFF), size);
00283 #undef _
00284 case MKBETAG('f','r','e','e'):
00285 if (size < 0)
00286 return AVERROR_INVALIDDATA;
00287 avio_skip(pb, size);
00288 break;
00289 }
00290 }
00291
00292 if (!found_data)
00293 return AVERROR_INVALIDDATA;
00294
00295 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
00296 if (caf->data_size > 0)
00297 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
00298 } else if (st->nb_index_entries) {
00299 st->codec->bit_rate = st->codec->sample_rate * caf->data_size * 8 /
00300 st->duration;
00301 } else {
00302 av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
00303 "block size or frame size are variable.\n");
00304 return AVERROR_INVALIDDATA;
00305 }
00306
00307 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00308 st->start_time = 0;
00309
00310
00311 if (caf->data_size >= 0)
00312 avio_seek(pb, caf->data_start, SEEK_SET);
00313
00314 return 0;
00315 }
00316
00317 #define CAF_MAX_PKT_SIZE 4096
00318
00319 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00320 {
00321 AVIOContext *pb = s->pb;
00322 AVStream *st = s->streams[0];
00323 CaffContext *caf = s->priv_data;
00324 int res, pkt_size = 0, pkt_frames = 0;
00325 int64_t left = CAF_MAX_PKT_SIZE;
00326
00327 if (url_feof(pb))
00328 return AVERROR(EIO);
00329
00330
00331 if (caf->data_size > 0) {
00332 left = (caf->data_start + caf->data_size) - avio_tell(pb);
00333 if (left <= 0)
00334 return AVERROR(EIO);
00335 }
00336
00337 pkt_frames = caf->frames_per_packet;
00338 pkt_size = caf->bytes_per_packet;
00339
00340 if (pkt_size > 0 && pkt_frames == 1) {
00341 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
00342 pkt_size = FFMIN(pkt_size, left);
00343 pkt_frames = pkt_size / caf->bytes_per_packet;
00344 } else if (st->nb_index_entries) {
00345 if (caf->packet_cnt < st->nb_index_entries - 1) {
00346 pkt_size = st->index_entries[caf->packet_cnt + 1].pos - st->index_entries[caf->packet_cnt].pos;
00347 pkt_frames = st->index_entries[caf->packet_cnt + 1].timestamp - st->index_entries[caf->packet_cnt].timestamp;
00348 } else if (caf->packet_cnt == st->nb_index_entries - 1) {
00349 pkt_size = caf->num_bytes - st->index_entries[caf->packet_cnt].pos;
00350 pkt_frames = st->duration - st->index_entries[caf->packet_cnt].timestamp;
00351 } else {
00352 return AVERROR(EIO);
00353 }
00354 }
00355
00356 if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
00357 return AVERROR(EIO);
00358
00359 res = av_get_packet(pb, pkt, pkt_size);
00360 if (res < 0)
00361 return res;
00362
00363 pkt->size = res;
00364 pkt->stream_index = 0;
00365 pkt->dts = pkt->pts = caf->frame_cnt;
00366
00367 caf->packet_cnt++;
00368 caf->frame_cnt += pkt_frames;
00369
00370 return 0;
00371 }
00372
00373 static int read_seek(AVFormatContext *s, int stream_index,
00374 int64_t timestamp, int flags)
00375 {
00376 AVStream *st = s->streams[0];
00377 CaffContext *caf = s->priv_data;
00378 CaffContext caf2 = *caf;
00379 int64_t pos;
00380
00381 timestamp = FFMAX(timestamp, 0);
00382
00383 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
00384
00385 pos = caf->bytes_per_packet * timestamp / caf->frames_per_packet;
00386 if (caf->data_size > 0)
00387 pos = FFMIN(pos, caf->data_size);
00388 caf->packet_cnt = pos / caf->bytes_per_packet;
00389 caf->frame_cnt = caf->frames_per_packet * caf->packet_cnt;
00390 } else if (st->nb_index_entries) {
00391 caf->packet_cnt = av_index_search_timestamp(st, timestamp, flags);
00392 caf->frame_cnt = st->index_entries[caf->packet_cnt].timestamp;
00393 pos = st->index_entries[caf->packet_cnt].pos;
00394 } else {
00395 return -1;
00396 }
00397
00398 if (avio_seek(s->pb, pos + caf->data_start, SEEK_SET) < 0) {
00399 *caf = caf2;
00400 return -1;
00401 }
00402 return 0;
00403 }
00404
00405 AVInputFormat ff_caf_demuxer = {
00406 .name = "caf",
00407 .long_name = NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
00408 .priv_data_size = sizeof(CaffContext),
00409 .read_probe = probe,
00410 .read_header = read_header,
00411 .read_packet = read_packet,
00412 .read_seek = read_seek,
00413 .codec_tag = (const AVCodecTag*[]){ff_codec_caf_tags, 0},
00414 };