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