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 {
00209 AVIOContext *pb = s->pb;
00210 CaffContext *caf = s->priv_data;
00211 AVStream *st;
00212 uint32_t tag = 0;
00213 int found_data, ret;
00214 int64_t size;
00215
00216 avio_skip(pb, 8);
00217
00218
00219 if (avio_rb32(pb) != MKBETAG('d','e','s','c')) {
00220 av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
00221 return AVERROR_INVALIDDATA;
00222 }
00223 size = avio_rb64(pb);
00224 if (size != 32)
00225 return AVERROR_INVALIDDATA;
00226
00227 ret = read_desc_chunk(s);
00228 if (ret)
00229 return ret;
00230 st = s->streams[0];
00231
00232
00233 found_data = 0;
00234 while (!url_feof(pb)) {
00235
00236
00237
00238 if (found_data && (caf->data_size < 0 || !pb->seekable))
00239 break;
00240
00241 tag = avio_rb32(pb);
00242 size = avio_rb64(pb);
00243 if (url_feof(pb))
00244 break;
00245
00246 switch (tag) {
00247 case MKBETAG('d','a','t','a'):
00248 avio_skip(pb, 4);
00249 caf->data_start = avio_tell(pb);
00250 caf->data_size = size < 0 ? -1 : size - 4;
00251 if (caf->data_size > 0 && pb->seekable)
00252 avio_skip(pb, caf->data_size);
00253 found_data = 1;
00254 break;
00255
00256
00257 case MKBETAG('k','u','k','i'):
00258 if (read_kuki_chunk(s, size))
00259 return AVERROR_INVALIDDATA;
00260 break;
00261
00262
00263 case MKBETAG('p','a','k','t'):
00264 if (read_pakt_chunk(s, size))
00265 return AVERROR_INVALIDDATA;
00266 break;
00267
00268 case MKBETAG('i','n','f','o'):
00269 read_info_chunk(s, size);
00270 break;
00271
00272 case MKBETAG('c','h','a','n'):
00273 if (size < 12)
00274 return AVERROR_INVALIDDATA;
00275 ff_mov_read_chan(s, size, st->codec);
00276 break;
00277
00278 default:
00279 #define _(x) ((x) >= ' ' ? (x) : ' ')
00280 av_log(s, AV_LOG_WARNING, "skipping CAF chunk: %08X (%c%c%c%c), size %"PRId64"\n",
00281 tag, _(tag>>24), _((tag>>16)&0xFF), _((tag>>8)&0xFF), _(tag&0xFF), size);
00282 #undef _
00283 case MKBETAG('f','r','e','e'):
00284 if (size < 0)
00285 return AVERROR_INVALIDDATA;
00286 avio_skip(pb, size);
00287 break;
00288 }
00289 }
00290
00291 if (!found_data)
00292 return AVERROR_INVALIDDATA;
00293
00294 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
00295 if (caf->data_size > 0)
00296 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
00297 } else if (st->nb_index_entries) {
00298 st->codec->bit_rate = st->codec->sample_rate * caf->data_size * 8 /
00299 st->duration;
00300 } else {
00301 av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
00302 "block size or frame size are variable.\n");
00303 return AVERROR_INVALIDDATA;
00304 }
00305
00306 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00307 st->start_time = 0;
00308
00309
00310 if (caf->data_size >= 0)
00311 avio_seek(pb, caf->data_start, SEEK_SET);
00312
00313 return 0;
00314 }
00315
00316 #define CAF_MAX_PKT_SIZE 4096
00317
00318 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00319 {
00320 AVIOContext *pb = s->pb;
00321 AVStream *st = s->streams[0];
00322 CaffContext *caf = s->priv_data;
00323 int res, pkt_size = 0, pkt_frames = 0;
00324 int64_t left = CAF_MAX_PKT_SIZE;
00325
00326 if (url_feof(pb))
00327 return AVERROR(EIO);
00328
00329
00330 if (caf->data_size > 0) {
00331 left = (caf->data_start + caf->data_size) - avio_tell(pb);
00332 if (left <= 0)
00333 return AVERROR(EIO);
00334 }
00335
00336 pkt_frames = caf->frames_per_packet;
00337 pkt_size = caf->bytes_per_packet;
00338
00339 if (pkt_size > 0 && pkt_frames == 1) {
00340 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
00341 pkt_size = FFMIN(pkt_size, left);
00342 pkt_frames = pkt_size / caf->bytes_per_packet;
00343 } else if (st->nb_index_entries) {
00344 if (caf->packet_cnt < st->nb_index_entries - 1) {
00345 pkt_size = st->index_entries[caf->packet_cnt + 1].pos - st->index_entries[caf->packet_cnt].pos;
00346 pkt_frames = st->index_entries[caf->packet_cnt + 1].timestamp - st->index_entries[caf->packet_cnt].timestamp;
00347 } else if (caf->packet_cnt == st->nb_index_entries - 1) {
00348 pkt_size = caf->num_bytes - st->index_entries[caf->packet_cnt].pos;
00349 pkt_frames = st->duration - st->index_entries[caf->packet_cnt].timestamp;
00350 } else {
00351 return AVERROR(EIO);
00352 }
00353 }
00354
00355 if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
00356 return AVERROR(EIO);
00357
00358 res = av_get_packet(pb, pkt, pkt_size);
00359 if (res < 0)
00360 return res;
00361
00362 pkt->size = res;
00363 pkt->stream_index = 0;
00364 pkt->dts = pkt->pts = caf->frame_cnt;
00365
00366 caf->packet_cnt++;
00367 caf->frame_cnt += pkt_frames;
00368
00369 return 0;
00370 }
00371
00372 static int read_seek(AVFormatContext *s, int stream_index,
00373 int64_t timestamp, int flags)
00374 {
00375 AVStream *st = s->streams[0];
00376 CaffContext *caf = s->priv_data;
00377 int64_t pos, packet_cnt, frame_cnt;
00378
00379 timestamp = FFMAX(timestamp, 0);
00380
00381 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
00382
00383 pos = caf->bytes_per_packet * timestamp / caf->frames_per_packet;
00384 if (caf->data_size > 0)
00385 pos = FFMIN(pos, caf->data_size);
00386 packet_cnt = pos / caf->bytes_per_packet;
00387 frame_cnt = caf->frames_per_packet * packet_cnt;
00388 } else if (st->nb_index_entries) {
00389 packet_cnt = av_index_search_timestamp(st, timestamp, flags);
00390 frame_cnt = st->index_entries[packet_cnt].timestamp;
00391 pos = st->index_entries[packet_cnt].pos;
00392 } else {
00393 return -1;
00394 }
00395
00396 if (avio_seek(s->pb, pos + caf->data_start, SEEK_SET) < 0)
00397 return -1;
00398
00399 caf->packet_cnt = packet_cnt;
00400 caf->frame_cnt = frame_cnt;
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 };