FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/cafdec.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 172 297 57.9%
Functions: 7 8 87.5%
Branches: 96 211 45.5%

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