3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "libavutil/intreadwrite.h"
28 static int fsb_probe(AVProbeData *p)
30 if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5)
32 if (AV_RL32(p->buf + 4) != 1)
34 return AVPROBE_SCORE_MAX;
37 static int fsb_read_header(AVFormatContext *s)
39 AVIOContext *pb = s->pb;
40 unsigned format, version, c;
42 AVCodecContext *codec;
43 AVStream *st = avformat_new_stream(s, NULL);
45 avio_skip(pb, 3); // "FSB"
46 version = avio_r8(pb) - '0';
47 if (version != 4 && version != 3) {
48 avpriv_request_sample(s, "version %d", version);
49 return AVERROR_PATCHWELCOME;
55 return AVERROR(ENOMEM);
57 codec->codec_type = AVMEDIA_TYPE_AUDIO;
61 offset = avio_rl32(pb) + 0x18;
63 st->duration = avio_rl32(pb);
65 format = avio_rl32(pb);
66 codec->sample_rate = avio_rl32(pb);
67 if (codec->sample_rate <= 0)
68 return AVERROR_INVALIDDATA;
70 codec->channels = avio_rl16(pb);
72 return AVERROR_INVALIDDATA;
74 if (format & 0x00000100) {
75 codec->codec_id = AV_CODEC_ID_PCM_S16LE;
76 codec->block_align = 4096 * codec->channels;
77 } else if (format & 0x00400000) {
78 codec->bits_per_coded_sample = 4;
79 codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WAV;
80 codec->block_align = 36 * codec->channels;
81 } else if (format & 0x00800000) {
82 codec->codec_id = AV_CODEC_ID_ADPCM_PSX;
83 codec->block_align = 16 * codec->channels;
84 } else if (format & 0x02000000) {
85 codec->codec_id = AV_CODEC_ID_ADPCM_THP;
86 codec->block_align = 8 * codec->channels;
87 if (codec->channels > INT_MAX / 32)
88 return AVERROR_INVALIDDATA;
89 ff_alloc_extradata(codec, 32 * codec->channels);
90 if (!codec->extradata)
91 return AVERROR(ENOMEM);
92 avio_seek(pb, 0x68, SEEK_SET);
93 for (c = 0; c < codec->channels; c++) {
94 avio_read(pb, codec->extradata + 32 * c, 32);
98 avpriv_request_sample(s, "format 0x%X", format);
99 return AVERROR_PATCHWELCOME;
101 } else if (version == 4) {
102 offset = avio_rl32(pb) + 0x30;
104 st->duration = avio_rl32(pb);
106 format = avio_rb32(pb);
112 codec->codec_id = AV_CODEC_ID_XMA2;
115 codec->codec_id = AV_CODEC_ID_ADPCM_THP;
118 avpriv_request_sample(s, "format 0x%X", format);
119 return AVERROR_PATCHWELCOME;
122 codec->sample_rate = avio_rl32(pb);
123 if (codec->sample_rate <= 0)
124 return AVERROR_INVALIDDATA;
127 codec->channels = avio_rl16(pb);
128 if (!codec->channels)
129 return AVERROR_INVALIDDATA;
131 switch (codec->codec_id) {
132 case AV_CODEC_ID_XMA2:
133 ff_alloc_extradata(codec, 34);
134 if (!codec->extradata)
135 return AVERROR(ENOMEM);
136 memset(codec->extradata, 0, 34);
137 codec->block_align = 2048;
139 case AV_CODEC_ID_ADPCM_THP:
140 if (codec->channels > INT_MAX / 32)
141 return AVERROR_INVALIDDATA;
142 ff_alloc_extradata(codec, 32 * codec->channels);
143 if (!codec->extradata)
144 return AVERROR(ENOMEM);
145 avio_seek(pb, 0x80, SEEK_SET);
146 for (c = 0; c < codec->channels; c++) {
147 avio_read(pb, codec->extradata + 32 * c, 32);
150 codec->block_align = 8 * codec->channels;
157 avio_skip(pb, offset - avio_tell(pb));
158 s->internal->data_offset = avio_tell(pb);
160 avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
165 static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
167 AVCodecContext *codec = s->streams[0]->codec;
171 if (avio_feof(s->pb))
174 pos = avio_tell(s->pb);
175 if (codec->codec_id == AV_CODEC_ID_ADPCM_THP &&
176 codec->channels > 1) {
179 ret = av_new_packet(pkt, codec->block_align);
182 for (i = 0; i < 4; i++) {
183 for (ch = 0; ch < codec->channels; ch++) {
184 pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
185 pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
190 ret = av_get_packet(s->pb, pkt, codec->block_align);
193 if (codec->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
194 pkt->duration = (pkt->data[0] >> 2) * 512;
197 pkt->stream_index = 0;
202 AVInputFormat ff_fsb_demuxer = {
204 .long_name = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"),
205 .read_probe = fsb_probe,
206 .read_header = fsb_read_header,
207 .read_packet = fsb_read_packet,
209 .flags = AVFMT_GENERIC_INDEX,