FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/cafdec.c
Date: 2026-05-03 03:13:14
Exec Total Coverage
Lines: 194 344 56.4%
Functions: 7 8 87.5%
Branches: 114 247 46.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 7480 static int probe(const AVProbeData *p)
61 {
62
2/2
✓ Branch 0 taken 7471 times.
✓ Branch 1 taken 9 times.
7480 if (AV_RB32(p->buf) != MKBETAG('c','a','f','f'))
63 7471 return 0;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (AV_RB16(&p->buf[4]) != 1)
65 return 0;
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (AV_RB32(p->buf + 8) != MKBETAG('d','e','s','c'))
67 return 0;
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (AV_RB64(p->buf + 12) != 32)
69 return 0;
70 9 return AVPROBE_SCORE_MAX;
71 }
72
73 /** Read audio description chunk */
74 9 static int read_desc_chunk(AVFormatContext *s)
75 {
76 9 AVIOContext *pb = s->pb;
77 9 CafContext *caf = s->priv_data;
78 AVStream *st;
79 int flags;
80
81 /* new audio stream */
82 9 st = avformat_new_stream(s, NULL);
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!st)
84 return AVERROR(ENOMEM);
85
86 /* parse format description */
87 9 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
88 9 st->codecpar->sample_rate = av_clipd(av_int2double(avio_rb64(pb)), 0, INT_MAX);
89 9 st->codecpar->codec_tag = avio_rl32(pb);
90 9 flags = avio_rb32(pb);
91 9 caf->bytes_per_packet = avio_rb32(pb);
92 9 st->codecpar->block_align = caf->bytes_per_packet;
93 9 caf->frames_per_packet = avio_rb32(pb);
94
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 st->codecpar->frame_size = caf->frames_per_packet != 1 ? caf->frames_per_packet : 0;
95 9 st->codecpar->ch_layout.nb_channels = avio_rb32(pb);
96 9 st->codecpar->bits_per_coded_sample = avio_rb32(pb);
97
98
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
9 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 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
9 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 3 st->codecpar->bit_rate = 0;
107 }
108
109 /* determine codec */
110
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 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 5 st->codecpar->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codecpar->codec_tag);
114 9 return 0;
115 }
116
117 /** Read magic cookie chunk */
118 4 static int read_kuki_chunk(AVFormatContext *s, int64_t size)
119 {
120 4 AVIOContext *pb = s->pb;
121 4 AVStream *st = s->streams[0];
122 int ret;
123
124
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
125 return -1;
126
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 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 strt = avio_tell(pb);
134 ff_mov_read_esds(s, pb);
135 skip = size - (avio_tell(pb) - strt);
136 if (skip < 0 || !st->codecpar->extradata ||
137 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 avio_skip(pb, skip);
142
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } 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 2 times.
2 } 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } 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 if (st->codecpar->ch_layout.nb_channels > 2) {
235 avpriv_request_sample(s, "multichannel Opus in CAF");
236 return AVERROR_PATCHWELCOME;
237 }
238
239 ret = ff_alloc_extradata(st->codecpar, 19);
240 if (ret < 0)
241 return ret;
242
243 AV_WB32A(st->codecpar->extradata, MKBETAG('O','p','u','s'));
244 AV_WB32A(st->codecpar->extradata + 4, MKBETAG('H','e','a','d'));
245 AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
246 AV_WB8(st->codecpar->extradata + 9, st->codecpar->ch_layout.nb_channels);
247 AV_WL16A(st->codecpar->extradata + 10, st->codecpar->initial_padding);
248 AV_WL32A(st->codecpar->extradata + 12, st->codecpar->sample_rate);
249 AV_WL16A(st->codecpar->extradata + 16, 0);
250 AV_WB8(st->codecpar->extradata + 18, 0);
251
252 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 4 return 0;
258 }
259
260 /** Read packet table chunk */
261 4 static int read_pakt_chunk(AVFormatContext *s, int64_t size)
262 {
263 4 AVIOContext *pb = s->pb;
264 4 AVStream *st = s->streams[0];
265 4 CafContext *caf = s->priv_data;
266 4 int64_t pos = 0, ccount, num_packets;
267 unsigned priming;
268 int i;
269 int ret;
270
271 4 ccount = avio_tell(pb);
272
273 4 num_packets = avio_rb64(pb);
274
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (num_packets < 0 || INT32_MAX / sizeof(AVIndexEntry) < num_packets)
275 return AVERROR_INVALIDDATA;
276
277 4 st->nb_frames = avio_rb64(pb); /* valid frames */
278 4 priming = avio_rb32(pb); /* priming frames */
279 4 caf->remainder = avio_rb32(pb); /* remainder frames */
280
281 4 caf->frame_cnt = -(int64_t)priming;
282 4 st->codecpar->initial_padding = priming;
283 4 st->nb_frames += priming;
284 4 st->nb_frames += caf->remainder;
285
286
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS && st->codecpar->extradata_size)
287 AV_WL16A(st->codecpar->extradata + 10, st->codecpar->initial_padding);
288
289
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
290
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!num_packets) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (caf->data_size < 0)
292 return AVERROR_INVALIDDATA;
293 1 num_packets = caf->data_size / caf->bytes_per_packet;
294 }
295 1 st->duration = caf->frames_per_packet * num_packets - priming;
296 1 pos = caf-> bytes_per_packet * num_packets;
297 } else {
298 3 st->duration = caf->frame_cnt;
299
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 3 times.
543 for (i = 0; i < num_packets; i++) {
300
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 540 times.
540 if (avio_feof(pb))
301 return AVERROR_INVALIDDATA;
302 540 ret = av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 540 times.
540 if (ret < 0)
304 return ret;
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 540 times.
540 pos += caf->bytes_per_packet ? caf->bytes_per_packet : ff_mp4_read_descr_len(pb);
306
1/2
✓ Branch 0 taken 540 times.
✗ Branch 1 not taken.
540 st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb);
307 }
308 }
309 4 st->duration -= caf->remainder;
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (st->duration < 0)
311 return AVERROR_INVALIDDATA;
312
313
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 if (avio_tell(pb) - ccount > size || size > INT64_MAX - ccount) {
314 av_log(s, AV_LOG_ERROR, "error reading packet table\n");
315 return AVERROR_INVALIDDATA;
316 }
317 4 avio_seek(pb, ccount + size, SEEK_SET);
318
319 4 caf->num_packets = num_packets;
320 4 caf->num_bytes = pos;
321 4 return 0;
322 }
323
324 /** Read information chunk */
325 3 static void read_info_chunk(AVFormatContext *s, int64_t size)
326 {
327 3 AVIOContext *pb = s->pb;
328 unsigned int i;
329 3 unsigned int nb_entries = avio_rb32(pb);
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (3LL * nb_entries > size)
332 return;
333
334
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
30 for (i = 0; i < nb_entries && !avio_feof(pb); i++) {
335 char key[32];
336 char value[1024];
337 27 avio_get_str(pb, INT_MAX, key, sizeof(key));
338 27 avio_get_str(pb, INT_MAX, value, sizeof(value));
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!*key)
340 continue;
341 27 av_dict_set(&s->metadata, key, value, 0);
342 }
343 }
344
345 9 static int read_header(AVFormatContext *s)
346 {
347 9 AVIOContext *pb = s->pb;
348 9 CafContext *caf = s->priv_data;
349 AVStream *st;
350 9 uint32_t tag = 0;
351 int found_data, ret;
352 int64_t size, pos;
353
354 9 avio_skip(pb, 8); /* magic, version, file flags */
355
356 /* audio description chunk */
357
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (avio_rb32(pb) != MKBETAG('d','e','s','c')) {
358 av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
359 return AVERROR_INVALIDDATA;
360 }
361 9 size = avio_rb64(pb);
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (size != 32)
363 return AVERROR_INVALIDDATA;
364
365 9 ret = read_desc_chunk(s);
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret)
367 return ret;
368 9 st = s->streams[0];
369
370 /* parse each chunk */
371 9 found_data = 0;
372
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
38 while (!avio_feof(pb)) {
373
374 /* stop at data chunk if seeking is not supported or
375 data chunk size is unknown */
376
4/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
38 if (found_data && (caf->data_size < 0 || !(pb->seekable & AVIO_SEEKABLE_NORMAL)))
377 break;
378
379 38 tag = avio_rb32(pb);
380 38 size = avio_rb64(pb);
381 38 pos = avio_tell(pb);
382
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 29 times.
38 if (avio_feof(pb))
383 9 break;
384
385
6/7
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
29 switch (tag) {
386 9 case MKBETAG('d','a','t','a'):
387 9 avio_skip(pb, 4); /* edit count */
388 9 caf->data_start = avio_tell(pb);
389
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 caf->data_size = size < 0 ? -1 : size - 4;
390
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (caf->data_start < 0 || caf->data_size > INT64_MAX - caf->data_start)
391 return AVERROR_INVALIDDATA;
392
393
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 if (caf->data_size > 0 && (pb->seekable & AVIO_SEEKABLE_NORMAL))
394 9 avio_skip(pb, caf->data_size);
395 9 found_data = 1;
396 9 break;
397
398 8 case MKBETAG('c','h','a','n'):
399
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if ((ret = ff_mov_read_chan(s, s->pb, st, size)) < 0)
400 return ret;
401 8 break;
402
403 /* magic cookie chunk */
404 4 case MKBETAG('k','u','k','i'):
405
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (read_kuki_chunk(s, size))
406 return AVERROR_INVALIDDATA;
407 4 break;
408
409 /* packet table chunk */
410 4 case MKBETAG('p','a','k','t'):
411
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (read_pakt_chunk(s, size))
412 return AVERROR_INVALIDDATA;
413 4 break;
414
415 3 case MKBETAG('i','n','f','o'):
416 3 read_info_chunk(s, size);
417 3 break;
418
419 default:
420 av_log(s, AV_LOG_WARNING,
421 "skipping CAF chunk: %08"PRIX32" (%s), size %"PRId64"\n",
422 tag, av_fourcc2str(av_bswap32(tag)), size);
423 av_fallthrough;
424 1 case MKBETAG('f','r','e','e'):
425
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (size < 0 && found_data)
426 goto found_data;
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size < 0)
428 return AVERROR_INVALIDDATA;
429 1 break;
430 }
431
432
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 if (size > 0 && (pb->seekable & AVIO_SEEKABLE_NORMAL)) {
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (pos > INT64_MAX - size)
434 return AVERROR_INVALIDDATA;
435 29 avio_seek(pb, pos + size, SEEK_SET);
436 }
437 }
438
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!found_data)
440 return AVERROR_INVALIDDATA;
441
442 9 found_data:
443
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
9 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
444
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)
445 6 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
446
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 } else if (ffstream(st)->nb_index_entries && st->duration > 0) {
447
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (st->codecpar->sample_rate && caf->data_size / st->duration > INT64_MAX / st->codecpar->sample_rate / 8) {
448 av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * 8 * %"PRId64"\n",
449 st->codecpar->sample_rate, caf->data_size / st->duration);
450 return AVERROR_INVALIDDATA;
451 }
452 3 st->codecpar->bit_rate = st->codecpar->sample_rate * 8LL *
453 3 (caf->data_size / st->duration);
454 } else {
455 av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
456 "block size or frame size are variable.\n");
457 return AVERROR_INVALIDDATA;
458 }
459
460 9 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
461 9 st->start_time = 0;
462
463 /* position the stream at the start of data */
464
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (caf->data_size >= 0)
465 9 avio_seek(pb, caf->data_start, SEEK_SET);
466
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!ff_is_intra_only(st->codecpar->codec_id))
468 ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
469
470 9 return 0;
471 }
472
473 #define CAF_MAX_PKT_SIZE 4096
474
475 128 static int read_packet(AVFormatContext *s, AVPacket *pkt)
476 {
477 128 AVIOContext *pb = s->pb;
478 128 AVStream *st = s->streams[0];
479 128 FFStream *const sti = ffstream(st);
480 128 CafContext *caf = s->priv_data;
481 128 int res, pkt_size = 0, pkt_frames = 0;
482 128 unsigned priming = 0, remainder = 0;
483 128 int64_t left = CAF_MAX_PKT_SIZE;
484
485
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 128 times.
128 if (avio_feof(pb))
486 return AVERROR_EOF;
487
488 /* don't read past end of data chunk */
489
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 if (caf->data_size > 0) {
490 128 left = (caf->data_start + caf->data_size) - avio_tell(pb);
491
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 126 times.
128 if (!left)
492 2 return AVERROR_EOF;
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (left < 0)
494 return AVERROR_INVALIDDATA;
495 }
496
497 126 pkt_frames = caf->frames_per_packet;
498 126 pkt_size = caf->bytes_per_packet;
499
500
4/4
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 30 times.
126 if (pkt_size > 0 && pkt_frames == 1) {
501 74 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
502 74 pkt_size = FFMIN(pkt_size, left);
503 74 pkt_frames = pkt_size / caf->bytes_per_packet;
504
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 30 times.
52 } else if (sti->nb_index_entries) {
505
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (caf->packet_cnt < sti->nb_index_entries - 1) {
506 22 pkt_size = sti->index_entries[caf->packet_cnt + 1].pos - sti->index_entries[caf->packet_cnt].pos;
507 22 pkt_frames = sti->index_entries[caf->packet_cnt + 1].timestamp - sti->index_entries[caf->packet_cnt].timestamp;
508 } else if (caf->packet_cnt == sti->nb_index_entries - 1) {
509 pkt_size = caf->num_bytes - sti->index_entries[caf->packet_cnt].pos;
510 pkt_frames = st->duration - sti->index_entries[caf->packet_cnt].timestamp;
511 remainder = caf->remainder;
512 } else {
513 return AVERROR_INVALIDDATA;
514 }
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 } else if (caf->packet_cnt + 1 == caf->num_packets) {
516 pkt_frames -= caf->remainder;
517 remainder = caf->remainder;
518 }
519
520
3/6
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 126 times.
126 if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
521 return AVERROR_INVALIDDATA;
522
523 126 res = av_get_packet(pb, pkt, pkt_size);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (res < 0)
525 return res;
526
527
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 117 times.
126 if (!caf->packet_cnt)
528 9 priming = st->codecpar->initial_padding;
529
530
2/4
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 126 times.
126 if (priming > 0 || remainder > 0) {
531 uint8_t* side_data = av_packet_new_side_data(pkt,
532 AV_PKT_DATA_SKIP_SAMPLES,
533 10);
534 if (!side_data)
535 return AVERROR(ENOMEM);
536
537 AV_WL32A(side_data, priming);
538 AV_WL32A(side_data + 4, remainder);
539 }
540
541 126 pkt->duration = pkt_frames;
542 126 pkt->size = res;
543 126 pkt->stream_index = 0;
544 126 pkt->dts = pkt->pts = caf->frame_cnt;
545
546 126 caf->packet_cnt++;
547 126 caf->frame_cnt += pkt_frames;
548
549 126 return 0;
550 }
551
552 static int read_seek(AVFormatContext *s, int stream_index,
553 int64_t timestamp, int flags)
554 {
555 AVStream *st = s->streams[0];
556 FFStream *const sti = ffstream(st);
557 CafContext *caf = s->priv_data;
558 int64_t pos, packet_cnt, frame_cnt;
559
560 timestamp = FFMAX(timestamp, 0);
561
562 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
563 /* calculate new byte position based on target frame position */
564 pos = caf->bytes_per_packet * (timestamp / caf->frames_per_packet);
565 if (caf->data_size > 0)
566 pos = FFMIN(pos, caf->data_size);
567 packet_cnt = pos / caf->bytes_per_packet;
568 frame_cnt = caf->frames_per_packet * packet_cnt - st->codecpar->initial_padding;
569 } else if (sti->nb_index_entries) {
570 packet_cnt = av_index_search_timestamp(st, timestamp, flags);
571 if (packet_cnt < 0)
572 return -1;
573 frame_cnt = sti->index_entries[packet_cnt].timestamp;
574 pos = sti->index_entries[packet_cnt].pos;
575 } else {
576 return -1;
577 }
578
579 if (avio_seek(s->pb, pos + caf->data_start, SEEK_SET) < 0)
580 return -1;
581
582 caf->packet_cnt = packet_cnt;
583 caf->frame_cnt = frame_cnt;
584
585 return 0;
586 }
587
588 const FFInputFormat ff_caf_demuxer = {
589 .p.name = "caf",
590 .p.long_name = NULL_IF_CONFIG_SMALL("Apple CAF (Core Audio Format)"),
591 .p.codec_tag = ff_caf_codec_tags_list,
592 .priv_data_size = sizeof(CafContext),
593 .read_probe = probe,
594 .read_header = read_header,
595 .read_packet = read_packet,
596 .read_seek = read_seek,
597 };
598