FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
27 
28 static 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 
38 {
39  AVIOContext *pb = s->pb;
40  unsigned format, version, c;
41  int64_t offset;
42  AVCodecParameters *par;
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;
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) {
76  par->block_align = 4096 * par->channels;
77  } else if (format & 0x00400000) {
78  par->bits_per_coded_sample = 4;
80  par->block_align = 36 * par->channels;
81  } else if (format & 0x00800000) {
83  par->block_align = 16 * par->channels;
84  } else if (format & 0x02000000) {
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:
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;
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 
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 
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 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1600
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:4427
int64_t data_offset
offset of the first packet
Definition: internal.h:82
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3922
int size
Definition: avcodec.h:1581
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1752
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:304
int version
Definition: avisynth_c.h:629
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3914
Format I/O context.
Definition: avformat.h:1325
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:751
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1598
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4065
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
uint8_t * data
Definition: avcodec.h:1580
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:260
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:598
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:720
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3918
simple assert() macros that are a bit more flexible than ISO C assert().
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:589
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:461
int block_align
Audio only.
Definition: avcodec.h:4039
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:3107
Stream structure.
Definition: avformat.h:876
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
static const char * format
Definition: movenc.c:47
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:485
static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: fsb.c:165
static int fsb_probe(AVProbeData *p)
Definition: fsb.c:28
This structure contains the data a format has to probe a file.
Definition: avformat.h:459
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:930
int sample_rate
Audio only.
Definition: avcodec.h:4032
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:471
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:704
Main libavformat public API header.
static int fsb_read_header(AVFormatContext *s)
Definition: fsb.c:37
static double c[64]
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3964
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3936
int channels
Audio only.
Definition: avcodec.h:4028
AVInputFormat ff_fsb_demuxer
Definition: fsb.c:202
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:660
AVCodecParameters * codecpar
Definition: avformat.h:1006
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:328
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3926
int stream_index
Definition: avcodec.h:1582
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1557