FFmpeg
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"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "avio.h"
26 #include "demux.h"
27 #include "internal.h"
28 
29 static 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;
43  AVCodecParameters *par;
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);
51  return AVERROR_PATCHWELCOME;
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)
70  return AVERROR_INVALIDDATA;
71  avio_skip(pb, 6);
72  par->ch_layout.nb_channels = avio_rl16(pb);
73  if (!par->ch_layout.nb_channels)
74  return AVERROR_INVALIDDATA;
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)
90  return AVERROR_INVALIDDATA;
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);
101  return AVERROR_PATCHWELCOME;
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:
114  par->codec_id = AV_CODEC_ID_XMA2;
115  break;
116  case 0x40000802:
118  break;
119  default:
120  avpriv_request_sample(s, "format 0x%X", format);
121  return AVERROR_PATCHWELCOME;
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 
129  par->ch_layout.nb_channels = avio_rl16(pb);
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 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:334
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:867
ff_fsb_demuxer
const FFInputFormat ff_fsb_demuxer
Definition: fsb.c:203
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
avassert.h
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
pkt
AVPacket * pkt
Definition: movenc.c:60
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
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
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
fsb_read_packet
static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: fsb.c:166
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
avio.h
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
version
version
Definition: libkvazaar.c:321
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
demux.h
fsb_probe
static int fsb_probe(const AVProbeData *p)
Definition: fsb.c:29
av_get_packet
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
fsb_read_header
static int fsb_read_header(AVFormatContext *s)
Definition: fsb.c:38
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AV_CODEC_ID_ADPCM_THP
@ AV_CODEC_ID_ADPCM_THP
Definition: codec_id.h:391
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AV_CODEC_ID_XMA2
@ AV_CODEC_ID_XMA2
Definition: codec_id.h:526
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:374
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_ADPCM_PSX
@ AV_CODEC_ID_ADPCM_PSX
Definition: codec_id.h:410
ff_alloc_extradata
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:227
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346