FFmpeg
Loading...
Searching...
No Matches
fsb.c
Go to the documentation of this file.
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"
24#include "avformat.h"
25#include "avio.h"
26#include "avio_internal.h"
27#include "demux.h"
28#include "internal.h"
29
30static int fsb_probe(const AVProbeData *p)
31{
32 if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5)
33 return 0;
34 if (AV_RL32(p->buf + 4) != 1)
35 return 0;
36 return AVPROBE_SCORE_MAX;
37}
38
40{
41 AVIOContext *pb = s->pb;
42 unsigned format, version, c;
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);
53 }
54
55 avio_skip(pb, 4);
56
57 if (!st)
58 return AVERROR(ENOMEM);
59 par = st->codecpar;
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)
72 avio_skip(pb, 6);
74 if (!par->ch_layout.nb_channels)
76
77 if (format & 0x00000100) {
79 par->block_align = 4096 * par->ch_layout.nb_channels;
80 } else if (format & 0x00400000) {
81 par->bits_per_coded_sample = 4;
83 par->block_align = 36 * par->ch_layout.nb_channels;
84 } else if (format & 0x00800000) {
86 par->block_align = 16 * par->ch_layout.nb_channels;
87 } else if (format & 0x02000000) {
89 par->block_align = 8 * par->ch_layout.nb_channels;
90 if (par->ch_layout.nb_channels > INT_MAX / 32)
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);
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:
118 break;
119 case 0x40000802:
121 break;
122 default:
123 avpriv_request_sample(s, "format 0x%X", format);
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
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;
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
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
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};
static const char *const format[]
Definition af_aiir.c:445
const FFInputFormat ff_fsb_demuxer
Definition fsb.c:208
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
Buffered I/O operations.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int fsb_probe(const AVProbeData *p)
Definition fsb.c:30
static int fsb_read_header(AVFormatContext *s)
Definition fsb.c:39
static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition fsb.c:171
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_XMA2
Definition codec_id.h:533
@ AV_CODEC_ID_ADPCM_PSX
Definition codec_id.h:407
@ AV_CODEC_ID_ADPCM_THP
Definition codec_id.h:388
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition codec_id.h:371
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
version
Definition libkvazaar.c:313
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
#define avpriv_request_sample(...)
static double c[64]