FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/cafdec.c
Date: 2026-10-30 23:38:52
Exec Total Coverage
Lines: 238 346 68.8%
Functions: 8 8 100.0%
Branches: 136 251 54.2%

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