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