FFmpeg  4.0
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:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
static const char * format[]
Definition: af_aiir.c:311
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1450
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:4811
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:3884
int size
Definition: avcodec.h:1431
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT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AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1793
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
int version
Definition: avisynth_c.h:766
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3876
Format I/O context.
Definition: avformat.h:1342
#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:801
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4441
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
uint8_t * data
Definition: avcodec.h:1430
#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:310
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
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:770
#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:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
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:639
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int block_align
Audio only.
Definition: avcodec.h:4001
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:3271
#define AV_RL32
Definition: intreadwrite.h:146
Stream structure.
Definition: avformat.h:873
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:470
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:448
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
int sample_rate
Audio only.
Definition: avcodec.h:3994
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
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:3926
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3898
int channels
Audio only.
Definition: avcodec.h:3990
AVInputFormat ff_fsb_demuxer
Definition: fsb.c:202
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3888
int stream_index
Definition: avcodec.h:1432
This structure stores compressed data.
Definition: avcodec.h:1407