You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

474 lines
14KB

  1. /*
  2. * BRSTM demuxer
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavcodec/bytestream.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. typedef struct BRSTMDemuxContext {
  26. uint32_t block_size;
  27. uint32_t block_count;
  28. uint32_t current_block;
  29. uint32_t samples_per_block;
  30. uint32_t last_block_used_bytes;
  31. uint32_t last_block_size;
  32. uint32_t last_block_samples;
  33. uint32_t data_start;
  34. uint8_t *table;
  35. uint8_t *adpc;
  36. int little_endian;
  37. } BRSTMDemuxContext;
  38. static int probe(AVProbeData *p)
  39. {
  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;
  44. return 0;
  45. }
  46. static int probe_bfstm(AVProbeData *p)
  47. {
  48. if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') ||
  49. AV_RL32(p->buf) == MKTAG('C','S','T','M')) &&
  50. (AV_RL16(p->buf + 4) == 0xFFFE ||
  51. AV_RL16(p->buf + 4) == 0xFEFF))
  52. return AVPROBE_SCORE_MAX / 3 * 2;
  53. return 0;
  54. }
  55. static int read_close(AVFormatContext *s)
  56. {
  57. BRSTMDemuxContext *b = s->priv_data;
  58. av_freep(&b->table);
  59. av_freep(&b->adpc);
  60. return 0;
  61. }
  62. static av_always_inline unsigned int read16(AVFormatContext *s)
  63. {
  64. BRSTMDemuxContext *b = s->priv_data;
  65. if (b->little_endian)
  66. return avio_rl16(s->pb);
  67. else
  68. return avio_rb16(s->pb);
  69. }
  70. static av_always_inline unsigned int read32(AVFormatContext *s)
  71. {
  72. BRSTMDemuxContext *b = s->priv_data;
  73. if (b->little_endian)
  74. return avio_rl32(s->pb);
  75. else
  76. return avio_rb32(s->pb);
  77. }
  78. static int read_header(AVFormatContext *s)
  79. {
  80. BRSTMDemuxContext *b = s->priv_data;
  81. int bom, major, minor, codec, chunk;
  82. int64_t h1offset, pos, toffset;
  83. uint32_t size, asize, start = 0;
  84. AVStream *st;
  85. int ret = AVERROR_EOF;
  86. int loop = 0;
  87. int bfstm = !strcmp("bfstm", s->iformat->name);
  88. st = avformat_new_stream(s, NULL);
  89. if (!st)
  90. return AVERROR(ENOMEM);
  91. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  92. avio_skip(s->pb, 4);
  93. bom = avio_rb16(s->pb);
  94. if (bom != 0xFEFF && bom != 0xFFFE) {
  95. av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
  96. return AVERROR_INVALIDDATA;
  97. }
  98. if (bom == 0xFFFE)
  99. b->little_endian = 1;
  100. if (!bfstm) {
  101. major = avio_r8(s->pb);
  102. minor = avio_r8(s->pb);
  103. avio_skip(s->pb, 4); // size of file
  104. size = read16(s);
  105. if (size < 14)
  106. return AVERROR_INVALIDDATA;
  107. avio_skip(s->pb, size - 14);
  108. pos = avio_tell(s->pb);
  109. if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
  110. return AVERROR_INVALIDDATA;
  111. } else {
  112. uint32_t info_offset = 0;
  113. uint16_t section_count, header_size, i;
  114. header_size = read16(s); // 6
  115. avio_skip(s->pb, 4); // Unknown constant 0x00030000
  116. avio_skip(s->pb, 4); // size of file
  117. section_count = read16(s);
  118. avio_skip(s->pb, 2); // padding
  119. for (i = 0; avio_tell(s->pb) < header_size
  120. && !(start && info_offset)
  121. && i < section_count; i++) {
  122. uint16_t flag = read16(s);
  123. avio_skip(s->pb, 2);
  124. switch (flag) {
  125. case 0x4000:
  126. info_offset = read32(s);
  127. /*info_size =*/ read32(s);
  128. break;
  129. case 0x4001:
  130. avio_skip(s->pb, 4); // seek offset
  131. avio_skip(s->pb, 4); // seek size
  132. break;
  133. case 0x4002:
  134. start = read32(s) + 8;
  135. avio_skip(s->pb, 4); //data_size = read32(s);
  136. break;
  137. case 0x4003:
  138. avio_skip(s->pb, 4); // REGN offset
  139. avio_skip(s->pb, 4); // REGN size
  140. break;
  141. }
  142. }
  143. if (!info_offset || !start)
  144. return AVERROR_INVALIDDATA;
  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;
  149. }
  150. size = read32(s);
  151. if (size < 192)
  152. return AVERROR_INVALIDDATA;
  153. avio_skip(s->pb, 4); // unknown
  154. h1offset = read32(s);
  155. if (h1offset > size)
  156. return AVERROR_INVALIDDATA;
  157. avio_skip(s->pb, 12);
  158. toffset = read32(s) + 16LL;
  159. if (toffset > size)
  160. return AVERROR_INVALIDDATA;
  161. avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
  162. codec = avio_r8(s->pb);
  163. switch (codec) {
  164. case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
  165. case 1: codec = b->little_endian ?
  166. AV_CODEC_ID_PCM_S16LE_PLANAR :
  167. AV_CODEC_ID_PCM_S16BE_PLANAR; break;
  168. case 2: codec = b->little_endian ?
  169. AV_CODEC_ID_ADPCM_THP_LE :
  170. AV_CODEC_ID_ADPCM_THP; break;
  171. default:
  172. avpriv_request_sample(s, "codec %d", codec);
  173. return AVERROR_PATCHWELCOME;
  174. }
  175. loop = avio_r8(s->pb); // 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;
  180. avio_skip(s->pb, 1); // padding
  181. st->codec->sample_rate = bfstm ? read32(s) : read16(s);
  182. if (!st->codec->sample_rate)
  183. return AVERROR_INVALIDDATA;
  184. if (!bfstm)
  185. avio_skip(s->pb, 2); // padding
  186. if (loop) {
  187. if (av_dict_set_int(&s->metadata, "loop_start",
  188. av_rescale(read32(s), AV_TIME_BASE,
  189. st->codec->sample_rate),
  190. 0) < 0)
  191. return AVERROR(ENOMEM);
  192. } else {
  193. avio_skip(s->pb, 4);
  194. }
  195. st->start_time = 0;
  196. st->duration = read32(s);
  197. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  198. if (!bfstm)
  199. start = read32(s);
  200. b->current_block = 0;
  201. b->block_count = read32(s);
  202. if (b->block_count > UINT16_MAX) {
  203. av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
  204. return AVERROR_INVALIDDATA;
  205. }
  206. b->block_size = read32(s);
  207. if (b->block_size > UINT32_MAX / st->codec->channels)
  208. return AVERROR_INVALIDDATA;
  209. b->samples_per_block = read32(s);
  210. b->last_block_used_bytes = read32(s);
  211. b->last_block_samples = read32(s);
  212. b->last_block_size = read32(s);
  213. if (b->last_block_size > UINT32_MAX / st->codec->channels)
  214. return AVERROR_INVALIDDATA;
  215. if (b->last_block_used_bytes > b->last_block_size)
  216. return AVERROR_INVALIDDATA;
  217. if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
  218. int ch;
  219. avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
  220. if (!bfstm)
  221. toffset = read32(s) + 16LL;
  222. else
  223. toffset = toffset + read32(s) + st->codec->channels * 8 - 8;
  224. if (toffset > size)
  225. return AVERROR_INVALIDDATA;
  226. avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
  227. b->table = av_mallocz(32 * st->codec->channels);
  228. if (!b->table)
  229. return AVERROR(ENOMEM);
  230. for (ch = 0; ch < st->codec->channels; ch++) {
  231. if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
  232. ret = AVERROR_INVALIDDATA;
  233. goto fail;
  234. }
  235. avio_skip(s->pb, bfstm ? 14 : 24);
  236. }
  237. }
  238. if (size < (avio_tell(s->pb) - pos)) {
  239. ret = AVERROR_INVALIDDATA;
  240. goto fail;
  241. }
  242. avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
  243. while (!avio_feof(s->pb)) {
  244. chunk = avio_rl32(s->pb);
  245. size = read32(s);
  246. if (size < 8) {
  247. ret = AVERROR_INVALIDDATA;
  248. goto fail;
  249. }
  250. size -= 8;
  251. switch (chunk) {
  252. case MKTAG('S','E','E','K'):
  253. case MKTAG('A','D','P','C'):
  254. if (codec != AV_CODEC_ID_ADPCM_THP &&
  255. codec != AV_CODEC_ID_ADPCM_THP_LE)
  256. goto skip;
  257. asize = b->block_count * st->codec->channels * 4;
  258. if (size < asize) {
  259. ret = AVERROR_INVALIDDATA;
  260. goto fail;
  261. }
  262. if (b->adpc) {
  263. av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
  264. goto skip;
  265. } else {
  266. b->adpc = av_mallocz(asize);
  267. if (!b->adpc) {
  268. ret = AVERROR(ENOMEM);
  269. goto fail;
  270. }
  271. if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
  272. // Big-endian BFSTMs have little-endian SEEK tables
  273. // for some strange reason.
  274. int i;
  275. for (i = 0; i < asize; i += 2) {
  276. b->adpc[i+1] = avio_r8(s->pb);
  277. b->adpc[i] = avio_r8(s->pb);
  278. }
  279. } else {
  280. avio_read(s->pb, b->adpc, asize);
  281. }
  282. avio_skip(s->pb, size - asize);
  283. }
  284. break;
  285. case MKTAG('D','A','T','A'):
  286. if ((start < avio_tell(s->pb)) ||
  287. (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
  288. codec == AV_CODEC_ID_ADPCM_THP_LE))) {
  289. ret = AVERROR_INVALIDDATA;
  290. goto fail;
  291. }
  292. avio_skip(s->pb, start - avio_tell(s->pb));
  293. if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
  294. codec == AV_CODEC_ID_ADPCM_THP_LE))
  295. avio_skip(s->pb, 24);
  296. b->data_start = avio_tell(s->pb);
  297. if (!bfstm && (major != 1 || minor))
  298. avpriv_request_sample(s, "Version %d.%d", major, minor);
  299. return 0;
  300. default:
  301. av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
  302. skip:
  303. avio_skip(s->pb, size);
  304. }
  305. }
  306. fail:
  307. read_close(s);
  308. return ret;
  309. }
  310. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  311. {
  312. AVCodecContext *codec = s->streams[0]->codec;
  313. BRSTMDemuxContext *b = s->priv_data;
  314. uint32_t samples, size, skip = 0;
  315. int ret, i;
  316. if (avio_feof(s->pb))
  317. return AVERROR_EOF;
  318. b->current_block++;
  319. if (b->current_block == b->block_count) {
  320. size = b->last_block_used_bytes;
  321. samples = b->last_block_samples;
  322. skip = b->last_block_size - b->last_block_used_bytes;
  323. if (samples < size * 14 / 8) {
  324. uint32_t adjusted_size = samples / 14 * 8;
  325. if (samples % 14)
  326. adjusted_size += (samples % 14 + 1) / 2 + 1;
  327. skip += size - adjusted_size;
  328. size = adjusted_size;
  329. }
  330. } else if (b->current_block < b->block_count) {
  331. size = b->block_size;
  332. samples = b->samples_per_block;
  333. } else {
  334. return AVERROR_EOF;
  335. }
  336. if (codec->codec_id == AV_CODEC_ID_ADPCM_THP ||
  337. codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
  338. uint8_t *dst;
  339. if (size > (INT_MAX - 32 - 4) ||
  340. (32 + 4 + size) > (INT_MAX / codec->channels) ||
  341. (32 + 4 + size) * codec->channels > INT_MAX - 8)
  342. return AVERROR_INVALIDDATA;
  343. if (av_new_packet(pkt, 8 + (32 + 4 + size) * codec->channels) < 0)
  344. return AVERROR(ENOMEM);
  345. dst = pkt->data;
  346. if (codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
  347. bytestream_put_le32(&dst, size * codec->channels);
  348. bytestream_put_le32(&dst, samples);
  349. } else {
  350. bytestream_put_be32(&dst, size * codec->channels);
  351. bytestream_put_be32(&dst, samples);
  352. }
  353. bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
  354. bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
  355. (b->current_block - 1), 4 * codec->channels);
  356. for (i = 0; i < codec->channels; i++) {
  357. ret = avio_read(s->pb, dst, size);
  358. dst += size;
  359. avio_skip(s->pb, skip);
  360. if (ret != size) {
  361. av_packet_unref(pkt);
  362. break;
  363. }
  364. }
  365. pkt->duration = samples;
  366. } else {
  367. size *= codec->channels;
  368. ret = av_get_packet(s->pb, pkt, size);
  369. }
  370. pkt->stream_index = 0;
  371. if (ret != size)
  372. ret = AVERROR(EIO);
  373. return ret;
  374. }
  375. static int read_seek(AVFormatContext *s, int stream_index,
  376. int64_t timestamp, int flags)
  377. {
  378. AVStream *st = s->streams[stream_index];
  379. BRSTMDemuxContext *b = s->priv_data;
  380. int64_t ret = 0;
  381. timestamp /= b->samples_per_block;
  382. ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
  383. st->codec->channels, SEEK_SET);
  384. if (ret < 0)
  385. return ret;
  386. b->current_block = timestamp;
  387. ff_update_cur_dts(s, st, timestamp * b->samples_per_block);
  388. return 0;
  389. }
  390. AVInputFormat ff_brstm_demuxer = {
  391. .name = "brstm",
  392. .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
  393. .priv_data_size = sizeof(BRSTMDemuxContext),
  394. .read_probe = probe,
  395. .read_header = read_header,
  396. .read_packet = read_packet,
  397. .read_close = read_close,
  398. .read_seek = read_seek,
  399. .extensions = "brstm",
  400. };
  401. AVInputFormat ff_bfstm_demuxer = {
  402. .name = "bfstm",
  403. .long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
  404. .priv_data_size = sizeof(BRSTMDemuxContext),
  405. .read_probe = probe_bfstm,
  406. .read_header = read_header,
  407. .read_packet = read_packet,
  408. .read_close = read_close,
  409. .read_seek = read_seek,
  410. .extensions = "bfstm,bcstm",
  411. };