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