1 /*
2 * Core Audio Format demuxer
3 * Copyright (c) 2007 Justin Ruggles
4 * Copyright (c) 2009 Peter Ross
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Core Audio Format demuxer
26 */
27
28 #include <inttypes.h>
29
30 #include "avformat.h"
31 #include "demux.h"
32 #include "internal.h"
33 #include "isom.h"
34 #include "mov_chan.h"
35 #include "libavcodec/flac.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/intfloat.h"
38 #include "libavutil/dict.h"
39 #include "caf.h"
40
41 typedef struct CafContext {
42 int bytes_per_packet; ///< bytes in a packet, or 0 if variable
43 int frames_per_packet; ///< frames in a packet, or 0 if variable
44 int64_t num_bytes; ///< total number of bytes in stream
45
46 int64_t packet_cnt; ///< packet counter
47 int64_t frame_cnt; ///< frame counter
48
49 int64_t data_start; ///< data start position, in bytes
50 int64_t data_size; ///< raw data size, in bytes
51 } CafContext;
52
probe(const AVProbeData * p)53 static int probe(const AVProbeData *p)
54 {
55 if (AV_RB32(p->buf) == MKBETAG('c','a','f','f') && AV_RB16(&p->buf[4]) == 1)
56 return AVPROBE_SCORE_MAX;
57 return 0;
58 }
59
60 /** Read audio description chunk */
read_desc_chunk(AVFormatContext * s)61 static int read_desc_chunk(AVFormatContext *s)
62 {
63 AVIOContext *pb = s->pb;
64 CafContext *caf = s->priv_data;
65 AVStream *st;
66 int flags;
67
68 /* new audio stream */
69 st = avformat_new_stream(s, NULL);
70 if (!st)
71 return AVERROR(ENOMEM);
72
73 /* parse format description */
74 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
75 st->codecpar->sample_rate = av_clipd(av_int2double(avio_rb64(pb)), 0, INT_MAX);
76 st->codecpar->codec_tag = avio_rl32(pb);
77 flags = avio_rb32(pb);
78 caf->bytes_per_packet = avio_rb32(pb);
79 st->codecpar->block_align = caf->bytes_per_packet;
80 caf->frames_per_packet = avio_rb32(pb);
81 st->codecpar->ch_layout.nb_channels = avio_rb32(pb);
82 st->codecpar->bits_per_coded_sample = avio_rb32(pb);
83
84 if (caf->bytes_per_packet < 0 || caf->frames_per_packet < 0 || st->codecpar->ch_layout.nb_channels < 0)
85 return AVERROR_INVALIDDATA;
86
87 /* calculate bit rate for constant size packets */
88 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
89 st->codecpar->bit_rate = (uint64_t)st->codecpar->sample_rate * (uint64_t)caf->bytes_per_packet * 8
90 / (uint64_t)caf->frames_per_packet;
91 } else {
92 st->codecpar->bit_rate = 0;
93 }
94
95 /* determine codec */
96 if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
97 st->codecpar->codec_id = ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample, (flags ^ 0x2) | 0x4);
98 else
99 st->codecpar->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codecpar->codec_tag);
100 return 0;
101 }
102
103 /** Read magic cookie chunk */
read_kuki_chunk(AVFormatContext * s,int64_t size)104 static int read_kuki_chunk(AVFormatContext *s, int64_t size)
105 {
106 AVIOContext *pb = s->pb;
107 AVStream *st = s->streams[0];
108 int ret;
109
110 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 return -1;
112
113 if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
114 /* The magic cookie format for AAC is an mp4 esds atom.
115 The lavc AAC decoder requires the data from the codec specific
116 description as extradata input. */
117 int strt, skip;
118
119 strt = avio_tell(pb);
120 ff_mov_read_esds(s, pb);
121 skip = size - (avio_tell(pb) - strt);
122 if (skip < 0 || !st->codecpar->extradata ||
123 st->codecpar->codec_id != AV_CODEC_ID_AAC) {
124 av_log(s, AV_LOG_ERROR, "invalid AAC magic cookie\n");
125 return AVERROR_INVALIDDATA;
126 }
127 avio_skip(pb, skip);
128 } else if (st->codecpar->codec_id == AV_CODEC_ID_ALAC) {
129 #define ALAC_PREAMBLE 12
130 #define ALAC_HEADER 36
131 #define ALAC_NEW_KUKI 24
132 uint8_t preamble[12];
133 if (size < ALAC_NEW_KUKI) {
134 av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
135 avio_skip(pb, size);
136 return AVERROR_INVALIDDATA;
137 }
138 if (avio_read(pb, preamble, ALAC_PREAMBLE) != ALAC_PREAMBLE) {
139 av_log(s, AV_LOG_ERROR, "failed to read preamble\n");
140 return AVERROR_INVALIDDATA;
141 }
142
143 if ((ret = ff_alloc_extradata(st->codecpar, ALAC_HEADER)) < 0)
144 return ret;
145
146 /* For the old style cookie, we skip 12 bytes, then read 36 bytes.
147 * The new style cookie only contains the last 24 bytes of what was
148 * 36 bytes in the old style cookie, so we fabricate the first 12 bytes
149 * in that case to maintain compatibility. */
150 if (!memcmp(&preamble[4], "frmaalac", 8)) {
151 if (size < ALAC_PREAMBLE + ALAC_HEADER) {
152 av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
153 av_freep(&st->codecpar->extradata);
154 return AVERROR_INVALIDDATA;
155 }
156 if (avio_read(pb, st->codecpar->extradata, ALAC_HEADER) != ALAC_HEADER) {
157 av_log(s, AV_LOG_ERROR, "failed to read kuki header\n");
158 av_freep(&st->codecpar->extradata);
159 return AVERROR_INVALIDDATA;
160 }
161 avio_skip(pb, size - ALAC_PREAMBLE - ALAC_HEADER);
162 } else {
163 AV_WB32(st->codecpar->extradata, 36);
164 memcpy(&st->codecpar->extradata[4], "alac", 4);
165 AV_WB32(&st->codecpar->extradata[8], 0);
166 memcpy(&st->codecpar->extradata[12], preamble, 12);
167 if (avio_read(pb, &st->codecpar->extradata[24], ALAC_NEW_KUKI - 12) != ALAC_NEW_KUKI - 12) {
168 av_log(s, AV_LOG_ERROR, "failed to read new kuki header\n");
169 av_freep(&st->codecpar->extradata);
170 return AVERROR_INVALIDDATA;
171 }
172 avio_skip(pb, size - ALAC_NEW_KUKI);
173 }
174 } else if (st->codecpar->codec_id == AV_CODEC_ID_FLAC) {
175 int last, type, flac_metadata_size;
176 uint8_t buf[4];
177 /* The magic cookie format for FLAC consists mostly of an mp4 dfLa atom. */
178 if (size < (16 + FLAC_STREAMINFO_SIZE)) {
179 av_log(s, AV_LOG_ERROR, "invalid FLAC magic cookie\n");
180 return AVERROR_INVALIDDATA;
181 }
182 /* Check cookie version. */
183 if (avio_r8(pb) != 0) {
184 av_log(s, AV_LOG_ERROR, "unknown FLAC magic cookie\n");
185 return AVERROR_INVALIDDATA;
186 }
187 avio_rb24(pb); /* Flags */
188 /* read dfLa fourcc */
189 if (avio_read(pb, buf, 4) != 4) {
190 av_log(s, AV_LOG_ERROR, "failed to read FLAC magic cookie\n");
191 return pb->error < 0 ? pb->error : AVERROR_INVALIDDATA;
192 }
193 if (memcmp(buf, "dfLa", 4)) {
194 av_log(s, AV_LOG_ERROR, "invalid FLAC magic cookie\n");
195 return AVERROR_INVALIDDATA;
196 }
197 /* Check dfLa version. */
198 if (avio_r8(pb) != 0) {
199 av_log(s, AV_LOG_ERROR, "unknown dfLa version\n");
200 return AVERROR_INVALIDDATA;
201 }
202 avio_rb24(pb); /* Flags */
203 if (avio_read(pb, buf, sizeof(buf)) != sizeof(buf)) {
204 av_log(s, AV_LOG_ERROR, "failed to read FLAC metadata block header\n");
205 return pb->error < 0 ? pb->error : AVERROR_INVALIDDATA;
206 }
207 flac_parse_block_header(buf, &last, &type, &flac_metadata_size);
208 if (type != FLAC_METADATA_TYPE_STREAMINFO || flac_metadata_size != FLAC_STREAMINFO_SIZE) {
209 av_log(s, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
210 return AVERROR_INVALIDDATA;
211 }
212 ret = ff_get_extradata(s, st->codecpar, pb, FLAC_STREAMINFO_SIZE);
213 if (ret < 0)
214 return ret;
215 if (!last)
216 av_log(s, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
217 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
218 // The data layout for Opus is currently unknown, so we do not export
219 // extradata at all. Multichannel streams are not supported.
220 if (st->codecpar->ch_layout.nb_channels > 2) {
221 avpriv_request_sample(s, "multichannel Opus in CAF");
222 return AVERROR_PATCHWELCOME;
223 }
224 avio_skip(pb, size);
225 } else if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0) {
226 return ret;
227 }
228
229 return 0;
230 }
231
232 /** Read packet table chunk */
read_pakt_chunk(AVFormatContext * s,int64_t size)233 static int read_pakt_chunk(AVFormatContext *s, int64_t size)
234 {
235 AVIOContext *pb = s->pb;
236 AVStream *st = s->streams[0];
237 CafContext *caf = s->priv_data;
238 int64_t pos = 0, ccount, num_packets;
239 int i;
240 int ret;
241
242 ccount = avio_tell(pb);
243
244 num_packets = avio_rb64(pb);
245 if (num_packets < 0 || INT32_MAX / sizeof(AVIndexEntry) < num_packets)
246 return AVERROR_INVALIDDATA;
247
248 st->nb_frames = avio_rb64(pb); /* valid frames */
249 st->nb_frames += avio_rb32(pb); /* priming frames */
250 st->nb_frames += avio_rb32(pb); /* remainder frames */
251
252 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
253 st->duration = caf->frames_per_packet * num_packets;
254 pos = caf-> bytes_per_packet * num_packets;
255 } else {
256 st->duration = 0;
257 for (i = 0; i < num_packets; i++) {
258 if (avio_feof(pb))
259 return AVERROR_INVALIDDATA;
260 ret = av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
261 if (ret < 0)
262 return ret;
263 pos += caf->bytes_per_packet ? caf->bytes_per_packet : ff_mp4_read_descr_len(pb);
264 st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb);
265 }
266 }
267
268 if (avio_tell(pb) - ccount > size) {
269 av_log(s, AV_LOG_ERROR, "error reading packet table\n");
270 return AVERROR_INVALIDDATA;
271 }
272 avio_seek(pb, ccount + size, SEEK_SET);
273
274 caf->num_bytes = pos;
275 return 0;
276 }
277
278 /** Read information chunk */
read_info_chunk(AVFormatContext * s,int64_t size)279 static void read_info_chunk(AVFormatContext *s, int64_t size)
280 {
281 AVIOContext *pb = s->pb;
282 unsigned int i;
283 unsigned int nb_entries = avio_rb32(pb);
284 for (i = 0; i < nb_entries && !avio_feof(pb); i++) {
285 char key[32];
286 char value[1024];
287 avio_get_str(pb, INT_MAX, key, sizeof(key));
288 avio_get_str(pb, INT_MAX, value, sizeof(value));
289 if (!*key)
290 continue;
291 av_dict_set(&s->metadata, key, value, 0);
292 }
293 }
294
read_header(AVFormatContext * s)295 static int read_header(AVFormatContext *s)
296 {
297 AVIOContext *pb = s->pb;
298 CafContext *caf = s->priv_data;
299 AVStream *st;
300 uint32_t tag = 0;
301 int found_data, ret;
302 int64_t size, pos;
303
304 avio_skip(pb, 8); /* magic, version, file flags */
305
306 /* audio description chunk */
307 if (avio_rb32(pb) != MKBETAG('d','e','s','c')) {
308 av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
309 return AVERROR_INVALIDDATA;
310 }
311 size = avio_rb64(pb);
312 if (size != 32)
313 return AVERROR_INVALIDDATA;
314
315 ret = read_desc_chunk(s);
316 if (ret)
317 return ret;
318 st = s->streams[0];
319
320 /* parse each chunk */
321 found_data = 0;
322 while (!avio_feof(pb)) {
323
324 /* stop at data chunk if seeking is not supported or
325 data chunk size is unknown */
326 if (found_data && (caf->data_size < 0 || !(pb->seekable & AVIO_SEEKABLE_NORMAL)))
327 break;
328
329 tag = avio_rb32(pb);
330 size = avio_rb64(pb);
331 pos = avio_tell(pb);
332 if (avio_feof(pb))
333 break;
334
335 switch (tag) {
336 case MKBETAG('d','a','t','a'):
337 avio_skip(pb, 4); /* edit count */
338 caf->data_start = avio_tell(pb);
339 caf->data_size = size < 0 ? -1 : size - 4;
340 if (caf->data_size > 0 && (pb->seekable & AVIO_SEEKABLE_NORMAL))
341 avio_skip(pb, caf->data_size);
342 found_data = 1;
343 break;
344
345 case MKBETAG('c','h','a','n'):
346 if ((ret = ff_mov_read_chan(s, s->pb, st, size)) < 0)
347 return ret;
348 break;
349
350 /* magic cookie chunk */
351 case MKBETAG('k','u','k','i'):
352 if (read_kuki_chunk(s, size))
353 return AVERROR_INVALIDDATA;
354 break;
355
356 /* packet table chunk */
357 case MKBETAG('p','a','k','t'):
358 if (read_pakt_chunk(s, size))
359 return AVERROR_INVALIDDATA;
360 break;
361
362 case MKBETAG('i','n','f','o'):
363 read_info_chunk(s, size);
364 break;
365
366 default:
367 av_log(s, AV_LOG_WARNING,
368 "skipping CAF chunk: %08"PRIX32" (%s), size %"PRId64"\n",
369 tag, av_fourcc2str(av_bswap32(tag)), size);
370 case MKBETAG('f','r','e','e'):
371 if (size < 0 && found_data)
372 goto found_data;
373 if (size < 0)
374 return AVERROR_INVALIDDATA;
375 break;
376 }
377
378 if (size > 0 && (pb->seekable & AVIO_SEEKABLE_NORMAL)) {
379 if (pos > INT64_MAX - size)
380 return AVERROR_INVALIDDATA;
381 avio_seek(pb, pos + size, SEEK_SET);
382 }
383 }
384
385 if (!found_data)
386 return AVERROR_INVALIDDATA;
387
388 found_data:
389 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
390 if (caf->data_size > 0 && caf->data_size / caf->bytes_per_packet < INT64_MAX / caf->frames_per_packet)
391 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
392 } else if (ffstream(st)->nb_index_entries && st->duration > 0) {
393 if (st->codecpar->sample_rate && caf->data_size / st->duration > INT64_MAX / st->codecpar->sample_rate / 8) {
394 av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * 8 * %"PRId64"\n",
395 st->codecpar->sample_rate, caf->data_size / st->duration);
396 return AVERROR_INVALIDDATA;
397 }
398 st->codecpar->bit_rate = st->codecpar->sample_rate * 8LL *
399 (caf->data_size / st->duration);
400 } else {
401 av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
402 "block size or frame size are variable.\n");
403 return AVERROR_INVALIDDATA;
404 }
405
406 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
407 st->start_time = 0;
408
409 /* position the stream at the start of data */
410 if (caf->data_size >= 0)
411 avio_seek(pb, caf->data_start, SEEK_SET);
412
413 return 0;
414 }
415
416 #define CAF_MAX_PKT_SIZE 4096
417
read_packet(AVFormatContext * s,AVPacket * pkt)418 static int read_packet(AVFormatContext *s, AVPacket *pkt)
419 {
420 AVIOContext *pb = s->pb;
421 AVStream *st = s->streams[0];
422 FFStream *const sti = ffstream(st);
423 CafContext *caf = s->priv_data;
424 int res, pkt_size = 0, pkt_frames = 0;
425 int64_t left = CAF_MAX_PKT_SIZE;
426
427 if (avio_feof(pb))
428 return AVERROR_EOF;
429
430 /* don't read past end of data chunk */
431 if (caf->data_size > 0) {
432 left = (caf->data_start + caf->data_size) - avio_tell(pb);
433 if (!left)
434 return AVERROR_EOF;
435 if (left < 0)
436 return AVERROR(EIO);
437 }
438
439 pkt_frames = caf->frames_per_packet;
440 pkt_size = caf->bytes_per_packet;
441
442 if (pkt_size > 0 && pkt_frames == 1) {
443 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
444 pkt_size = FFMIN(pkt_size, left);
445 pkt_frames = pkt_size / caf->bytes_per_packet;
446 } else if (sti->nb_index_entries) {
447 if (caf->packet_cnt < sti->nb_index_entries - 1) {
448 pkt_size = sti->index_entries[caf->packet_cnt + 1].pos - sti->index_entries[caf->packet_cnt].pos;
449 pkt_frames = sti->index_entries[caf->packet_cnt + 1].timestamp - sti->index_entries[caf->packet_cnt].timestamp;
450 } else if (caf->packet_cnt == sti->nb_index_entries - 1) {
451 pkt_size = caf->num_bytes - sti->index_entries[caf->packet_cnt].pos;
452 pkt_frames = st->duration - sti->index_entries[caf->packet_cnt].timestamp;
453 } else {
454 return AVERROR(EIO);
455 }
456 }
457
458 if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
459 return AVERROR(EIO);
460
461 res = av_get_packet(pb, pkt, pkt_size);
462 if (res < 0)
463 return res;
464
465 pkt->size = res;
466 pkt->stream_index = 0;
467 pkt->dts = pkt->pts = caf->frame_cnt;
468
469 caf->packet_cnt++;
470 caf->frame_cnt += pkt_frames;
471
472 return 0;
473 }
474
read_seek(AVFormatContext * s,int stream_index,int64_t timestamp,int flags)475 static int read_seek(AVFormatContext *s, int stream_index,
476 int64_t timestamp, int flags)
477 {
478 AVStream *st = s->streams[0];
479 FFStream *const sti = ffstream(st);
480 CafContext *caf = s->priv_data;
481 int64_t pos, packet_cnt, frame_cnt;
482
483 timestamp = FFMAX(timestamp, 0);
484
485 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
486 /* calculate new byte position based on target frame position */
487 pos = caf->bytes_per_packet * (timestamp / caf->frames_per_packet);
488 if (caf->data_size > 0)
489 pos = FFMIN(pos, caf->data_size);
490 packet_cnt = pos / caf->bytes_per_packet;
491 frame_cnt = caf->frames_per_packet * packet_cnt;
492 } else if (sti->nb_index_entries) {
493 packet_cnt = av_index_search_timestamp(st, timestamp, flags);
494 frame_cnt = sti->index_entries[packet_cnt].timestamp;
495 pos = sti->index_entries[packet_cnt].pos;
496 } else {
497 return -1;
498 }
499
500 if (avio_seek(s->pb, pos + caf->data_start, SEEK_SET) < 0)
501 return -1;
502
503 caf->packet_cnt = packet_cnt;
504 caf->frame_cnt = frame_cnt;
505
506 return 0;
507 }
508
509 const AVInputFormat ff_caf_demuxer = {
510 .name = "caf",
511 .long_name = NULL_IF_CONFIG_SMALL("Apple CAF (Core Audio Format)"),
512 .priv_data_size = sizeof(CafContext),
513 .read_probe = probe,
514 .read_header = read_header,
515 .read_packet = read_packet,
516 .read_seek = read_seek,
517 .codec_tag = ff_caf_codec_tags_list,
518 };
519