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  AVCodecContext *codec;
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  codec = st->codec;
58  codec->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  codec->sample_rate = avio_rl32(pb);
67  if (codec->sample_rate <= 0)
68  return AVERROR_INVALIDDATA;
69  avio_skip(pb, 6);
70  codec->channels = avio_rl16(pb);
71  if (!codec->channels)
72  return AVERROR_INVALIDDATA;
73 
74  if (format & 0x00000100) {
76  codec->block_align = 4096 * codec->channels;
77  } else if (format & 0x00400000) {
78  codec->bits_per_coded_sample = 4;
80  codec->block_align = 36 * codec->channels;
81  } else if (format & 0x00800000) {
83  codec->block_align = 16 * codec->channels;
84  } else if (format & 0x02000000) {
86  codec->block_align = 8 * codec->channels;
87  if (codec->channels > INT_MAX / 32)
88  return AVERROR_INVALIDDATA;
89  ff_alloc_extradata(codec, 32 * codec->channels);
90  if (!codec->extradata)
91  return AVERROR(ENOMEM);
92  avio_seek(pb, 0x68, SEEK_SET);
93  for (c = 0; c < codec->channels; c++) {
94  avio_read(pb, codec->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  codec->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  codec->sample_rate = avio_rl32(pb);
123  if (codec->sample_rate <= 0)
124  return AVERROR_INVALIDDATA;
125  avio_skip(pb, 6);
126 
127  codec->channels = avio_rl16(pb);
128  if (!codec->channels)
129  return AVERROR_INVALIDDATA;
130 
131  switch (codec->codec_id) {
132  case AV_CODEC_ID_XMA2:
133  ff_alloc_extradata(codec, 34);
134  if (!codec->extradata)
135  return AVERROR(ENOMEM);
136  memset(codec->extradata, 0, 34);
137  codec->block_align = 2048;
138  break;
140  if (codec->channels > INT_MAX / 32)
141  return AVERROR_INVALIDDATA;
142  ff_alloc_extradata(codec, 32 * codec->channels);
143  if (!codec->extradata)
144  return AVERROR(ENOMEM);
145  avio_seek(pb, 0x80, SEEK_SET);
146  for (c = 0; c < codec->channels; c++) {
147  avio_read(pb, codec->extradata + 32 * c, 32);
148  avio_skip(pb, 14);
149  }
150  codec->block_align = 8 * codec->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, codec->sample_rate);
161 
162  return 0;
163 }
164 
166 {
167  AVCodecContext *codec = s->streams[0]->codec;
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 (codec->codec_id == AV_CODEC_ID_ADPCM_THP &&
176  codec->channels > 1) {
177  int i, ch;
178 
179  ret = av_new_packet(pkt, codec->block_align);
180  if (ret < 0)
181  return ret;
182  for (i = 0; i < 4; i++) {
183  for (ch = 0; ch < codec->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, codec->block_align);
191  }
192 
193  if (codec->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:111
#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:1487
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:4149
int64_t data_offset
offset of the first packet
Definition: internal.h:80
int size
Definition: avcodec.h:1468
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1741
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
int version
Definition: avisynth_c.h:629
static AVPacket pkt
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2324
Format I/O context.
Definition: avformat.h:1314
#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:698
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
uint8_t * data
Definition: avcodec.h:1467
#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:252
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
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:667
#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
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:536
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
Stream structure.
Definition: avformat.h:877
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
int sample_rate
samples per second
Definition: avcodec.h:2287
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
main external API structure.
Definition: avcodec.h:1532
static const char * format
Definition: movenc-test.c:47
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2979
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:486
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:460
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:936
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:651
Main libavformat public API header.
static int fsb_read_header(AVFormatContext *s)
Definition: fsb.c:37
static double c[64]
int channels
number of audio channels
Definition: avcodec.h:2288
AVInputFormat ff_fsb_demuxer
Definition: fsb.c:202
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:306
int stream_index
Definition: avcodec.h:1469
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1444