FFmpeg  4.4.9
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 "avio_internal.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->channels = avio_rl16(pb);
73  if (!par->channels)
74  return AVERROR_INVALIDDATA;
75 
76  if (format & 0x00000100) {
78  par->block_align = 4096 * par->channels;
79  } else if (format & 0x00400000) {
80  par->bits_per_coded_sample = 4;
82  par->block_align = 36 * par->channels;
83  } else if (format & 0x00800000) {
85  par->block_align = 16 * par->channels;
86  } else if (format & 0x02000000) {
88  par->block_align = 8 * par->channels;
89  if (par->channels > INT_MAX / 32)
90  return AVERROR_INVALIDDATA;
91  ret = ff_alloc_extradata(par, 32 * par->channels);
92  if (ret < 0)
93  return ret;
94  avio_seek(pb, 0x68, SEEK_SET);
95  for (c = 0; c < par->channels; c++) {
96  ret = ffio_read_size(pb, par->extradata + 32 * c, 32);
97  if (ret < 0)
98  return ret;
99  avio_skip(pb, 14);
100  }
101  } else {
102  avpriv_request_sample(s, "format 0x%X", format);
103  return AVERROR_PATCHWELCOME;
104  }
105  } else if (version == 4) {
106  offset = avio_rl32(pb) + 0x30;
107  avio_skip(pb, 80);
108  st->duration = avio_rl32(pb);
109 
110  format = avio_rb32(pb);
111  switch(format) {
112  case 0x40001001:
113  case 0x00001005:
114  case 0x40001081:
115  case 0x40200001:
116  par->codec_id = AV_CODEC_ID_XMA2;
117  break;
118  case 0x40000802:
120  break;
121  default:
122  avpriv_request_sample(s, "format 0x%X", format);
123  return AVERROR_PATCHWELCOME;
124  }
125 
126  par->sample_rate = avio_rl32(pb);
127  if (par->sample_rate <= 0)
128  return AVERROR_INVALIDDATA;
129  avio_skip(pb, 6);
130 
131  par->channels = avio_rl16(pb);
132  if (!par->channels)
133  return AVERROR_INVALIDDATA;
134 
135  switch (par->codec_id) {
136  case AV_CODEC_ID_XMA2:
137  ret = ff_alloc_extradata(par, 34);
138  if (ret < 0)
139  return ret;
140  memset(par->extradata, 0, 34);
141  par->block_align = 2048;
142  break;
144  if (par->channels > INT_MAX / 32)
145  return AVERROR_INVALIDDATA;
146  ret = ff_alloc_extradata(par, 32 * par->channels);
147  if (ret < 0)
148  return ret;
149  avio_seek(pb, 0x80, SEEK_SET);
150  for (c = 0; c < par->channels; c++) {
151  ret = ffio_read_size(pb, par->extradata + 32 * c, 32);
152  if (ret < 0)
153  return ret;
154  avio_skip(pb, 14);
155  }
156  par->block_align = 8 * par->channels;
157  break;
158  }
159  } else {
160  av_assert0(0);
161  }
162 
163  avio_skip(pb, offset - avio_tell(pb));
164  s->internal->data_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->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->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  .name = "fsb",
210  .long_name = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"),
211  .read_probe = fsb_probe,
212  .read_header = fsb_read_header,
213  .read_packet = fsb_read_packet,
214  .extensions = "fsb",
215  .flags = AVFMT_GENERIC_INDEX,
216 };
static const char *const format[]
Definition: af_aiir.c:457
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:37
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
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:310
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
Buffered I/O operations.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:371
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:750
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:797
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:698
#define AV_RL32
Definition: intreadwrite.h:146
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
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:171
AVInputFormat ff_fsb_demuxer
Definition: fsb.c:208
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
@ AV_CODEC_ID_XMA2
Definition: codec_id.h:505
@ AV_CODEC_ID_ADPCM_PSX
Definition: codec_id.h:391
@ AV_CODEC_ID_ADPCM_THP
Definition: codec_id.h:371
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:354
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
int i
Definition: input.c:407
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4944
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:3314
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
version
Definition: libkvazaar.c:326
unsigned int pos
Definition: spdifenc.c:412
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
#define avpriv_request_sample(...)
AVPacket * pkt
Definition: movenc.c:59
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
static double c[64]