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 = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
188 case 2: codec = b->little_endian ?
189 AV_CODEC_ID_ADPCM_THP_LE :
190 AV_CODEC_ID_ADPCM_THP; break;
192 avpriv_request_sample(s, "codec %d", codec);
193 return AVERROR_PATCHWELCOME;
196 avio_skip(s->pb, 1); // loop flag
197 st->codec->codec_id = codec;
198 st->codec->channels = avio_r8(s->pb);
199 if (!st->codec->channels)
200 return AVERROR_INVALIDDATA;
202 avio_skip(s->pb, 1); // padding
204 st->codec->sample_rate = bfstm ? read32(s) : read16(s);
205 if (!st->codec->sample_rate)
206 return AVERROR_INVALIDDATA;
209 avio_skip(s->pb, 2); // padding
210 avio_skip(s->pb, 4); // loop start sample
212 st->duration = read32(s);
213 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
217 b->current_block = 0;
218 b->block_count = read32(s);
219 if (b->block_count > UINT16_MAX) {
220 av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
221 return AVERROR_INVALIDDATA;
224 b->block_size = read32(s);
225 if (b->block_size > UINT32_MAX / st->codec->channels)
226 return AVERROR_INVALIDDATA;
228 b->samples_per_block = read32(s);
229 b->last_block_used_bytes = read32(s);
230 b->last_block_samples = read32(s);
231 b->last_block_size = read32(s);
232 if (b->last_block_size > UINT32_MAX / st->codec->channels)
233 return AVERROR_INVALIDDATA;
234 if (b->last_block_used_bytes > b->last_block_size)
235 return AVERROR_INVALIDDATA;
238 if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
241 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
243 toffset = read32(s) + 16LL;
245 toffset = toffset + read32(s) + st->codec->channels * 8 - 8;
247 return AVERROR_INVALIDDATA;
249 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
250 b->table = av_mallocz(32 * st->codec->channels);
252 return AVERROR(ENOMEM);
254 for (ch = 0; ch < st->codec->channels; ch++) {
255 if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
256 ret = AVERROR_INVALIDDATA;
259 avio_skip(s->pb, bfstm ? 14 : 24);
263 if (size < (avio_tell(s->pb) - pos)) {
264 ret = AVERROR_INVALIDDATA;
268 avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
270 while (!avio_feof(s->pb)) {
271 chunk = avio_rl32(s->pb);
274 ret = AVERROR_INVALIDDATA;
279 case MKTAG('S','E','E','K'):
280 case MKTAG('A','D','P','C'):
281 if (codec != AV_CODEC_ID_ADPCM_THP &&
282 codec != AV_CODEC_ID_ADPCM_THP_LE)
285 asize = b->block_count * st->codec->channels * 4;
287 ret = AVERROR_INVALIDDATA;
291 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
294 b->adpc = av_mallocz(asize);
296 ret = AVERROR(ENOMEM);
299 if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
300 // Big-endian BFSTMs have little-endian SEEK tables
301 // for some strange reason.
303 for (i = 0; i < asize; i += 2) {
304 b->adpc[i+1] = avio_r8(s->pb);
305 b->adpc[i] = avio_r8(s->pb);
308 avio_read(s->pb, b->adpc, asize);
310 avio_skip(s->pb, size - asize);
313 case MKTAG('D','A','T','A'):
314 if ((start < avio_tell(s->pb)) ||
315 (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
316 codec == AV_CODEC_ID_ADPCM_THP_LE))) {
317 ret = AVERROR_INVALIDDATA;
320 avio_skip(s->pb, start - avio_tell(s->pb));
322 if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
323 codec == AV_CODEC_ID_ADPCM_THP_LE))
324 avio_skip(s->pb, 24);
326 b->data_start = avio_tell(s->pb);
328 if ((major != 1 || minor) && !bfstm)
329 avpriv_request_sample(s, "Version %d.%d", major, minor);
333 av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
335 avio_skip(s->pb, size);
345 static int read_packet(AVFormatContext *s, AVPacket *pkt)
347 AVCodecContext *codec = s->streams[0]->codec;
348 BRSTMDemuxContext *b = s->priv_data;
349 uint32_t samples, size, skip = 0;
352 if (avio_feof(s->pb))
355 if (b->current_block == b->block_count) {
356 size = b->last_block_used_bytes;
357 samples = b->last_block_samples;
358 skip = b->last_block_size - b->last_block_used_bytes;
359 } else if (b->current_block < b->block_count) {
360 size = b->block_size;
361 samples = b->samples_per_block;
366 if (codec->codec_id == AV_CODEC_ID_ADPCM_THP ||
367 codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
370 if (av_new_packet(pkt, 8 + (32 + 4 + size) * codec->channels) < 0)
371 return AVERROR(ENOMEM);
373 if (codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
374 bytestream_put_le32(&dst, size * codec->channels);
375 bytestream_put_le32(&dst, samples);
377 bytestream_put_be32(&dst, size * codec->channels);
378 bytestream_put_be32(&dst, samples);
380 bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
381 bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
382 (b->current_block - 1), 4 * codec->channels);
384 for (i = 0; i < codec->channels; i++) {
385 ret = avio_read(s->pb, dst, size);
387 avio_skip(s->pb, skip);
393 pkt->duration = samples;
395 size *= codec->channels;
396 ret = av_get_packet(s->pb, pkt, size);
399 pkt->stream_index = 0;
407 static int read_seek(AVFormatContext *s, int stream_index,
408 int64_t timestamp, int flags)
410 AVStream *st = s->streams[stream_index];
411 BRSTMDemuxContext *b = s->priv_data;
414 timestamp /= b->samples_per_block;
415 ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
416 st->codec->channels, SEEK_SET);
420 b->current_block = timestamp;
421 ff_update_cur_dts(s, st, timestamp * b->samples_per_block);
425 AVInputFormat ff_brstm_demuxer = {
427 .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
428 .priv_data_size = sizeof(BRSTMDemuxContext),
430 .read_header = read_header,
431 .read_packet = read_packet,
432 .read_close = read_close,
433 .read_seek = read_seek,
434 .extensions = "brstm",
437 AVInputFormat ff_bfstm_demuxer = {
439 .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
440 .priv_data_size = sizeof(BRSTMDemuxContext),
441 .read_probe = probe_bfstm,
442 .read_header = read_header,
443 .read_packet = read_packet,
444 .read_close = read_close,
445 .read_seek = read_seek,
446 .extensions = "bfstm,bcstm",