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 BRSTMCoeffOffset {
32 typedef struct BRSTMDemuxContext {
35 uint32_t current_block;
36 uint32_t samples_per_block;
37 uint32_t last_block_used_bytes;
38 uint32_t last_block_size;
39 uint32_t last_block_samples;
43 BRSTMCoeffOffset offsets[256];
47 static int probe(const AVProbeData *p)
49 if (AV_RL32(p->buf) == MKTAG('R','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 probe_bfstm(const AVProbeData *p)
58 if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') ||
59 AV_RL32(p->buf) == MKTAG('C','S','T','M')) &&
60 (AV_RL16(p->buf + 4) == 0xFFFE ||
61 AV_RL16(p->buf + 4) == 0xFEFF))
62 return AVPROBE_SCORE_MAX / 3 * 2;
66 static int read_close(AVFormatContext *s)
68 BRSTMDemuxContext *b = s->priv_data;
76 static int sort_offsets(const void *a, const void *b)
78 const BRSTMCoeffOffset *s1 = a;
79 const BRSTMCoeffOffset *s2 = b;
80 return FFDIFFSIGN(s1->offset, s2->offset);
83 static av_always_inline unsigned int read16(AVFormatContext *s)
85 BRSTMDemuxContext *b = s->priv_data;
87 return avio_rl16(s->pb);
89 return avio_rb16(s->pb);
92 static av_always_inline unsigned int read32(AVFormatContext *s)
94 BRSTMDemuxContext *b = s->priv_data;
96 return avio_rl32(s->pb);
98 return avio_rb32(s->pb);
101 static int read_header(AVFormatContext *s)
103 BRSTMDemuxContext *b = s->priv_data;
104 int bom, major, minor, codec, chunk;
105 int64_t h1offset, pos, toffset;
106 uint32_t size, asize, start = 0;
108 int ret = AVERROR_EOF;
110 int bfstm = !strcmp("bfstm", s->iformat->name);
112 st = avformat_new_stream(s, NULL);
114 return AVERROR(ENOMEM);
115 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
119 bom = avio_rb16(s->pb);
120 if (bom != 0xFEFF && bom != 0xFFFE) {
121 av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
122 return AVERROR_INVALIDDATA;
126 b->little_endian = 1;
129 major = avio_r8(s->pb);
130 minor = avio_r8(s->pb);
131 avio_skip(s->pb, 4); // size of file
134 return AVERROR_INVALIDDATA;
136 avio_skip(s->pb, size - 14);
137 pos = avio_tell(s->pb);
138 if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
139 return AVERROR_INVALIDDATA;
141 uint32_t info_offset = 0;
142 uint16_t section_count, header_size, i;
144 header_size = read16(s); // 6
146 avio_skip(s->pb, 4); // Unknown constant 0x00030000
147 avio_skip(s->pb, 4); // size of file
148 section_count = read16(s);
149 avio_skip(s->pb, 2); // padding
150 for (i = 0; avio_tell(s->pb) < header_size
151 && !(start && info_offset)
152 && i < section_count; i++) {
153 uint16_t flag = read16(s);
157 info_offset = read32(s);
158 /*info_size =*/ read32(s);
161 avio_skip(s->pb, 4); // seek offset
162 avio_skip(s->pb, 4); // seek size
165 start = read32(s) + 8;
166 avio_skip(s->pb, 4); //data_size = read32(s);
169 avio_skip(s->pb, 4); // REGN offset
170 avio_skip(s->pb, 4); // REGN size
175 if (!info_offset || !start)
176 return AVERROR_INVALIDDATA;
178 avio_skip(s->pb, info_offset - avio_tell(s->pb));
179 pos = avio_tell(s->pb);
180 if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
181 return AVERROR_INVALIDDATA;
186 return AVERROR_INVALIDDATA;
187 avio_skip(s->pb, 4); // unknown
188 h1offset = read32(s);
190 return AVERROR_INVALIDDATA;
191 avio_skip(s->pb, 12);
192 toffset = read32(s) + 16LL;
194 return AVERROR_INVALIDDATA;
196 avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
197 codec = avio_r8(s->pb);
200 case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
201 case 1: codec = b->little_endian ?
202 AV_CODEC_ID_PCM_S16LE_PLANAR :
203 AV_CODEC_ID_PCM_S16BE_PLANAR; break;
204 case 2: codec = b->little_endian ?
205 AV_CODEC_ID_ADPCM_THP_LE :
206 AV_CODEC_ID_ADPCM_THP; break;
208 avpriv_request_sample(s, "codec %d", codec);
209 return AVERROR_PATCHWELCOME;
212 loop = avio_r8(s->pb); // loop flag
213 st->codecpar->codec_id = codec;
214 st->codecpar->channels = avio_r8(s->pb);
215 if (!st->codecpar->channels)
216 return AVERROR_INVALIDDATA;
218 avio_skip(s->pb, 1); // padding
220 st->codecpar->sample_rate = bfstm ? read32(s) : read16(s);
221 if (st->codecpar->sample_rate <= 0)
222 return AVERROR_INVALIDDATA;
225 avio_skip(s->pb, 2); // padding
228 if (av_dict_set_int(&s->metadata, "loop_start",
229 av_rescale(read32(s), AV_TIME_BASE,
230 st->codecpar->sample_rate),
232 return AVERROR(ENOMEM);
238 st->duration = read32(s);
239 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
243 b->current_block = 0;
244 b->block_count = read32(s);
245 if (b->block_count > UINT16_MAX) {
246 av_log(s, AV_LOG_WARNING, "too many blocks: %"PRIu32"\n", b->block_count);
247 return AVERROR_INVALIDDATA;
250 b->block_size = read32(s);
251 if (b->block_size > UINT32_MAX / st->codecpar->channels)
252 return AVERROR_INVALIDDATA;
254 b->samples_per_block = read32(s);
255 b->last_block_used_bytes = read32(s);
256 b->last_block_samples = read32(s);
257 b->last_block_size = read32(s);
258 if (b->last_block_size > UINT32_MAX / st->codecpar->channels)
259 return AVERROR_INVALIDDATA;
260 if (b->last_block_used_bytes > b->last_block_size)
261 return AVERROR_INVALIDDATA;
264 if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
267 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
269 toffset = read32(s) + 16LL;
271 toffset = toffset + read32(s) + st->codecpar->channels * 8 - 8;
273 return AVERROR_INVALIDDATA;
276 avio_skip(s->pb, pos + toffset - avio_tell(s->pb) - 8LL * (st->codecpar->channels + 1));
277 for (ch = 0; ch < st->codecpar->channels; ch++) {
279 b->offsets[ch].channel = ch;
280 b->offsets[ch].offset = read32(s);
283 qsort(b->offsets, st->codecpar->channels, sizeof(*b->offsets), sort_offsets);
286 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
287 b->table = av_mallocz(32 * st->codecpar->channels);
289 return AVERROR(ENOMEM);
291 for (ch = 0; ch < st->codecpar->channels; ch++) {
293 avio_skip(s->pb, pos + 16LL + b->offsets[ch].offset - avio_tell(s->pb));
295 if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
296 ret = AVERROR_INVALIDDATA;
301 avio_skip(s->pb, 14);
305 if (size < (avio_tell(s->pb) - pos)) {
306 ret = AVERROR_INVALIDDATA;
310 avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
312 while (!avio_feof(s->pb)) {
313 chunk = avio_rl32(s->pb);
316 ret = AVERROR_INVALIDDATA;
321 case MKTAG('S','E','E','K'):
322 case MKTAG('A','D','P','C'):
323 if (codec != AV_CODEC_ID_ADPCM_THP &&
324 codec != AV_CODEC_ID_ADPCM_THP_LE)
327 asize = b->block_count * st->codecpar->channels * 4;
329 ret = AVERROR_INVALIDDATA;
333 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
336 b->adpc = av_mallocz(asize);
338 ret = AVERROR(ENOMEM);
341 if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
342 // Big-endian BFSTMs have little-endian SEEK tables
343 // for some strange reason.
345 for (i = 0; i < asize; i += 2) {
346 b->adpc[i+1] = avio_r8(s->pb);
347 b->adpc[i] = avio_r8(s->pb);
350 avio_read(s->pb, b->adpc, asize);
352 avio_skip(s->pb, size - asize);
355 case MKTAG('D','A','T','A'):
356 if ((start < avio_tell(s->pb)) ||
357 (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
358 codec == AV_CODEC_ID_ADPCM_THP_LE))) {
359 ret = AVERROR_INVALIDDATA;
362 avio_skip(s->pb, start - avio_tell(s->pb));
364 if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
365 codec == AV_CODEC_ID_ADPCM_THP_LE))
366 avio_skip(s->pb, 24);
368 b->data_start = avio_tell(s->pb);
370 if (!bfstm && (major != 1 || minor))
371 avpriv_request_sample(s, "Version %d.%d", major, minor);
375 av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
377 avio_skip(s->pb, size);
387 static int read_packet(AVFormatContext *s, AVPacket *pkt)
389 AVCodecParameters *par = s->streams[0]->codecpar;
390 BRSTMDemuxContext *b = s->priv_data;
391 uint32_t samples, size, skip = 0;
394 if (avio_feof(s->pb))
397 if (b->current_block == b->block_count) {
398 size = b->last_block_used_bytes;
399 samples = b->last_block_samples;
400 skip = b->last_block_size - b->last_block_used_bytes;
402 if (samples < size * 14 / 8) {
403 uint32_t adjusted_size = samples / 14 * 8;
405 adjusted_size += (samples % 14 + 1) / 2 + 1;
407 skip += size - adjusted_size;
408 size = adjusted_size;
410 } else if (b->current_block < b->block_count) {
411 size = b->block_size;
412 samples = b->samples_per_block;
417 if (par->codec_id == AV_CODEC_ID_ADPCM_THP ||
418 par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
422 av_log(s, AV_LOG_ERROR, "adpcm_thp requires ADPC chunk, but none was found.\n");
423 return AVERROR_INVALIDDATA;
426 b->table = av_mallocz(32 * par->channels);
428 return AVERROR(ENOMEM);
431 if (size > (INT_MAX - 32 - 4) ||
432 (32 + 4 + size) > (INT_MAX / par->channels) ||
433 (32 + 4 + size) * par->channels > INT_MAX - 8)
434 return AVERROR_INVALIDDATA;
435 if ((ret = av_new_packet(pkt, 8 + (32 + 4 + size) * par->channels)) < 0)
438 if (par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
439 bytestream_put_le32(&dst, size * par->channels);
440 bytestream_put_le32(&dst, samples);
442 bytestream_put_be32(&dst, size * par->channels);
443 bytestream_put_be32(&dst, samples);
445 bytestream_put_buffer(&dst, b->table, 32 * par->channels);
446 bytestream_put_buffer(&dst, b->adpc + 4 * par->channels *
447 (b->current_block - 1), 4 * par->channels);
449 for (i = 0; i < par->channels; i++) {
450 ret = avio_read(s->pb, dst, size);
452 avio_skip(s->pb, skip);
457 pkt->duration = samples;
459 size *= par->channels;
460 ret = av_get_packet(s->pb, pkt, size);
463 pkt->stream_index = 0;
471 static int read_seek(AVFormatContext *s, int stream_index,
472 int64_t timestamp, int flags)
474 AVStream *st = s->streams[stream_index];
475 BRSTMDemuxContext *b = s->priv_data;
480 timestamp /= b->samples_per_block;
481 if (timestamp >= b->block_count)
482 timestamp = b->block_count - 1;
483 ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
484 st->codecpar->channels, SEEK_SET);
488 b->current_block = timestamp;
489 ff_update_cur_dts(s, st, timestamp * b->samples_per_block);
493 AVInputFormat ff_brstm_demuxer = {
495 .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
496 .priv_data_size = sizeof(BRSTMDemuxContext),
498 .read_header = read_header,
499 .read_packet = read_packet,
500 .read_close = read_close,
501 .read_seek = read_seek,
502 .extensions = "brstm",
505 AVInputFormat ff_bfstm_demuxer = {
507 .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
508 .priv_data_size = sizeof(BRSTMDemuxContext),
509 .read_probe = probe_bfstm,
510 .read_header = read_header,
511 .read_packet = read_packet,
512 .read_close = read_close,
513 .read_seek = read_seek,
514 .extensions = "bfstm,bcstm",