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;
33 uint32_t last_block_size;
34 uint32_t last_block_samples;
41 static int probe(AVProbeData *p)
43 if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
44 (AV_RL16(p->buf + 4) == 0xFFFE ||
45 AV_RL16(p->buf + 4) == 0xFEFF))
46 return AVPROBE_SCORE_MAX / 3 * 2;
50 static int probe_bfstm(AVProbeData *p)
52 if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') ||
53 AV_RL32(p->buf) == MKTAG('C','S','T','M')) &&
54 (AV_RL16(p->buf + 4) == 0xFFFE ||
55 AV_RL16(p->buf + 4) == 0xFEFF))
56 return AVPROBE_SCORE_MAX / 3 * 2;
60 static int read_close(AVFormatContext *s)
62 BRSTMDemuxContext *b = s->priv_data;
70 static av_always_inline unsigned int read16(AVFormatContext *s)
72 BRSTMDemuxContext *b = s->priv_data;
74 return avio_rl16(s->pb);
76 return avio_rb16(s->pb);
79 static av_always_inline unsigned int read32(AVFormatContext *s)
81 BRSTMDemuxContext *b = s->priv_data;
83 return avio_rl32(s->pb);
85 return avio_rb32(s->pb);
88 static int read_header(AVFormatContext *s)
90 BRSTMDemuxContext *b = s->priv_data;
91 int bom, major, minor, codec, chunk;
92 int64_t h1offset, pos, toffset;
93 uint32_t size, asize, start = 0;
95 int ret = AVERROR_EOF;
96 int bfstm = !strcmp("bfstm", s->iformat->name);
98 st = avformat_new_stream(s, NULL);
100 return AVERROR(ENOMEM);
101 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
105 bom = avio_rb16(s->pb);
106 if (bom != 0xFEFF && bom != 0xFFFE) {
107 av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
108 return AVERROR_INVALIDDATA;
112 b->little_endian = 1;
115 major = avio_r8(s->pb);
116 minor = avio_r8(s->pb);
117 avio_skip(s->pb, 4); // size of file
120 return AVERROR_INVALIDDATA;
122 avio_skip(s->pb, size - 14);
123 pos = avio_tell(s->pb);
124 if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
125 return AVERROR_INVALIDDATA;
127 uint32_t info_offset = 0, info_size;
128 uint16_t section_count, header_size, i;
130 header_size = read16(s); // 6
132 avio_skip(s->pb, 4); // Unknown constant 0x00030000
133 avio_skip(s->pb, 4); // size of file
134 section_count = read16(s);
135 avio_skip(s->pb, 2); // padding
136 for (i = 0; avio_tell(s->pb) < header_size
137 && !(start && info_offset)
138 && i < section_count; i++) {
139 uint16_t flag = read16(s);
143 info_offset = read32(s);
144 info_size = read32(s);
147 avio_skip(s->pb, 4); // seek offset
148 avio_skip(s->pb, 4); // seek size
151 start = read32(s) + 8;
152 avio_skip(s->pb, 4); //data_size = read32(s);
155 avio_skip(s->pb, 4); // REGN offset
156 avio_skip(s->pb, 4); // REGN size
161 if (!info_offset || !start)
162 return AVERROR_INVALIDDATA;
164 avio_skip(s->pb, info_offset - avio_tell(s->pb));
165 pos = avio_tell(s->pb);
166 if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
167 return AVERROR_INVALIDDATA;
172 return AVERROR_INVALIDDATA;
173 avio_skip(s->pb, 4); // unknown
174 h1offset = read32(s);
176 return AVERROR_INVALIDDATA;
177 avio_skip(s->pb, 12);
178 toffset = read32(s) + 16LL;
180 return AVERROR_INVALIDDATA;
182 avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
183 codec = avio_r8(s->pb);
186 case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
187 case 1: codec = b->little_endian ?
188 AV_CODEC_ID_PCM_S16LE_PLANAR :
189 AV_CODEC_ID_PCM_S16BE_PLANAR; break;
190 case 2: codec = b->little_endian ?
191 AV_CODEC_ID_ADPCM_THP_LE :
192 AV_CODEC_ID_ADPCM_THP; break;
194 avpriv_request_sample(s, "codec %d", codec);
195 return AVERROR_PATCHWELCOME;
198 avio_skip(s->pb, 1); // loop flag
199 st->codec->codec_id = codec;
200 st->codec->channels = avio_r8(s->pb);
201 if (!st->codec->channels)
202 return AVERROR_INVALIDDATA;
204 avio_skip(s->pb, 1); // padding
206 st->codec->sample_rate = bfstm ? read32(s) : read16(s);
207 if (!st->codec->sample_rate)
208 return AVERROR_INVALIDDATA;
211 avio_skip(s->pb, 2); // padding
212 avio_skip(s->pb, 4); // loop start sample
214 st->duration = read32(s);
215 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
219 b->current_block = 0;
220 b->block_count = read32(s);
221 if (b->block_count > UINT16_MAX) {
222 av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
223 return AVERROR_INVALIDDATA;
226 b->block_size = read32(s);
227 if (b->block_size > UINT32_MAX / st->codec->channels)
228 return AVERROR_INVALIDDATA;
230 b->samples_per_block = read32(s);
231 b->last_block_used_bytes = read32(s);
232 b->last_block_samples = read32(s);
233 b->last_block_size = read32(s);
234 if (b->last_block_size > UINT32_MAX / st->codec->channels)
235 return AVERROR_INVALIDDATA;
236 if (b->last_block_used_bytes > b->last_block_size)
237 return AVERROR_INVALIDDATA;
240 if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
243 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
245 toffset = read32(s) + 16LL;
247 toffset = toffset + read32(s) + st->codec->channels * 8 - 8;
249 return AVERROR_INVALIDDATA;
251 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
252 b->table = av_mallocz(32 * st->codec->channels);
254 return AVERROR(ENOMEM);
256 for (ch = 0; ch < st->codec->channels; ch++) {
257 if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
258 ret = AVERROR_INVALIDDATA;
261 avio_skip(s->pb, bfstm ? 14 : 24);
265 if (size < (avio_tell(s->pb) - pos)) {
266 ret = AVERROR_INVALIDDATA;
270 avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
272 while (!avio_feof(s->pb)) {
273 chunk = avio_rl32(s->pb);
276 ret = AVERROR_INVALIDDATA;
281 case MKTAG('S','E','E','K'):
282 case MKTAG('A','D','P','C'):
283 if (codec != AV_CODEC_ID_ADPCM_THP &&
284 codec != AV_CODEC_ID_ADPCM_THP_LE)
287 asize = b->block_count * st->codec->channels * 4;
289 ret = AVERROR_INVALIDDATA;
293 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
296 b->adpc = av_mallocz(asize);
298 ret = AVERROR(ENOMEM);
301 if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
302 // Big-endian BFSTMs have little-endian SEEK tables
303 // for some strange reason.
305 for (i = 0; i < asize; i += 2) {
306 b->adpc[i+1] = avio_r8(s->pb);
307 b->adpc[i] = avio_r8(s->pb);
310 avio_read(s->pb, b->adpc, asize);
312 avio_skip(s->pb, size - asize);
315 case MKTAG('D','A','T','A'):
316 if ((start < avio_tell(s->pb)) ||
317 (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
318 codec == AV_CODEC_ID_ADPCM_THP_LE))) {
319 ret = AVERROR_INVALIDDATA;
322 avio_skip(s->pb, start - avio_tell(s->pb));
324 if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
325 codec == AV_CODEC_ID_ADPCM_THP_LE))
326 avio_skip(s->pb, 24);
328 b->data_start = avio_tell(s->pb);
330 if ((major != 1 || minor) && !bfstm)
331 avpriv_request_sample(s, "Version %d.%d", major, minor);
335 av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
337 avio_skip(s->pb, size);
347 static int read_packet(AVFormatContext *s, AVPacket *pkt)
349 AVCodecContext *codec = s->streams[0]->codec;
350 BRSTMDemuxContext *b = s->priv_data;
351 uint32_t samples, size, skip = 0;
354 if (avio_feof(s->pb))
357 if (b->current_block == b->block_count) {
358 size = b->last_block_used_bytes;
359 samples = b->last_block_samples;
360 skip = b->last_block_size - b->last_block_used_bytes;
361 } else if (b->current_block < b->block_count) {
362 size = b->block_size;
363 samples = b->samples_per_block;
368 if (codec->codec_id == AV_CODEC_ID_ADPCM_THP ||
369 codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
372 if (av_new_packet(pkt, 8 + (32 + 4 + size) * codec->channels) < 0)
373 return AVERROR(ENOMEM);
375 if (codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
376 bytestream_put_le32(&dst, size * codec->channels);
377 bytestream_put_le32(&dst, samples);
379 bytestream_put_be32(&dst, size * codec->channels);
380 bytestream_put_be32(&dst, samples);
382 bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
383 bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
384 (b->current_block - 1), 4 * codec->channels);
386 for (i = 0; i < codec->channels; i++) {
387 ret = avio_read(s->pb, dst, size);
389 avio_skip(s->pb, skip);
395 pkt->duration = samples;
397 size *= codec->channels;
398 ret = av_get_packet(s->pb, pkt, size);
401 pkt->stream_index = 0;
409 static int read_seek(AVFormatContext *s, int stream_index,
410 int64_t timestamp, int flags)
412 AVStream *st = s->streams[stream_index];
413 BRSTMDemuxContext *b = s->priv_data;
416 timestamp /= b->samples_per_block;
417 ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
418 st->codec->channels, SEEK_SET);
422 b->current_block = timestamp;
423 ff_update_cur_dts(s, st, timestamp * b->samples_per_block);
427 AVInputFormat ff_brstm_demuxer = {
429 .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
430 .priv_data_size = sizeof(BRSTMDemuxContext),
432 .read_header = read_header,
433 .read_packet = read_packet,
434 .read_close = read_close,
435 .read_seek = read_seek,
436 .extensions = "brstm",
439 AVInputFormat ff_bfstm_demuxer = {
441 .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
442 .priv_data_size = sizeof(BRSTMDemuxContext),
443 .read_probe = probe_bfstm,
444 .read_header = read_header,
445 .read_packet = read_packet,
446 .read_close = read_close,
447 .read_seek = read_seek,
448 .extensions = "bfstm,bcstm",