| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * FSB demuxer | ||
| 3 | * Copyright (c) 2015 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/avassert.h" | ||
| 23 | #include "libavutil/intreadwrite.h" | ||
| 24 | #include "avformat.h" | ||
| 25 | #include "avio.h" | ||
| 26 | #include "avio_internal.h" | ||
| 27 | #include "demux.h" | ||
| 28 | #include "internal.h" | ||
| 29 | |||
| 30 | 7957 | static int fsb_probe(const AVProbeData *p) | |
| 31 | { | ||
| 32 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 7957 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
7957 | if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5) |
| 33 | 7957 | return 0; | |
| 34 | ✗ | if (AV_RL32(p->buf + 4) != 1) | |
| 35 | ✗ | return 0; | |
| 36 | ✗ | return AVPROBE_SCORE_MAX; | |
| 37 | } | ||
| 38 | |||
| 39 | ✗ | static int fsb_read_header(AVFormatContext *s) | |
| 40 | { | ||
| 41 | ✗ | AVIOContext *pb = s->pb; | |
| 42 | unsigned format, version, c; | ||
| 43 | int64_t offset; | ||
| 44 | AVCodecParameters *par; | ||
| 45 | ✗ | AVStream *st = avformat_new_stream(s, NULL); | |
| 46 | int ret; | ||
| 47 | |||
| 48 | ✗ | avio_skip(pb, 3); // "FSB" | |
| 49 | ✗ | version = avio_r8(pb) - '0'; | |
| 50 | ✗ | if (version != 4 && version != 3) { | |
| 51 | ✗ | avpriv_request_sample(s, "version %d", version); | |
| 52 | ✗ | return AVERROR_PATCHWELCOME; | |
| 53 | } | ||
| 54 | |||
| 55 | ✗ | avio_skip(pb, 4); | |
| 56 | |||
| 57 | ✗ | if (!st) | |
| 58 | ✗ | return AVERROR(ENOMEM); | |
| 59 | ✗ | par = st->codecpar; | |
| 60 | ✗ | par->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 61 | ✗ | par->codec_tag = 0; | |
| 62 | |||
| 63 | ✗ | if (version == 3) { | |
| 64 | ✗ | offset = avio_rl32(pb) + 0x18; | |
| 65 | ✗ | avio_skip(pb, 44); | |
| 66 | ✗ | st->duration = avio_rl32(pb); | |
| 67 | ✗ | avio_skip(pb, 12); | |
| 68 | ✗ | format = avio_rl32(pb); | |
| 69 | ✗ | par->sample_rate = avio_rl32(pb); | |
| 70 | ✗ | if (par->sample_rate <= 0) | |
| 71 | ✗ | return AVERROR_INVALIDDATA; | |
| 72 | ✗ | avio_skip(pb, 6); | |
| 73 | ✗ | par->ch_layout.nb_channels = avio_rl16(pb); | |
| 74 | ✗ | if (!par->ch_layout.nb_channels) | |
| 75 | ✗ | return AVERROR_INVALIDDATA; | |
| 76 | |||
| 77 | ✗ | if (format & 0x00000100) { | |
| 78 | ✗ | par->codec_id = AV_CODEC_ID_PCM_S16LE; | |
| 79 | ✗ | par->block_align = 4096 * par->ch_layout.nb_channels; | |
| 80 | ✗ | } else if (format & 0x00400000) { | |
| 81 | ✗ | par->bits_per_coded_sample = 4; | |
| 82 | ✗ | par->codec_id = AV_CODEC_ID_ADPCM_IMA_WAV; | |
| 83 | ✗ | par->block_align = 36 * par->ch_layout.nb_channels; | |
| 84 | ✗ | } else if (format & 0x00800000) { | |
| 85 | ✗ | par->codec_id = AV_CODEC_ID_ADPCM_PSX; | |
| 86 | ✗ | par->block_align = 16 * par->ch_layout.nb_channels; | |
| 87 | ✗ | } else if (format & 0x02000000) { | |
| 88 | ✗ | par->codec_id = AV_CODEC_ID_ADPCM_THP; | |
| 89 | ✗ | par->block_align = 8 * par->ch_layout.nb_channels; | |
| 90 | ✗ | if (par->ch_layout.nb_channels > INT_MAX / 32) | |
| 91 | ✗ | return AVERROR_INVALIDDATA; | |
| 92 | ✗ | ret = ff_alloc_extradata(par, 32 * par->ch_layout.nb_channels); | |
| 93 | ✗ | if (ret < 0) | |
| 94 | ✗ | return ret; | |
| 95 | ✗ | avio_seek(pb, 0x68, SEEK_SET); | |
| 96 | ✗ | for (c = 0; c < par->ch_layout.nb_channels; c++) { | |
| 97 | ✗ | ret = ffio_read_size(pb, par->extradata + 32 * c, 32); | |
| 98 | ✗ | if (ret < 0) | |
| 99 | ✗ | return ret; | |
| 100 | ✗ | avio_skip(pb, 14); | |
| 101 | } | ||
| 102 | } else { | ||
| 103 | ✗ | avpriv_request_sample(s, "format 0x%X", format); | |
| 104 | ✗ | return AVERROR_PATCHWELCOME; | |
| 105 | } | ||
| 106 | ✗ | } else if (version == 4) { | |
| 107 | ✗ | offset = avio_rl32(pb) + 0x30; | |
| 108 | ✗ | avio_skip(pb, 80); | |
| 109 | ✗ | st->duration = avio_rl32(pb); | |
| 110 | |||
| 111 | ✗ | format = avio_rb32(pb); | |
| 112 | ✗ | switch(format) { | |
| 113 | ✗ | case 0x40001001: | |
| 114 | case 0x00001005: | ||
| 115 | case 0x40001081: | ||
| 116 | case 0x40200001: | ||
| 117 | ✗ | par->codec_id = AV_CODEC_ID_XMA2; | |
| 118 | ✗ | break; | |
| 119 | ✗ | case 0x40000802: | |
| 120 | ✗ | par->codec_id = AV_CODEC_ID_ADPCM_THP; | |
| 121 | ✗ | break; | |
| 122 | ✗ | default: | |
| 123 | ✗ | avpriv_request_sample(s, "format 0x%X", format); | |
| 124 | ✗ | return AVERROR_PATCHWELCOME; | |
| 125 | } | ||
| 126 | |||
| 127 | ✗ | par->sample_rate = avio_rl32(pb); | |
| 128 | ✗ | if (par->sample_rate <= 0) | |
| 129 | ✗ | return AVERROR_INVALIDDATA; | |
| 130 | ✗ | avio_skip(pb, 6); | |
| 131 | |||
| 132 | ✗ | par->ch_layout.nb_channels = avio_rl16(pb); | |
| 133 | ✗ | if (!par->ch_layout.nb_channels) | |
| 134 | ✗ | return AVERROR_INVALIDDATA; | |
| 135 | |||
| 136 | ✗ | switch (par->codec_id) { | |
| 137 | ✗ | case AV_CODEC_ID_XMA2: | |
| 138 | ✗ | ret = ff_alloc_extradata(par, 34); | |
| 139 | ✗ | if (ret < 0) | |
| 140 | ✗ | return ret; | |
| 141 | ✗ | memset(par->extradata, 0, 34); | |
| 142 | ✗ | par->block_align = 2048; | |
| 143 | ✗ | break; | |
| 144 | ✗ | case AV_CODEC_ID_ADPCM_THP: | |
| 145 | ✗ | if (par->ch_layout.nb_channels > INT_MAX / 32) | |
| 146 | ✗ | return AVERROR_INVALIDDATA; | |
| 147 | ✗ | ret = ff_alloc_extradata(par, 32 * par->ch_layout.nb_channels); | |
| 148 | ✗ | if (ret < 0) | |
| 149 | ✗ | return ret; | |
| 150 | ✗ | avio_seek(pb, 0x80, SEEK_SET); | |
| 151 | ✗ | for (c = 0; c < par->ch_layout.nb_channels; c++) { | |
| 152 | ✗ | ret = ffio_read_size(pb, par->extradata + 32 * c, 32); | |
| 153 | ✗ | if (ret < 0) | |
| 154 | ✗ | return ret; | |
| 155 | ✗ | avio_skip(pb, 14); | |
| 156 | } | ||
| 157 | ✗ | par->block_align = 8 * par->ch_layout.nb_channels; | |
| 158 | ✗ | break; | |
| 159 | } | ||
| 160 | } else { | ||
| 161 | ✗ | av_assert0(0); | |
| 162 | } | ||
| 163 | |||
| 164 | ✗ | avio_skip(pb, offset - avio_tell(pb)); | |
| 165 | |||
| 166 | ✗ | avpriv_set_pts_info(st, 64, 1, par->sample_rate); | |
| 167 | |||
| 168 | ✗ | return 0; | |
| 169 | } | ||
| 170 | |||
| 171 | ✗ | static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 172 | { | ||
| 173 | ✗ | AVCodecParameters *par = s->streams[0]->codecpar; | |
| 174 | int64_t pos; | ||
| 175 | int ret; | ||
| 176 | |||
| 177 | ✗ | if (avio_feof(s->pb)) | |
| 178 | ✗ | return AVERROR_EOF; | |
| 179 | |||
| 180 | ✗ | pos = avio_tell(s->pb); | |
| 181 | ✗ | if (par->codec_id == AV_CODEC_ID_ADPCM_THP && | |
| 182 | ✗ | par->ch_layout.nb_channels > 1) { | |
| 183 | int i, ch; | ||
| 184 | |||
| 185 | ✗ | ret = av_new_packet(pkt, par->block_align); | |
| 186 | ✗ | if (ret < 0) | |
| 187 | ✗ | return ret; | |
| 188 | ✗ | for (i = 0; i < 4; i++) { | |
| 189 | ✗ | for (ch = 0; ch < par->ch_layout.nb_channels; ch++) { | |
| 190 | ✗ | pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb); | |
| 191 | ✗ | pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb); | |
| 192 | } | ||
| 193 | } | ||
| 194 | ✗ | ret = 0; | |
| 195 | } else { | ||
| 196 | ✗ | ret = av_get_packet(s->pb, pkt, par->block_align); | |
| 197 | } | ||
| 198 | |||
| 199 | ✗ | if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1) | |
| 200 | ✗ | pkt->duration = (pkt->data[0] >> 2) * 512; | |
| 201 | |||
| 202 | ✗ | pkt->pos = pos; | |
| 203 | ✗ | pkt->stream_index = 0; | |
| 204 | |||
| 205 | ✗ | return ret; | |
| 206 | } | ||
| 207 | |||
| 208 | const FFInputFormat ff_fsb_demuxer = { | ||
| 209 | .p.name = "fsb", | ||
| 210 | .p.long_name = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"), | ||
| 211 | .p.extensions = "fsb", | ||
| 212 | .p.flags = AVFMT_GENERIC_INDEX, | ||
| 213 | .read_probe = fsb_probe, | ||
| 214 | .read_header = fsb_read_header, | ||
| 215 | .read_packet = fsb_read_packet, | ||
| 216 | }; | ||
| 217 |