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;
38 static int probe(AVProbeData *p)
40 if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
41 (AV_RL16(p->buf + 4) == 0xFFFE ||
42 AV_RL16(p->buf + 4) == 0xFEFF))
43 return AVPROBE_SCORE_MAX / 3 * 2;
47 static int probe_bfstm(AVProbeData *p)
49 if (AV_RL32(p->buf) == MKTAG('F','S','T','M') &&
50 (AV_RL16(p->buf + 4) == 0xFFFE ||
51 AV_RL16(p->buf + 4) == 0xFEFF))
52 return AVPROBE_SCORE_MAX / 3 * 2;
56 static int read_close(AVFormatContext *s)
58 BRSTMDemuxContext *b = s->priv_data;
66 static int read_header(AVFormatContext *s)
68 BRSTMDemuxContext *b = s->priv_data;
69 int bom, major, minor, codec, chunk;
70 int64_t h1offset, pos, toffset, data_offset = 0;
71 uint32_t size, start, asize;
73 int ret = AVERROR_EOF;
75 b->bfstm = !strcmp("bfstm", s->iformat->name);
77 st = avformat_new_stream(s, NULL);
79 return AVERROR(ENOMEM);
80 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
84 bom = avio_rb16(s->pb);
85 if (bom != 0xFEFF && bom != 0xFFFE) {
86 av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
87 return AVERROR_INVALIDDATA;
90 avpriv_request_sample(s, "little endian byte order");
91 return AVERROR_PATCHWELCOME;
95 major = avio_r8(s->pb);
96 minor = avio_r8(s->pb);
97 avio_skip(s->pb, 4); // size of file
98 size = avio_rb16(s->pb);
100 return AVERROR_INVALIDDATA;
102 avio_skip(s->pb, size - 14);
103 pos = avio_tell(s->pb);
104 if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
105 return AVERROR_INVALIDDATA;
107 uint32_t info_offset = 0, info_size;
108 uint16_t section_count, header_size, i;
110 header_size = avio_rb16(s->pb); // 6
112 avio_skip(s->pb, 4); // Unknown constant 0x00030000
113 avio_skip(s->pb, 4); // size of file
114 section_count = avio_rb16(s->pb);
115 avio_skip(s->pb, 2); // padding
116 for (i = 0; avio_tell(s->pb) < header_size
117 && !(data_offset && info_offset)
118 && i < section_count; i++) {
119 uint32_t flag = avio_rb32(s->pb);
122 info_offset = avio_rb32(s->pb);
123 info_size = avio_rb32(s->pb);
126 avio_skip(s->pb, 4); // seek offset
127 avio_skip(s->pb, 4); // seek size
130 data_offset = avio_rb32(s->pb);
131 avio_skip(s->pb, 4); //data_size = avio_rb32(s->pb);
134 avio_skip(s->pb, 4); // REGN offset
135 avio_skip(s->pb, 4); // REGN size
140 if (!info_offset || !data_offset)
141 return AVERROR_INVALIDDATA;
143 start = data_offset + 8;
145 avio_skip(s->pb, info_offset - avio_tell(s->pb));
146 pos = avio_tell(s->pb);
147 if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
148 return AVERROR_INVALIDDATA;
151 size = avio_rb32(s->pb);
153 return AVERROR_INVALIDDATA;
154 avio_skip(s->pb, 4); // unknown
155 h1offset = avio_rb32(s->pb);
157 return AVERROR_INVALIDDATA;
158 avio_skip(s->pb, 12);
159 toffset = avio_rb32(s->pb) + 16LL;
161 return AVERROR_INVALIDDATA;
163 avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
164 codec = avio_r8(s->pb);
167 case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
168 case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
169 case 2: codec = AV_CODEC_ID_ADPCM_THP; break;
171 avpriv_request_sample(s, "codec %d", codec);
172 return AVERROR_PATCHWELCOME;
175 avio_skip(s->pb, 1); // loop flag
176 st->codec->codec_id = codec;
177 st->codec->channels = avio_r8(s->pb);
178 if (!st->codec->channels)
179 return AVERROR_INVALIDDATA;
181 avio_skip(s->pb, 1); // padding
183 avio_skip(s->pb, 2); // padding
185 st->codec->sample_rate = avio_rb16(s->pb);
186 if (!st->codec->sample_rate)
187 return AVERROR_INVALIDDATA;
190 avio_skip(s->pb, 2); // padding
191 avio_skip(s->pb, 4); // loop start sample
193 st->duration = avio_rb32(s->pb);
194 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
197 start = avio_rb32(s->pb);
198 b->current_block = 0;
199 b->block_count = avio_rb32(s->pb);
200 if (b->block_count > UINT16_MAX) {
201 av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
202 return AVERROR_INVALIDDATA;
205 b->block_size = avio_rb32(s->pb);
206 if (b->block_size > UINT16_MAX / st->codec->channels)
207 return AVERROR_INVALIDDATA;
208 b->block_size *= st->codec->channels;
210 b->samples_per_block = avio_rb32(s->pb);
211 b->last_block_used_bytes = avio_rb32(s->pb);
212 if (b->last_block_used_bytes > UINT16_MAX / st->codec->channels)
213 return AVERROR_INVALIDDATA;
214 b->last_block_used_bytes *= st->codec->channels;
216 avio_skip(s->pb, 4); // last block samples
217 avio_skip(s->pb, 4); // last block size
219 if (codec == AV_CODEC_ID_ADPCM_THP) {
222 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
224 toffset = avio_rb32(s->pb) + 16LL;
226 toffset = toffset + avio_rb32(s->pb) + st->codec->channels * 8 - 8;
228 return AVERROR_INVALIDDATA;
230 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
231 b->table = av_mallocz(32 * st->codec->channels);
233 return AVERROR(ENOMEM);
235 for (ch = 0; ch < st->codec->channels; ch++) {
236 if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
237 ret = AVERROR_INVALIDDATA;
240 avio_skip(s->pb, b->bfstm ? 14 : 24);
244 st->codec->extradata_size = 32 * st->codec->channels;
245 st->codec->extradata = av_malloc(st->codec->extradata_size);
246 if (!st->codec->extradata)
247 return AVERROR(ENOMEM);
248 memcpy(st->codec->extradata, b->table, st->codec->extradata_size);
252 if (size < (avio_tell(s->pb) - pos)) {
253 ret = AVERROR_INVALIDDATA;
258 avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
260 avio_skip(s->pb, data_offset - avio_tell(s->pb));
262 while (!avio_feof(s->pb)) {
263 chunk = avio_rl32(s->pb);
264 size = avio_rb32(s->pb);
266 ret = AVERROR_INVALIDDATA;
271 case MKTAG('A','D','P','C'):
272 if (codec != AV_CODEC_ID_ADPCM_THP)
275 asize = b->block_count * st->codec->channels * 4;
277 ret = AVERROR_INVALIDDATA;
281 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
284 b->adpc = av_mallocz(asize);
286 ret = AVERROR(ENOMEM);
289 avio_read(s->pb, b->adpc, asize);
290 avio_skip(s->pb, size - asize);
293 case MKTAG('D','A','T','A'):
294 if ((start < avio_tell(s->pb)) ||
295 (!b->adpc && codec == AV_CODEC_ID_ADPCM_THP && !b->bfstm)) {
296 ret = AVERROR_INVALIDDATA;
299 avio_skip(s->pb, start - avio_tell(s->pb));
301 if ((major != 1 || minor) && !b->bfstm)
302 avpriv_request_sample(s, "Version %d.%d", major, minor);
306 av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
308 avio_skip(s->pb, size);
318 static int read_packet(AVFormatContext *s, AVPacket *pkt)
320 AVCodecContext *codec = s->streams[0]->codec;
321 BRSTMDemuxContext *b = s->priv_data;
322 uint32_t samples, size;
325 if (avio_feof(s->pb))
328 if (b->current_block == b->block_count) {
329 size = b->last_block_used_bytes;
330 samples = size / (8 * codec->channels) * 14;
331 } else if (b->current_block < b->block_count) {
332 size = b->block_size;
333 samples = b->samples_per_block;
338 if (codec->codec_id == AV_CODEC_ID_ADPCM_THP && !codec->extradata) {
341 if (av_new_packet(pkt, 8 + (32 + 4) * codec->channels + size) < 0)
342 return AVERROR(ENOMEM);
344 bytestream_put_be32(&dst, size);
345 bytestream_put_be32(&dst, samples);
346 bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
347 bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
348 (b->current_block - 1), 4 * codec->channels);
350 ret = avio_read(s->pb, dst, size);
353 pkt->duration = samples;
355 ret = av_get_packet(s->pb, pkt, size);
358 pkt->stream_index = 0;
366 AVInputFormat ff_brstm_demuxer = {
368 .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
369 .priv_data_size = sizeof(BRSTMDemuxContext),
371 .read_header = read_header,
372 .read_packet = read_packet,
373 .read_close = read_close,
374 .extensions = "brstm",
377 AVInputFormat ff_bfstm_demuxer = {
379 .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
380 .priv_data_size = sizeof(BRSTMDemuxContext),
381 .read_probe = probe_bfstm,
382 .read_header = read_header,
383 .read_packet = read_packet,
384 .read_close = read_close,
385 .extensions = "bfstm",