| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * BRSTM demuxer | ||
| 3 | * Copyright (c) 2012 Paul B Mahol | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/intreadwrite.h" | ||
| 23 | #include "libavutil/mem.h" | ||
| 24 | #include "libavcodec/bytestream.h" | ||
| 25 | #include "avformat.h" | ||
| 26 | #include "avio_internal.h" | ||
| 27 | #include "demux.h" | ||
| 28 | #include "internal.h" | ||
| 29 | |||
| 30 | typedef struct BRSTMCoeffOffset { | ||
| 31 | uint8_t channel; | ||
| 32 | uint32_t offset; | ||
| 33 | } BRSTMCoeffOffset; | ||
| 34 | |||
| 35 | typedef struct BRSTMDemuxContext { | ||
| 36 | uint32_t block_size; | ||
| 37 | uint32_t block_count; | ||
| 38 | uint32_t current_block; | ||
| 39 | uint32_t samples_per_block; | ||
| 40 | uint32_t last_block_used_bytes; | ||
| 41 | uint32_t last_block_size; | ||
| 42 | uint32_t last_block_samples; | ||
| 43 | uint32_t data_start; | ||
| 44 | uint8_t table[256 * 32]; | ||
| 45 | uint8_t *adpc; | ||
| 46 | BRSTMCoeffOffset offsets[256]; | ||
| 47 | int little_endian; | ||
| 48 | } BRSTMDemuxContext; | ||
| 49 | |||
| 50 | 7480 | static int probe(const AVProbeData *p) | |
| 51 | { | ||
| 52 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7479 times.
|
7480 | if (AV_RL32(p->buf) == MKTAG('R','S','T','M') && |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | (AV_RL16(p->buf + 4) == 0xFFFE || |
| 54 | ✗ | AV_RL16(p->buf + 4) == 0xFEFF)) | |
| 55 | 1 | return AVPROBE_SCORE_MAX / 3 * 2; | |
| 56 | 7479 | return 0; | |
| 57 | } | ||
| 58 | |||
| 59 | 7480 | static int probe_bfstm(const AVProbeData *p) | |
| 60 | { | ||
| 61 |
2/2✓ Branch 0 taken 7479 times.
✓ Branch 1 taken 1 times.
|
7480 | if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') || |
| 62 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7478 times.
|
7479 | AV_RL32(p->buf) == MKTAG('C','S','T','M')) && |
| 63 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | (AV_RL16(p->buf + 4) == 0xFFFE || |
| 64 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | AV_RL16(p->buf + 4) == 0xFEFF)) |
| 65 | 2 | return AVPROBE_SCORE_MAX / 3 * 2; | |
| 66 | 7478 | return 0; | |
| 67 | } | ||
| 68 | |||
| 69 | 3 | static int read_close(AVFormatContext *s) | |
| 70 | { | ||
| 71 | 3 | BRSTMDemuxContext *b = s->priv_data; | |
| 72 | |||
| 73 | 3 | av_freep(&b->adpc); | |
| 74 | |||
| 75 | 3 | return 0; | |
| 76 | } | ||
| 77 | |||
| 78 | 7 | static int sort_offsets(const void *a, const void *b) | |
| 79 | { | ||
| 80 | 7 | const BRSTMCoeffOffset *s1 = a; | |
| 81 | 7 | const BRSTMCoeffOffset *s2 = b; | |
| 82 | 7 | return FFDIFFSIGN(s1->offset, s2->offset); | |
| 83 | } | ||
| 84 | |||
| 85 | 12 | static av_always_inline unsigned int read16(AVFormatContext *s) | |
| 86 | { | ||
| 87 | 12 | BRSTMDemuxContext *b = s->priv_data; | |
| 88 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
|
12 | if (b->little_endian) |
| 89 | 5 | return avio_rl16(s->pb); | |
| 90 | else | ||
| 91 | 7 | return avio_rb16(s->pb); | |
| 92 | } | ||
| 93 | |||
| 94 | 56 | static av_always_inline unsigned int read32(AVFormatContext *s) | |
| 95 | { | ||
| 96 | 56 | BRSTMDemuxContext *b = s->priv_data; | |
| 97 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 39 times.
|
56 | if (b->little_endian) |
| 98 | 17 | return avio_rl32(s->pb); | |
| 99 | else | ||
| 100 | 39 | return avio_rb32(s->pb); | |
| 101 | } | ||
| 102 | |||
| 103 | 3 | static int read_header(AVFormatContext *s) | |
| 104 | { | ||
| 105 | 3 | BRSTMDemuxContext *b = s->priv_data; | |
| 106 | int bom, major, minor, codec, chunk; | ||
| 107 | int64_t h1offset, pos, toffset; | ||
| 108 | 3 | uint32_t size, asize, start = 0; | |
| 109 | AVStream *st; | ||
| 110 | 3 | int loop = 0; | |
| 111 | 3 | int bfstm = !strcmp("bfstm", s->iformat->name); | |
| 112 | |||
| 113 | 3 | st = avformat_new_stream(s, NULL); | |
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 115 | ✗ | return AVERROR(ENOMEM); | |
| 116 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 117 | |||
| 118 | 3 | avio_skip(s->pb, 4); | |
| 119 | |||
| 120 | 3 | bom = avio_rb16(s->pb); | |
| 121 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
3 | if (bom != 0xFEFF && bom != 0xFFFE) { |
| 122 | ✗ | av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom); | |
| 123 | ✗ | return AVERROR_INVALIDDATA; | |
| 124 | } | ||
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (bom == 0xFFFE) |
| 127 | 1 | b->little_endian = 1; | |
| 128 | |||
| 129 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!bfstm) { |
| 130 | 1 | major = avio_r8(s->pb); | |
| 131 | 1 | minor = avio_r8(s->pb); | |
| 132 | 1 | avio_skip(s->pb, 4); // size of file | |
| 133 | 1 | size = read16(s); | |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (size < 14) |
| 135 | ✗ | return AVERROR_INVALIDDATA; | |
| 136 | |||
| 137 | 1 | avio_skip(s->pb, size - 14); | |
| 138 | 1 | pos = avio_tell(s->pb); | |
| 139 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (avio_rl32(s->pb) != MKTAG('H','E','A','D')) |
| 140 | ✗ | return AVERROR_INVALIDDATA; | |
| 141 | } else { | ||
| 142 | 2 | uint32_t info_offset = 0; | |
| 143 | uint16_t section_count, header_size, i; | ||
| 144 | |||
| 145 | 2 | header_size = read16(s); // 6 | |
| 146 | |||
| 147 | 2 | avio_skip(s->pb, 4); // Unknown constant 0x00030000 | |
| 148 | 2 | avio_skip(s->pb, 4); // size of file | |
| 149 | 2 | section_count = read16(s); | |
| 150 | 2 | avio_skip(s->pb, 2); // padding | |
| 151 | 8 | for (i = 0; avio_tell(s->pb) < header_size | |
| 152 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
8 | && !(start && info_offset) |
| 153 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
20 | && i < section_count; i++) { |
| 154 | 6 | uint16_t flag = read16(s); | |
| 155 | 6 | avio_skip(s->pb, 2); | |
| 156 |
3/5✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | switch (flag) { |
| 157 | 2 | case 0x4000: | |
| 158 | 2 | info_offset = read32(s); | |
| 159 | 2 | /*info_size =*/ read32(s); | |
| 160 | 2 | break; | |
| 161 | 2 | case 0x4001: | |
| 162 | 2 | avio_skip(s->pb, 4); // seek offset | |
| 163 | 2 | avio_skip(s->pb, 4); // seek size | |
| 164 | 2 | break; | |
| 165 | 2 | case 0x4002: | |
| 166 | 2 | start = read32(s) + 8; | |
| 167 | 2 | avio_skip(s->pb, 4); //data_size = read32(s); | |
| 168 | 2 | break; | |
| 169 | ✗ | case 0x4003: | |
| 170 | ✗ | avio_skip(s->pb, 4); // REGN offset | |
| 171 | ✗ | avio_skip(s->pb, 4); // REGN size | |
| 172 | ✗ | break; | |
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!info_offset || !start) |
| 177 | ✗ | return AVERROR_INVALIDDATA; | |
| 178 | |||
| 179 | 2 | avio_skip(s->pb, info_offset - avio_tell(s->pb)); | |
| 180 | 2 | pos = avio_tell(s->pb); | |
| 181 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (avio_rl32(s->pb) != MKTAG('I','N','F','O')) |
| 182 | ✗ | return AVERROR_INVALIDDATA; | |
| 183 | } | ||
| 184 | |||
| 185 | 3 | size = read32(s); | |
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (size < 40) |
| 187 | ✗ | return AVERROR_INVALIDDATA; | |
| 188 | 3 | avio_skip(s->pb, 4); // unknown | |
| 189 | 3 | h1offset = read32(s); | |
| 190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (h1offset > size) |
| 191 | ✗ | return AVERROR_INVALIDDATA; | |
| 192 | 3 | avio_skip(s->pb, 12); | |
| 193 | 3 | toffset = read32(s) + 16LL; | |
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (toffset > size) |
| 195 | ✗ | return AVERROR_INVALIDDATA; | |
| 196 | |||
| 197 | 3 | avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb)); | |
| 198 | 3 | codec = avio_r8(s->pb); | |
| 199 | |||
| 200 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | switch (codec) { |
| 201 | ✗ | case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break; | |
| 202 | ✗ | case 1: codec = b->little_endian ? | |
| 203 | ✗ | AV_CODEC_ID_PCM_S16LE_PLANAR : | |
| 204 | ✗ | AV_CODEC_ID_PCM_S16BE_PLANAR; break; | |
| 205 | 6 | case 2: codec = b->little_endian ? | |
| 206 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | AV_CODEC_ID_ADPCM_THP_LE : |
| 207 | 3 | AV_CODEC_ID_ADPCM_THP; break; | |
| 208 | ✗ | default: | |
| 209 | ✗ | avpriv_request_sample(s, "codec %d", codec); | |
| 210 | ✗ | return AVERROR_PATCHWELCOME; | |
| 211 | } | ||
| 212 | |||
| 213 | 3 | loop = avio_r8(s->pb); // loop flag | |
| 214 | 3 | st->codecpar->codec_id = codec; | |
| 215 | 3 | st->codecpar->ch_layout.nb_channels = avio_r8(s->pb); | |
| 216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st->codecpar->ch_layout.nb_channels) |
| 217 | ✗ | return AVERROR_INVALIDDATA; | |
| 218 | |||
| 219 | 3 | avio_skip(s->pb, 1); // padding | |
| 220 | |||
| 221 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | st->codecpar->sample_rate = bfstm ? read32(s) : read16(s); |
| 222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (st->codecpar->sample_rate <= 0) |
| 223 | ✗ | return AVERROR_INVALIDDATA; | |
| 224 | |||
| 225 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!bfstm) |
| 226 | 1 | avio_skip(s->pb, 2); // padding | |
| 227 | |||
| 228 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (loop) { |
| 229 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (av_dict_set_int(&s->metadata, "loop_start", |
| 230 | 2 | av_rescale(read32(s), AV_TIME_BASE, | |
| 231 | 2 | st->codecpar->sample_rate), | |
| 232 | 0) < 0) | ||
| 233 | ✗ | return AVERROR(ENOMEM); | |
| 234 | } else { | ||
| 235 | 1 | avio_skip(s->pb, 4); | |
| 236 | } | ||
| 237 | |||
| 238 | 3 | st->start_time = 0; | |
| 239 | 3 | st->duration = read32(s); | |
| 240 | 3 | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); | |
| 241 | |||
| 242 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!bfstm) |
| 243 | 1 | start = read32(s); | |
| 244 | 3 | b->current_block = 0; | |
| 245 | 3 | b->block_count = read32(s); | |
| 246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (b->block_count > UINT16_MAX) { |
| 247 | ✗ | av_log(s, AV_LOG_WARNING, "too many blocks: %"PRIu32"\n", b->block_count); | |
| 248 | ✗ | return AVERROR_INVALIDDATA; | |
| 249 | } | ||
| 250 | |||
| 251 | 3 | b->block_size = read32(s); | |
| 252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (b->block_size > UINT32_MAX / st->codecpar->ch_layout.nb_channels) |
| 253 | ✗ | return AVERROR_INVALIDDATA; | |
| 254 | |||
| 255 | 3 | b->samples_per_block = read32(s); | |
| 256 | 3 | b->last_block_used_bytes = read32(s); | |
| 257 | 3 | b->last_block_samples = read32(s); | |
| 258 | 3 | b->last_block_size = read32(s); | |
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (b->last_block_size > UINT32_MAX / st->codecpar->ch_layout.nb_channels) |
| 260 | ✗ | return AVERROR_INVALIDDATA; | |
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (b->last_block_used_bytes > b->last_block_size) |
| 262 | ✗ | return AVERROR_INVALIDDATA; | |
| 263 | |||
| 264 | |||
| 265 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
3 | if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) { |
| 266 | int ch; | ||
| 267 | |||
| 268 | 3 | avio_skip(s->pb, pos + toffset - avio_tell(s->pb)); | |
| 269 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!bfstm) |
| 270 | 1 | toffset = read32(s) + 16LL; | |
| 271 | else | ||
| 272 | 2 | toffset = toffset + read32(s) + st->codecpar->ch_layout.nb_channels * 8 - 8; | |
| 273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (toffset > size) |
| 274 | ✗ | return AVERROR_INVALIDDATA; | |
| 275 | |||
| 276 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!bfstm) { |
| 277 | 1 | avio_skip(s->pb, pos + toffset - avio_tell(s->pb) - 8LL * (st->codecpar->ch_layout.nb_channels + 1)); | |
| 278 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) { |
| 279 | 6 | avio_skip(s->pb, 4); | |
| 280 | 6 | b->offsets[ch].channel = ch; | |
| 281 | 6 | b->offsets[ch].offset = read32(s); | |
| 282 | } | ||
| 283 | |||
| 284 | 1 | qsort(b->offsets, st->codecpar->ch_layout.nb_channels, sizeof(*b->offsets), sort_offsets); | |
| 285 | } | ||
| 286 | |||
| 287 | 3 | avio_skip(s->pb, pos + toffset - avio_tell(s->pb)); | |
| 288 | |||
| 289 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) { |
| 290 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | if (!bfstm) |
| 291 | 6 | avio_skip(s->pb, pos + 16LL + b->offsets[ch].offset - avio_tell(s->pb)); | |
| 292 | |||
| 293 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if (avio_read(s->pb, b->table + ch * 32, 32) != 32) |
| 294 | ✗ | return AVERROR_INVALIDDATA; | |
| 295 | |||
| 296 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if (bfstm) |
| 297 | 3 | avio_skip(s->pb, 14); | |
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (size < (avio_tell(s->pb) - pos)) |
| 302 | ✗ | return AVERROR_INVALIDDATA; | |
| 303 | |||
| 304 | 3 | avio_skip(s->pb, size - (avio_tell(s->pb) - pos)); | |
| 305 | |||
| 306 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | while (!avio_feof(s->pb)) { |
| 307 | 6 | chunk = avio_rl32(s->pb); | |
| 308 | 6 | size = read32(s); | |
| 309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (size < 8) |
| 310 | ✗ | return AVERROR_INVALIDDATA; | |
| 311 | 6 | size -= 8; | |
| 312 |
2/3✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | switch (chunk) { |
| 313 | 3 | case MKTAG('S','E','E','K'): | |
| 314 | case MKTAG('A','D','P','C'): | ||
| 315 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
3 | if (codec != AV_CODEC_ID_ADPCM_THP && |
| 316 | codec != AV_CODEC_ID_ADPCM_THP_LE) | ||
| 317 | ✗ | goto skip; | |
| 318 | |||
| 319 | 3 | asize = b->block_count * st->codecpar->ch_layout.nb_channels * 4; | |
| 320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (size < asize) |
| 321 | ✗ | return AVERROR_INVALIDDATA; | |
| 322 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (b->adpc) { |
| 323 | ✗ | av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n"); | |
| 324 | ✗ | goto skip; | |
| 325 | } else { | ||
| 326 | 3 | b->adpc = av_mallocz(asize); | |
| 327 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!b->adpc) |
| 328 | ✗ | return AVERROR(ENOMEM); | |
| 329 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
4 | if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) { |
| 330 | // Big-endian BFSTMs have little-endian SEEK tables | ||
| 331 | // for some strange reason. | ||
| 332 | int i; | ||
| 333 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (i = 0; i < asize; i += 2) { |
| 334 | 32 | b->adpc[i+1] = avio_r8(s->pb); | |
| 335 | 32 | b->adpc[i] = avio_r8(s->pb); | |
| 336 | } | ||
| 337 | } else { | ||
| 338 | 2 | avio_read(s->pb, b->adpc, asize); | |
| 339 | } | ||
| 340 | 3 | avio_skip(s->pb, size - asize); | |
| 341 | } | ||
| 342 | 3 | break; | |
| 343 | 3 | case MKTAG('D','A','T','A'): | |
| 344 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if ((start < avio_tell(s->pb)) || |
| 345 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
3 | (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP || |
| 346 | codec == AV_CODEC_ID_ADPCM_THP_LE))) | ||
| 347 | ✗ | return AVERROR_INVALIDDATA; | |
| 348 | 3 | avio_skip(s->pb, start - avio_tell(s->pb)); | |
| 349 | |||
| 350 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
3 | if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP || |
| 351 | codec == AV_CODEC_ID_ADPCM_THP_LE)) | ||
| 352 | 2 | avio_skip(s->pb, 24); | |
| 353 | |||
| 354 | 3 | b->data_start = avio_tell(s->pb); | |
| 355 | |||
| 356 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
3 | if (!bfstm && (major != 1 || minor)) |
| 357 | ✗ | avpriv_request_sample(s, "Version %d.%d", major, minor); | |
| 358 | |||
| 359 | 3 | return 0; | |
| 360 | ✗ | default: | |
| 361 | ✗ | av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk); | |
| 362 | ✗ | skip: | |
| 363 | ✗ | avio_skip(s->pb, size); | |
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | ✗ | return AVERROR_EOF; | |
| 368 | } | ||
| 369 | |||
| 370 | 30 | static int read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 371 | { | ||
| 372 | 30 | AVCodecParameters *par = s->streams[0]->codecpar; | |
| 373 | 30 | BRSTMDemuxContext *b = s->priv_data; | |
| 374 | 30 | uint32_t samples, size, skip = 0; | |
| 375 | 30 | int channels = par->ch_layout.nb_channels; | |
| 376 | int ret, i; | ||
| 377 | |||
| 378 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 29 times.
|
30 | if (avio_feof(s->pb)) |
| 379 | 1 | return AVERROR_EOF; | |
| 380 | 29 | b->current_block++; | |
| 381 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
|
29 | if (b->current_block == b->block_count) { |
| 382 | 2 | size = b->last_block_used_bytes; | |
| 383 | 2 | samples = b->last_block_samples; | |
| 384 | 2 | skip = b->last_block_size - b->last_block_used_bytes; | |
| 385 | |||
| 386 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (samples < size * 14 / 8) { |
| 387 | 1 | uint32_t adjusted_size = samples / 14 * 8; | |
| 388 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (samples % 14) |
| 389 | 1 | adjusted_size += (samples % 14 + 1) / 2 + 1; | |
| 390 | |||
| 391 | 1 | skip += size - adjusted_size; | |
| 392 | 1 | size = adjusted_size; | |
| 393 | } | ||
| 394 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
|
27 | } else if (b->current_block < b->block_count) { |
| 395 | 24 | size = b->block_size; | |
| 396 | 24 | samples = b->samples_per_block; | |
| 397 | } else { | ||
| 398 | 3 | return AVERROR_EOF; | |
| 399 | } | ||
| 400 | |||
| 401 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 21 times.
|
26 | if (par->codec_id == AV_CODEC_ID_ADPCM_THP || |
| 402 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) { |
| 403 | uint8_t *dst; | ||
| 404 | |||
| 405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (!b->adpc) { |
| 406 | ✗ | av_log(s, AV_LOG_ERROR, "adpcm_thp requires ADPC chunk, but none was found.\n"); | |
| 407 | 1 | return AVERROR_INVALIDDATA; | |
| 408 | } | ||
| 409 | |||
| 410 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | if (size > (INT_MAX - 32 - 4) || |
| 411 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | (32 + 4 + size) > (INT_MAX / channels) || |
| 412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | (32 + 4 + size) * channels > INT_MAX - 8) |
| 413 | ✗ | return AVERROR_INVALIDDATA; | |
| 414 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
|
26 | if ((ret = av_new_packet(pkt, 8 + (32 + 4 + size) * channels)) < 0) |
| 415 | ✗ | return ret; | |
| 416 | 26 | dst = pkt->data; | |
| 417 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 21 times.
|
26 | if (par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) { |
| 418 | 5 | bytestream_put_le32(&dst, size * channels); | |
| 419 | 5 | bytestream_put_le32(&dst, samples); | |
| 420 | } else { | ||
| 421 | 21 | bytestream_put_be32(&dst, size * channels); | |
| 422 | 21 | bytestream_put_be32(&dst, samples); | |
| 423 | } | ||
| 424 | 26 | bytestream_put_buffer(&dst, b->table, 32 * channels); | |
| 425 | 26 | bytestream_put_buffer(&dst, b->adpc + 4 * channels * | |
| 426 | 26 | (b->current_block - 1), 4 * channels); | |
| 427 | |||
| 428 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 25 times.
|
76 | for (i = 0; i < channels; i++) { |
| 429 | 51 | ret = ffio_read_size(s->pb, dst, size); | |
| 430 | 51 | dst += size; | |
| 431 | 51 | avio_skip(s->pb, skip); | |
| 432 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 50 times.
|
51 | if (ret < 0) { |
| 433 | 1 | return ret; | |
| 434 | } | ||
| 435 | } | ||
| 436 | 25 | pkt->duration = samples; | |
| 437 | } else { | ||
| 438 | ✗ | size *= channels; | |
| 439 | ✗ | ret = av_get_packet(s->pb, pkt, size); | |
| 440 | } | ||
| 441 | |||
| 442 | 25 | pkt->stream_index = 0; | |
| 443 | |||
| 444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (ret != size) |
| 445 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 446 | |||
| 447 | 25 | return ret; | |
| 448 | } | ||
| 449 | |||
| 450 | ✗ | static int read_seek(AVFormatContext *s, int stream_index, | |
| 451 | int64_t timestamp, int flags) | ||
| 452 | { | ||
| 453 | ✗ | AVStream *st = s->streams[stream_index]; | |
| 454 | ✗ | BRSTMDemuxContext *b = s->priv_data; | |
| 455 | ✗ | int64_t ret = 0; | |
| 456 | |||
| 457 | ✗ | if (timestamp < 0) | |
| 458 | ✗ | timestamp = 0; | |
| 459 | ✗ | timestamp /= b->samples_per_block; | |
| 460 | ✗ | if (timestamp >= b->block_count) | |
| 461 | ✗ | timestamp = b->block_count - 1; | |
| 462 | ✗ | ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size * | |
| 463 | ✗ | st->codecpar->ch_layout.nb_channels, SEEK_SET); | |
| 464 | ✗ | if (ret < 0) | |
| 465 | ✗ | return ret; | |
| 466 | |||
| 467 | ✗ | b->current_block = timestamp; | |
| 468 | ✗ | avpriv_update_cur_dts(s, st, timestamp * b->samples_per_block); | |
| 469 | ✗ | return 0; | |
| 470 | } | ||
| 471 | |||
| 472 | const FFInputFormat ff_brstm_demuxer = { | ||
| 473 | .p.name = "brstm", | ||
| 474 | .p.long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"), | ||
| 475 | .p.extensions = "brstm", | ||
| 476 | .priv_data_size = sizeof(BRSTMDemuxContext), | ||
| 477 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 478 | .read_probe = probe, | ||
| 479 | .read_header = read_header, | ||
| 480 | .read_packet = read_packet, | ||
| 481 | .read_close = read_close, | ||
| 482 | .read_seek = read_seek, | ||
| 483 | }; | ||
| 484 | |||
| 485 | const FFInputFormat ff_bfstm_demuxer = { | ||
| 486 | .p.name = "bfstm", | ||
| 487 | .p.long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"), | ||
| 488 | .p.extensions = "bfstm,bcstm", | ||
| 489 | .priv_data_size = sizeof(BRSTMDemuxContext), | ||
| 490 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 491 | .read_probe = probe_bfstm, | ||
| 492 | .read_header = read_header, | ||
| 493 | .read_packet = read_packet, | ||
| 494 | .read_close = read_close, | ||
| 495 | .read_seek = read_seek, | ||
| 496 | }; | ||
| 497 |