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