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;
41 uint8_t table[256 * 32];
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;
75 static int sort_offsets(const void *a, const void *b)
77 const BRSTMCoeffOffset *s1 = a;
78 const BRSTMCoeffOffset *s2 = b;
79 return FFDIFFSIGN(s1->offset, s2->offset);
82 static av_always_inline unsigned int read16(AVFormatContext *s)
84 BRSTMDemuxContext *b = s->priv_data;
86 return avio_rl16(s->pb);
88 return avio_rb16(s->pb);
91 static av_always_inline unsigned int read32(AVFormatContext *s)
93 BRSTMDemuxContext *b = s->priv_data;
95 return avio_rl32(s->pb);
97 return avio_rb32(s->pb);
100 static int read_header(AVFormatContext *s)
102 BRSTMDemuxContext *b = s->priv_data;
103 int bom, major, minor, codec, chunk;
104 int64_t h1offset, pos, toffset;
105 uint32_t size, asize, start = 0;
107 int ret = AVERROR_EOF;
109 int bfstm = !strcmp("bfstm", s->iformat->name);
111 st = avformat_new_stream(s, NULL);
113 return AVERROR(ENOMEM);
114 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
118 bom = avio_rb16(s->pb);
119 if (bom != 0xFEFF && bom != 0xFFFE) {
120 av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
121 return AVERROR_INVALIDDATA;
125 b->little_endian = 1;
128 major = avio_r8(s->pb);
129 minor = avio_r8(s->pb);
130 avio_skip(s->pb, 4); // size of file
133 return AVERROR_INVALIDDATA;
135 avio_skip(s->pb, size - 14);
136 pos = avio_tell(s->pb);
137 if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
138 return AVERROR_INVALIDDATA;
140 uint32_t info_offset = 0;
141 uint16_t section_count, header_size, i;
143 header_size = read16(s); // 6
145 avio_skip(s->pb, 4); // Unknown constant 0x00030000
146 avio_skip(s->pb, 4); // size of file
147 section_count = read16(s);
148 avio_skip(s->pb, 2); // padding
149 for (i = 0; avio_tell(s->pb) < header_size
150 && !(start && info_offset)
151 && i < section_count; i++) {
152 uint16_t flag = read16(s);
156 info_offset = read32(s);
157 /*info_size =*/ read32(s);
160 avio_skip(s->pb, 4); // seek offset
161 avio_skip(s->pb, 4); // seek size
164 start = read32(s) + 8;
165 avio_skip(s->pb, 4); //data_size = read32(s);
168 avio_skip(s->pb, 4); // REGN offset
169 avio_skip(s->pb, 4); // REGN size
174 if (!info_offset || !start)
175 return AVERROR_INVALIDDATA;
177 avio_skip(s->pb, info_offset - avio_tell(s->pb));
178 pos = avio_tell(s->pb);
179 if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
180 return AVERROR_INVALIDDATA;
185 return AVERROR_INVALIDDATA;
186 avio_skip(s->pb, 4); // unknown
187 h1offset = read32(s);
189 return AVERROR_INVALIDDATA;
190 avio_skip(s->pb, 12);
191 toffset = read32(s) + 16LL;
193 return AVERROR_INVALIDDATA;
195 avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
196 codec = avio_r8(s->pb);
199 case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
200 case 1: codec = b->little_endian ?
201 AV_CODEC_ID_PCM_S16LE_PLANAR :
202 AV_CODEC_ID_PCM_S16BE_PLANAR; break;
203 case 2: codec = b->little_endian ?
204 AV_CODEC_ID_ADPCM_THP_LE :
205 AV_CODEC_ID_ADPCM_THP; break;
207 avpriv_request_sample(s, "codec %d", codec);
208 return AVERROR_PATCHWELCOME;
211 loop = avio_r8(s->pb); // loop flag
212 st->codecpar->codec_id = codec;
213 st->codecpar->channels = avio_r8(s->pb);
214 if (!st->codecpar->channels)
215 return AVERROR_INVALIDDATA;
217 avio_skip(s->pb, 1); // padding
219 st->codecpar->sample_rate = bfstm ? read32(s) : read16(s);
220 if (st->codecpar->sample_rate <= 0)
221 return AVERROR_INVALIDDATA;
224 avio_skip(s->pb, 2); // padding
227 if (av_dict_set_int(&s->metadata, "loop_start",
228 av_rescale(read32(s), AV_TIME_BASE,
229 st->codecpar->sample_rate),
231 return AVERROR(ENOMEM);
237 st->duration = read32(s);
238 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
242 b->current_block = 0;
243 b->block_count = read32(s);
244 if (b->block_count > UINT16_MAX) {
245 av_log(s, AV_LOG_WARNING, "too many blocks: %"PRIu32"\n", b->block_count);
246 return AVERROR_INVALIDDATA;
249 b->block_size = read32(s);
250 if (b->block_size > UINT32_MAX / st->codecpar->channels)
251 return AVERROR_INVALIDDATA;
253 b->samples_per_block = read32(s);
254 b->last_block_used_bytes = read32(s);
255 b->last_block_samples = read32(s);
256 b->last_block_size = read32(s);
257 if (b->last_block_size > UINT32_MAX / st->codecpar->channels)
258 return AVERROR_INVALIDDATA;
259 if (b->last_block_used_bytes > b->last_block_size)
260 return AVERROR_INVALIDDATA;
263 if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
266 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
268 toffset = read32(s) + 16LL;
270 toffset = toffset + read32(s) + st->codecpar->channels * 8 - 8;
272 return AVERROR_INVALIDDATA;
275 avio_skip(s->pb, pos + toffset - avio_tell(s->pb) - 8LL * (st->codecpar->channels + 1));
276 for (ch = 0; ch < st->codecpar->channels; ch++) {
278 b->offsets[ch].channel = ch;
279 b->offsets[ch].offset = read32(s);
282 qsort(b->offsets, st->codecpar->channels, sizeof(*b->offsets), sort_offsets);
285 avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
287 for (ch = 0; ch < st->codecpar->channels; ch++) {
289 avio_skip(s->pb, pos + 16LL + b->offsets[ch].offset - avio_tell(s->pb));
291 if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
292 ret = AVERROR_INVALIDDATA;
297 avio_skip(s->pb, 14);
301 if (size < (avio_tell(s->pb) - pos)) {
302 ret = AVERROR_INVALIDDATA;
306 avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
308 while (!avio_feof(s->pb)) {
309 chunk = avio_rl32(s->pb);
312 ret = AVERROR_INVALIDDATA;
317 case MKTAG('S','E','E','K'):
318 case MKTAG('A','D','P','C'):
319 if (codec != AV_CODEC_ID_ADPCM_THP &&
320 codec != AV_CODEC_ID_ADPCM_THP_LE)
323 asize = b->block_count * st->codecpar->channels * 4;
325 ret = AVERROR_INVALIDDATA;
329 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
332 b->adpc = av_mallocz(asize);
334 ret = AVERROR(ENOMEM);
337 if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
338 // Big-endian BFSTMs have little-endian SEEK tables
339 // for some strange reason.
341 for (i = 0; i < asize; i += 2) {
342 b->adpc[i+1] = avio_r8(s->pb);
343 b->adpc[i] = avio_r8(s->pb);
346 avio_read(s->pb, b->adpc, asize);
348 avio_skip(s->pb, size - asize);
351 case MKTAG('D','A','T','A'):
352 if ((start < avio_tell(s->pb)) ||
353 (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
354 codec == AV_CODEC_ID_ADPCM_THP_LE))) {
355 ret = AVERROR_INVALIDDATA;
358 avio_skip(s->pb, start - avio_tell(s->pb));
360 if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
361 codec == AV_CODEC_ID_ADPCM_THP_LE))
362 avio_skip(s->pb, 24);
364 b->data_start = avio_tell(s->pb);
366 if (!bfstm && (major != 1 || minor))
367 avpriv_request_sample(s, "Version %d.%d", major, minor);
371 av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
373 avio_skip(s->pb, size);
383 static int read_packet(AVFormatContext *s, AVPacket *pkt)
385 AVCodecParameters *par = s->streams[0]->codecpar;
386 BRSTMDemuxContext *b = s->priv_data;
387 uint32_t samples, size, skip = 0;
390 if (avio_feof(s->pb))
393 if (b->current_block == b->block_count) {
394 size = b->last_block_used_bytes;
395 samples = b->last_block_samples;
396 skip = b->last_block_size - b->last_block_used_bytes;
398 if (samples < size * 14 / 8) {
399 uint32_t adjusted_size = samples / 14 * 8;
401 adjusted_size += (samples % 14 + 1) / 2 + 1;
403 skip += size - adjusted_size;
404 size = adjusted_size;
406 } else if (b->current_block < b->block_count) {
407 size = b->block_size;
408 samples = b->samples_per_block;
413 if (par->codec_id == AV_CODEC_ID_ADPCM_THP ||
414 par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
418 av_log(s, AV_LOG_ERROR, "adpcm_thp requires ADPC chunk, but none was found.\n");
419 return AVERROR_INVALIDDATA;
422 if (size > (INT_MAX - 32 - 4) ||
423 (32 + 4 + size) > (INT_MAX / par->channels) ||
424 (32 + 4 + size) * par->channels > INT_MAX - 8)
425 return AVERROR_INVALIDDATA;
426 if ((ret = av_new_packet(pkt, 8 + (32 + 4 + size) * par->channels)) < 0)
429 if (par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
430 bytestream_put_le32(&dst, size * par->channels);
431 bytestream_put_le32(&dst, samples);
433 bytestream_put_be32(&dst, size * par->channels);
434 bytestream_put_be32(&dst, samples);
436 bytestream_put_buffer(&dst, b->table, 32 * par->channels);
437 bytestream_put_buffer(&dst, b->adpc + 4 * par->channels *
438 (b->current_block - 1), 4 * par->channels);
440 for (i = 0; i < par->channels; i++) {
441 ret = avio_read(s->pb, dst, size);
443 avio_skip(s->pb, skip);
448 pkt->duration = samples;
450 size *= par->channels;
451 ret = av_get_packet(s->pb, pkt, size);
454 pkt->stream_index = 0;
462 static int read_seek(AVFormatContext *s, int stream_index,
463 int64_t timestamp, int flags)
465 AVStream *st = s->streams[stream_index];
466 BRSTMDemuxContext *b = s->priv_data;
471 timestamp /= b->samples_per_block;
472 if (timestamp >= b->block_count)
473 timestamp = b->block_count - 1;
474 ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
475 st->codecpar->channels, SEEK_SET);
479 b->current_block = timestamp;
480 ff_update_cur_dts(s, st, timestamp * b->samples_per_block);
484 AVInputFormat ff_brstm_demuxer = {
486 .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
487 .priv_data_size = sizeof(BRSTMDemuxContext),
489 .read_header = read_header,
490 .read_packet = read_packet,
491 .read_close = read_close,
492 .read_seek = read_seek,
493 .extensions = "brstm",
496 AVInputFormat ff_bfstm_demuxer = {
498 .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
499 .priv_data_size = sizeof(BRSTMDemuxContext),
500 .read_probe = probe_bfstm,
501 .read_header = read_header,
502 .read_packet = read_packet,
503 .read_close = read_close,
504 .read_seek = read_seek,
505 .extensions = "bfstm,bcstm",