FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/cafdec.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 169 291 58.1%
Functions: 7 8 87.5%
Branches: 94 207 45.4%

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 6782 static int probe(const AVProbeData *p)
54 {
55
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6773 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
6782 if (AV_RB32(p->buf) == MKBETAG('c','a','f','f') && AV_RB16(&p->buf[4]) == 1)
56 9 return AVPROBE_SCORE_MAX;
57 6773 return 0;
58 }
59
60 /** Read audio description chunk */
61 9 static int read_desc_chunk(AVFormatContext *s)
62 {
63 9 AVIOContext *pb = s->pb;
64 9 CafContext *caf = s->priv_data;
65 AVStream *st;
66 int flags;
67
68 /* new audio stream */
69 9 st = avformat_new_stream(s, NULL);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!st)
71 return AVERROR(ENOMEM);
72
73 /* parse format description */
74 9 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
75 9 st->codecpar->sample_rate = av_clipd(av_int2double(avio_rb64(pb)), 0, INT_MAX);
76 9 st->codecpar->codec_tag = avio_rl32(pb);
77 9 flags = avio_rb32(pb);
78 9 caf->bytes_per_packet = avio_rb32(pb);
79 9 st->codecpar->block_align = caf->bytes_per_packet;
80 9 caf->frames_per_packet = avio_rb32(pb);
81 9 st->codecpar->ch_layout.nb_channels = avio_rb32(pb);
82 9 st->codecpar->bits_per_coded_sample = avio_rb32(pb);
83
84
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)
85 return AVERROR_INVALIDDATA;
86
87 /* calculate bit rate for constant size packets */
88
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) {
89 6 st->codecpar->bit_rate = (uint64_t)st->codecpar->sample_rate * (uint64_t)caf->bytes_per_packet * 8
90 6 / (uint64_t)caf->frames_per_packet;
91 } else {
92 3 st->codecpar->bit_rate = 0;
93 }
94
95 /* determine codec */
96
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
97 4 st->codecpar->codec_id = ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample, (flags ^ 0x2) | 0x4);
98 else
99 5 st->codecpar->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codecpar->codec_tag);
100 9 return 0;
101 }
102
103 /** Read magic cookie chunk */
104 4 static int read_kuki_chunk(AVFormatContext *s, int64_t size)
105 {
106 4 AVIOContext *pb = s->pb;
107 4 AVStream *st = s->streams[0];
108 int ret;
109
110
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)
111 return -1;
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 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
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!memcmp(&preamble[4], "frmaalac", 8)) {
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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 2 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 } else if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0) {
226 return ret;
227 }
228
229 4 return 0;
230 }
231
232 /** Read packet table chunk */
233 3 static int read_pakt_chunk(AVFormatContext *s, int64_t size)
234 {
235 3 AVIOContext *pb = s->pb;
236 3 AVStream *st = s->streams[0];
237 3 CafContext *caf = s->priv_data;
238 3 int64_t pos = 0, ccount, num_packets;
239 int i;
240 int ret;
241
242 3 ccount = avio_tell(pb);
243
244 3 num_packets = avio_rb64(pb);
245
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)
246 return AVERROR_INVALIDDATA;
247
248 3 st->nb_frames = avio_rb64(pb); /* valid frames */
249 3 st->nb_frames += avio_rb32(pb); /* priming frames */
250 3 st->nb_frames += avio_rb32(pb); /* remainder frames */
251
252
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) {
253 st->duration = caf->frames_per_packet * num_packets;
254 pos = caf-> bytes_per_packet * num_packets;
255 } else {
256 3 st->duration = 0;
257
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 3 times.
543 for (i = 0; i < num_packets; i++) {
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 540 times.
540 if (avio_feof(pb))
259 return AVERROR_INVALIDDATA;
260 540 ret = av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 540 times.
540 if (ret < 0)
262 return ret;
263
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);
264
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);
265 }
266 }
267
268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_tell(pb) - ccount > size) {
269 av_log(s, AV_LOG_ERROR, "error reading packet table\n");
270 return AVERROR_INVALIDDATA;
271 }
272 3 avio_seek(pb, ccount + size, SEEK_SET);
273
274 3 caf->num_bytes = pos;
275 3 return 0;
276 }
277
278 /** Read information chunk */
279 3 static void read_info_chunk(AVFormatContext *s, int64_t size)
280 {
281 3 AVIOContext *pb = s->pb;
282 unsigned int i;
283 3 unsigned int nb_entries = avio_rb32(pb);
284
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++) {
285 char key[32];
286 char value[1024];
287 27 avio_get_str(pb, INT_MAX, key, sizeof(key));
288 27 avio_get_str(pb, INT_MAX, value, sizeof(value));
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!*key)
290 continue;
291 27 av_dict_set(&s->metadata, key, value, 0);
292 }
293 3 }
294
295 9 static int read_header(AVFormatContext *s)
296 {
297 9 AVIOContext *pb = s->pb;
298 9 CafContext *caf = s->priv_data;
299 AVStream *st;
300 9 uint32_t tag = 0;
301 int found_data, ret;
302 int64_t size, pos;
303
304 9 avio_skip(pb, 8); /* magic, version, file flags */
305
306 /* audio description chunk */
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 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 9 size = avio_rb64(pb);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (size != 32)
313 return AVERROR_INVALIDDATA;
314
315 9 ret = read_desc_chunk(s);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret)
317 return ret;
318 9 st = s->streams[0];
319
320 /* parse each chunk */
321 9 found_data = 0;
322
1/2
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
37 while (!avio_feof(pb)) {
323
324 /* stop at data chunk if seeking is not supported or
325 data chunk size is unknown */
326
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)))
327 break;
328
329 37 tag = avio_rb32(pb);
330 37 size = avio_rb64(pb);
331 37 pos = avio_tell(pb);
332
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 28 times.
37 if (avio_feof(pb))
333 9 break;
334
335
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) {
336 9 case MKBETAG('d','a','t','a'):
337 9 avio_skip(pb, 4); /* edit count */
338 9 caf->data_start = avio_tell(pb);
339
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 caf->data_size = size < 0 ? -1 : size - 4;
340
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))
341 9 avio_skip(pb, caf->data_size);
342 9 found_data = 1;
343 9 break;
344
345 8 case MKBETAG('c','h','a','n'):
346
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if ((ret = ff_mov_read_chan(s, s->pb, st, size)) < 0)
347 return ret;
348 8 break;
349
350 /* magic cookie chunk */
351 4 case MKBETAG('k','u','k','i'):
352
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (read_kuki_chunk(s, size))
353 return AVERROR_INVALIDDATA;
354 4 break;
355
356 /* packet table chunk */
357 3 case MKBETAG('p','a','k','t'):
358
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (read_pakt_chunk(s, size))
359 return AVERROR_INVALIDDATA;
360 3 break;
361
362 3 case MKBETAG('i','n','f','o'):
363 3 read_info_chunk(s, size);
364 3 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 1 case MKBETAG('f','r','e','e'):
371
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)
372 goto found_data;
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size < 0)
374 return AVERROR_INVALIDDATA;
375 1 break;
376 }
377
378
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)) {
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (pos > INT64_MAX - size)
380 return AVERROR_INVALIDDATA;
381 28 avio_seek(pb, pos + size, SEEK_SET);
382 }
383 }
384
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!found_data)
386 return AVERROR_INVALIDDATA;
387
388 9 found_data:
389
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) {
390
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)
391 6 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
392
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) {
393
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) {
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 3 st->codecpar->bit_rate = st->codecpar->sample_rate * 8LL *
399 3 (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 9 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
407 9 st->start_time = 0;
408
409 /* position the stream at the start of data */
410
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (caf->data_size >= 0)
411 9 avio_seek(pb, caf->data_start, SEEK_SET);
412
413 9 return 0;
414 }
415
416 #define CAF_MAX_PKT_SIZE 4096
417
418 111 static int read_packet(AVFormatContext *s, AVPacket *pkt)
419 {
420 111 AVIOContext *pb = s->pb;
421 111 AVStream *st = s->streams[0];
422 111 FFStream *const sti = ffstream(st);
423 111 CafContext *caf = s->priv_data;
424 111 int res, pkt_size = 0, pkt_frames = 0;
425 111 int64_t left = CAF_MAX_PKT_SIZE;
426
427
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
111 if (avio_feof(pb))
428 return AVERROR_EOF;
429
430 /* don't read past end of data chunk */
431
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 if (caf->data_size > 0) {
432 111 left = (caf->data_start + caf->data_size) - avio_tell(pb);
433
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 109 times.
111 if (!left)
434 2 return AVERROR_EOF;
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109 times.
109 if (left < 0)
436 return AVERROR(EIO);
437 }
438
439 109 pkt_frames = caf->frames_per_packet;
440 109 pkt_size = caf->bytes_per_packet;
441
442
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) {
443 68 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
444 68 pkt_size = FFMIN(pkt_size, left);
445 68 pkt_frames = pkt_size / caf->bytes_per_packet;
446
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 21 times.
41 } else if (sti->nb_index_entries) {
447
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (caf->packet_cnt < sti->nb_index_entries - 1) {
448 20 pkt_size = sti->index_entries[caf->packet_cnt + 1].pos - sti->index_entries[caf->packet_cnt].pos;
449 20 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
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)
459 return AVERROR(EIO);
460
461 109 res = av_get_packet(pb, pkt, pkt_size);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109 times.
109 if (res < 0)
463 return res;
464
465 109 pkt->size = res;
466 109 pkt->stream_index = 0;
467 109 pkt->dts = pkt->pts = caf->frame_cnt;
468
469 109 caf->packet_cnt++;
470 109 caf->frame_cnt += pkt_frames;
471
472 109 return 0;
473 }
474
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