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.

298 lines
8.6KB

  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. uint8_t *table;
  32. uint8_t *adpc;
  33. } BRSTMDemuxContext;
  34. static int probe(AVProbeData *p)
  35. {
  36. if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
  37. (AV_RL16(p->buf + 4) == 0xFFFE ||
  38. AV_RL16(p->buf + 4) == 0xFEFF))
  39. return AVPROBE_SCORE_MAX / 3 * 2;
  40. return 0;
  41. }
  42. static int read_close(AVFormatContext *s)
  43. {
  44. BRSTMDemuxContext *b = s->priv_data;
  45. av_freep(&b->table);
  46. av_freep(&b->adpc);
  47. return 0;
  48. }
  49. static int read_header(AVFormatContext *s)
  50. {
  51. BRSTMDemuxContext *b = s->priv_data;
  52. int bom, major, minor, codec, chunk;
  53. int64_t pos, h1offset, toffset;
  54. uint32_t size, start, asize;
  55. AVStream *st;
  56. int ret = AVERROR_EOF;
  57. st = avformat_new_stream(s, NULL);
  58. if (!st)
  59. return AVERROR(ENOMEM);
  60. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  61. avio_skip(s->pb, 4);
  62. bom = avio_rb16(s->pb);
  63. if (bom != 0xFEFF && bom != 0xFFFE) {
  64. av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
  65. return AVERROR_INVALIDDATA;
  66. }
  67. if (bom == 0xFFFE) {
  68. avpriv_request_sample(s, "little endian byte order");
  69. return AVERROR_PATCHWELCOME;
  70. }
  71. major = avio_r8(s->pb);
  72. minor = avio_r8(s->pb);
  73. avio_skip(s->pb, 4); // size of file
  74. size = avio_rb16(s->pb);
  75. if (size < 14)
  76. return AVERROR_INVALIDDATA;
  77. avio_skip(s->pb, size - 14);
  78. pos = avio_tell(s->pb);
  79. if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
  80. return AVERROR_INVALIDDATA;
  81. size = avio_rb32(s->pb);
  82. if (size < 256)
  83. return AVERROR_INVALIDDATA;
  84. avio_skip(s->pb, 4); // unknown
  85. h1offset = avio_rb32(s->pb);
  86. if (h1offset > size)
  87. return AVERROR_INVALIDDATA;
  88. avio_skip(s->pb, 12);
  89. toffset = avio_rb32(s->pb) + 16LL;
  90. if (toffset > size)
  91. return AVERROR_INVALIDDATA;
  92. avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
  93. codec = avio_r8(s->pb);
  94. switch (codec) {
  95. case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
  96. case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
  97. case 2: codec = AV_CODEC_ID_ADPCM_THP; break;
  98. default:
  99. avpriv_request_sample(s, "codec %d", codec);
  100. return AVERROR_PATCHWELCOME;
  101. }
  102. avio_skip(s->pb, 1); // loop flag
  103. st->codec->codec_id = codec;
  104. st->codec->channels = avio_r8(s->pb);
  105. if (!st->codec->channels)
  106. return AVERROR_INVALIDDATA;
  107. avio_skip(s->pb, 1); // padding
  108. st->codec->sample_rate = avio_rb16(s->pb);
  109. if (!st->codec->sample_rate)
  110. return AVERROR_INVALIDDATA;
  111. avio_skip(s->pb, 2); // padding
  112. avio_skip(s->pb, 4); // loop start sample
  113. st->start_time = 0;
  114. st->duration = avio_rb32(s->pb);
  115. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  116. start = avio_rb32(s->pb);
  117. b->current_block = 0;
  118. b->block_count = avio_rb32(s->pb);
  119. if (b->block_count > UINT16_MAX) {
  120. av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
  121. return AVERROR_INVALIDDATA;
  122. }
  123. b->block_size = avio_rb32(s->pb);
  124. if (b->block_size > UINT16_MAX / st->codec->channels)
  125. return AVERROR_INVALIDDATA;
  126. b->block_size *= st->codec->channels;
  127. b->samples_per_block = avio_rb32(s->pb);
  128. b->last_block_used_bytes = avio_rb32(s->pb);
  129. if (b->last_block_used_bytes > UINT16_MAX / st->codec->channels)
  130. return AVERROR_INVALIDDATA;
  131. b->last_block_used_bytes *= st->codec->channels;
  132. avio_skip(s->pb, 4); // last block samples
  133. avio_skip(s->pb, 4); // last block size
  134. if (codec == AV_CODEC_ID_ADPCM_THP) {
  135. int ch;
  136. avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
  137. toffset = avio_rb32(s->pb) + 16LL;
  138. if (toffset > size)
  139. return AVERROR_INVALIDDATA;
  140. avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
  141. b->table = av_mallocz(32 * st->codec->channels);
  142. if (!b->table)
  143. return AVERROR(ENOMEM);
  144. for (ch = 0; ch < st->codec->channels; ch++) {
  145. if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
  146. ret = AVERROR_INVALIDDATA;
  147. goto fail;
  148. }
  149. avio_skip(s->pb, 24);
  150. }
  151. }
  152. if (size < (avio_tell(s->pb) - pos)) {
  153. ret = AVERROR_INVALIDDATA;
  154. goto fail;
  155. }
  156. avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
  157. while (!url_feof(s->pb)) {
  158. chunk = avio_rl32(s->pb);
  159. size = avio_rb32(s->pb);
  160. if (size < 8) {
  161. ret = AVERROR_INVALIDDATA;
  162. goto fail;
  163. }
  164. size -= 8;
  165. switch (chunk) {
  166. case MKTAG('A','D','P','C'):
  167. if (codec != AV_CODEC_ID_ADPCM_THP)
  168. goto skip;
  169. asize = b->block_count * st->codec->channels * 4;
  170. if (size < asize) {
  171. ret = AVERROR_INVALIDDATA;
  172. goto fail;
  173. }
  174. if (b->adpc) {
  175. av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
  176. goto skip;
  177. } else {
  178. b->adpc = av_mallocz(asize);
  179. if (!b->adpc) {
  180. ret = AVERROR(ENOMEM);
  181. goto fail;
  182. }
  183. avio_read(s->pb, b->adpc, asize);
  184. avio_skip(s->pb, size - asize);
  185. }
  186. break;
  187. case MKTAG('D','A','T','A'):
  188. if ((start < avio_tell(s->pb)) ||
  189. (!b->adpc && codec == AV_CODEC_ID_ADPCM_THP)) {
  190. ret = AVERROR_INVALIDDATA;
  191. goto fail;
  192. }
  193. avio_skip(s->pb, start - avio_tell(s->pb));
  194. if (major != 1 || minor)
  195. avpriv_request_sample(s, "Version %d.%d", major, minor);
  196. return 0;
  197. default:
  198. av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
  199. skip:
  200. avio_skip(s->pb, size);
  201. }
  202. }
  203. fail:
  204. read_close(s);
  205. return ret;
  206. }
  207. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  208. {
  209. AVCodecContext *codec = s->streams[0]->codec;
  210. BRSTMDemuxContext *b = s->priv_data;
  211. uint32_t samples, size;
  212. int ret;
  213. if (url_feof(s->pb))
  214. return AVERROR_EOF;
  215. b->current_block++;
  216. if (b->current_block == b->block_count) {
  217. size = b->last_block_used_bytes;
  218. samples = size / (8 * codec->channels) * 14;
  219. } else if (b->current_block < b->block_count) {
  220. size = b->block_size;
  221. samples = b->samples_per_block;
  222. } else {
  223. return AVERROR_EOF;
  224. }
  225. if (codec->codec_id == AV_CODEC_ID_ADPCM_THP) {
  226. uint8_t *dst;
  227. if (av_new_packet(pkt, 8 + (32 + 4) * codec->channels + size) < 0)
  228. return AVERROR(ENOMEM);
  229. dst = pkt->data;
  230. bytestream_put_be32(&dst, size);
  231. bytestream_put_be32(&dst, samples);
  232. bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
  233. bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
  234. (b->current_block - 1), 4 * codec->channels);
  235. ret = avio_read(s->pb, dst, size);
  236. if (ret != size)
  237. av_free_packet(pkt);
  238. pkt->duration = samples;
  239. } else {
  240. ret = av_get_packet(s->pb, pkt, size);
  241. }
  242. pkt->stream_index = 0;
  243. if (ret != size)
  244. ret = AVERROR(EIO);
  245. return ret;
  246. }
  247. AVInputFormat ff_brstm_demuxer = {
  248. .name = "brstm",
  249. .long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
  250. .priv_data_size = sizeof(BRSTMDemuxContext),
  251. .read_probe = probe,
  252. .read_header = read_header,
  253. .read_packet = read_packet,
  254. .read_close = read_close,
  255. .extensions = "brstm",
  256. };