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