3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/bytestream.h"
27 typedef struct BRSTMDemuxContext {
30 uint32_t current_block;
31 uint32_t samples_per_block;
32 uint32_t last_block_used_bytes;
39 static int probe(AVProbeData *p)
41 if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
42 (AV_RL16(p->buf + 4) == 0xFFFE ||
43 AV_RL16(p->buf + 4) == 0xFEFF))
44 return AVPROBE_SCORE_MAX / 3 * 2;
48 static int probe_bfstm(AVProbeData *p)
50 if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') ||
51 AV_RL32(p->buf) == MKTAG('C','S','T','M')) &&
52 (AV_RL16(p->buf + 4) == 0xFFFE ||
53 AV_RL16(p->buf + 4) == 0xFEFF))
54 return AVPROBE_SCORE_MAX / 3 * 2;
58 static int read_close(AVFormatContext *s)
60 BRSTMDemuxContext *b = s->priv_data;
68 static av_always_inline unsigned int read16(AVFormatContext *s)
70 BRSTMDemuxContext *b = s->priv_data;
72 return avio_rl16(s->pb);
74 return avio_rb16(s->pb);
77 static av_always_inline unsigned int read32(AVFormatContext *s)
79 BRSTMDemuxContext *b = s->priv_data;
81 return avio_rl32(s->pb);
83 return avio_rb32(s->pb);
86 static int read_header(AVFormatContext *s)
88 BRSTMDemuxContext *b = s->priv_data;
89 int bom, major, minor, codec, chunk;
90 int64_t h1offset, pos, toffset, data_offset = 0;
91 uint32_t size, start, asize;
93 int ret = AVERROR_EOF;
95 b->bfstm = !strcmp("bfstm", s->iformat->name);
97 st = avformat_new_stream(s, NULL);
99 return AVERROR(ENOMEM);
100 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
104 bom = avio_rb16(s->pb);
105 if (bom != 0xFEFF && bom != 0xFFFE) {
106 av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
107 return AVERROR_INVALIDDATA;
111 b->little_endian = 1;
114 major = avio_r8(s->pb);
115 minor = avio_r8(s->pb);
116 avio_skip(s->pb, 4); // size of file
119 return AVERROR_INVALIDDATA;
121 avio_skip(s->pb, size - 14);
122 pos = avio_tell(s->pb);
123 if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
124 return AVERROR_INVALIDDATA;
126 uint32_t info_offset = 0, info_size;
127 uint16_t section_count, header_size, i;
129 header_size = read16(s); // 6
131 avio_skip(s->pb, 4); // Unknown constant 0x00030000
132 avio_skip(s->pb, 4); // size of file
133 section_count = read16(s);
134 avio_skip(s->pb, 2); // padding
135 for (i = 0; avio_tell(s->pb) < header_size
136 && !(data_offset && info_offset)
137 && i < section_count; i++) {
138 uint16_t flag = read16(s);
142 info_offset = read32(s);
143 info_size = read32(s);
146 avio_skip(s->pb, 4); // seek offset
147 avio_skip(s->pb, 4); // seek size
150 data_offset = read32(s);
151 avio_skip(s->pb, 4); //data_size = read32(s);
154 avio_skip(s->pb, 4); // REGN offset
155 avio_skip(s->pb, 4); // REGN size
160 if (!info_offset || !data_offset)
161 return AVERROR_INVALIDDATA;
163 start = data_offset + 8;
165 avio_skip(s->pb, info_offset - avio_tell(s->pb));
166 pos = avio_tell(s->pb);
167 if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
168 return AVERROR_INVALIDDATA;
173 return AVERROR_INVALIDDATA;
174 avio_skip(s->pb, 4); // unknown
175 h1offset = read32(s);
177 return AVERROR_INVALIDDATA;
178 avio_skip(s->pb, 12);
179 toffset = read32(s) + 16LL;
181 return AVERROR_INVALIDDATA;
183 avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
184 codec = avio_r8(s->pb);
187 case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
188 case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
189 case 2: codec = b->little_endian ?
190 AV_CODEC_ID_ADPCM_THP_LE :
191 AV_CODEC_ID_ADPCM_THP; break;
193 avpriv_request_sample(s, "codec %d", codec);
194 return AVERROR_PATCHWELCOME;
197 avio_skip(s->pb, 1); // loop flag
198 st->codec->codec_id = codec;
199 st->codec->channels = avio_r8(s->pb);
200 if (!st->codec->channels)
201 return AVERROR_INVALIDDATA;
203 avio_skip(s->pb, 1); // padding
205 st->codec->sample_rate = b->bfstm ? read32(s) : read16(s);
206 if (!st->codec->sample_rate)
207 return AVERROR_INVALIDDATA;
210 avio_skip(s->pb, 2); // padding
211 avio_skip(s->pb, 4); // loop start sample
213 st->duration = read32(s);
214 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
218 b->current_block = 0;
219 b->block_count = read32(s);
220 if (b->block_count > UINT16_MAX) {
221 av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
222 return AVERROR_INVALIDDATA;
225 b->block_size = read32(s);
226 if (b->block_size > UINT16_MAX / st->codec->channels)
227 return AVERROR_INVALIDDATA;
228 b->block_size *= st->codec->channels;
230 b->samples_per_block = read32(s);
231 b->last_block_used_bytes = read32(s);
232 if (b->last_block_used_bytes > UINT16_MAX / st->codec->channels)
233 return AVERROR_INVALIDDATA;
234 b->last_block_used_bytes *= st->codec->channels;
236 avio_skip(s->pb, 4); // last block samples
237 avio_skip(s->pb, 4); // last block size
239 if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
242 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
244 toffset = read32(s) + 16LL;
246 toffset = toffset + read32(s) + st->codec->channels * 8 - 8;
248 return AVERROR_INVALIDDATA;
250 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
251 b->table = av_mallocz(32 * st->codec->channels);
253 return AVERROR(ENOMEM);
255 for (ch = 0; ch < st->codec->channels; ch++) {
256 if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
257 ret = AVERROR_INVALIDDATA;
260 avio_skip(s->pb, b->bfstm ? 14 : 24);
264 st->codec->extradata_size = 32 * st->codec->channels;
265 st->codec->extradata = av_malloc(st->codec->extradata_size);
266 if (!st->codec->extradata)
267 return AVERROR(ENOMEM);
268 memcpy(st->codec->extradata, b->table, st->codec->extradata_size);
272 if (size < (avio_tell(s->pb) - pos)) {
273 ret = AVERROR_INVALIDDATA;
278 avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
280 avio_skip(s->pb, data_offset - avio_tell(s->pb));
282 while (!avio_feof(s->pb)) {
283 chunk = avio_rl32(s->pb);
286 ret = AVERROR_INVALIDDATA;
291 case MKTAG('A','D','P','C'):
292 if (codec != AV_CODEC_ID_ADPCM_THP &&
293 codec != AV_CODEC_ID_ADPCM_THP_LE)
296 asize = b->block_count * st->codec->channels * 4;
298 ret = AVERROR_INVALIDDATA;
302 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
305 b->adpc = av_mallocz(asize);
307 ret = AVERROR(ENOMEM);
310 avio_read(s->pb, b->adpc, asize);
311 avio_skip(s->pb, size - asize);
314 case MKTAG('D','A','T','A'):
315 if ((start < avio_tell(s->pb)) ||
316 (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
317 codec == AV_CODEC_ID_ADPCM_THP_LE)
319 ret = AVERROR_INVALIDDATA;
322 avio_skip(s->pb, start - avio_tell(s->pb));
324 if ((major != 1 || minor) && !b->bfstm)
325 avpriv_request_sample(s, "Version %d.%d", major, minor);
329 av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
331 avio_skip(s->pb, size);
341 static int read_packet(AVFormatContext *s, AVPacket *pkt)
343 AVCodecContext *codec = s->streams[0]->codec;
344 BRSTMDemuxContext *b = s->priv_data;
345 uint32_t samples, size;
348 if (avio_feof(s->pb))
351 if (b->current_block == b->block_count) {
352 size = b->last_block_used_bytes;
353 samples = size / (8 * codec->channels) * 14;
354 } else if (b->current_block < b->block_count) {
355 size = b->block_size;
356 samples = b->samples_per_block;
361 if ((codec->codec_id == AV_CODEC_ID_ADPCM_THP ||
362 codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) && !codec->extradata) {
365 if (av_new_packet(pkt, 8 + (32 + 4) * codec->channels + size) < 0)
366 return AVERROR(ENOMEM);
368 bytestream_put_be32(&dst, size);
369 bytestream_put_be32(&dst, samples);
370 bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
371 bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
372 (b->current_block - 1), 4 * codec->channels);
374 ret = avio_read(s->pb, dst, size);
377 pkt->duration = samples;
379 ret = av_get_packet(s->pb, pkt, size);
382 pkt->stream_index = 0;
390 AVInputFormat ff_brstm_demuxer = {
392 .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
393 .priv_data_size = sizeof(BRSTMDemuxContext),
395 .read_header = read_header,
396 .read_packet = read_packet,
397 .read_close = read_close,
398 .extensions = "brstm",
401 AVInputFormat ff_bfstm_demuxer = {
403 .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
404 .priv_data_size = sizeof(BRSTMDemuxContext),
405 .read_probe = probe_bfstm,
406 .read_header = read_header,
407 .read_packet = read_packet,
408 .read_close = read_close,
409 .extensions = "bfstm,bcstm",