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.

211 lines
6.4KB

  1. /*
  2. * FSB demuxer
  3. * Copyright (c) 2015 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/avassert.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "avio.h"
  25. #include "internal.h"
  26. static int fsb_probe(AVProbeData *p)
  27. {
  28. if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5)
  29. return 0;
  30. if (AV_RL32(p->buf + 4) != 1)
  31. return 0;
  32. return AVPROBE_SCORE_MAX;
  33. }
  34. static int fsb_read_header(AVFormatContext *s)
  35. {
  36. AVIOContext *pb = s->pb;
  37. unsigned format, version, c;
  38. int64_t offset;
  39. AVCodecContext *codec;
  40. AVStream *st = avformat_new_stream(s, NULL);
  41. avio_skip(pb, 3); // "FSB"
  42. version = avio_r8(pb) - '0';
  43. if (version != 4 && version != 3) {
  44. avpriv_request_sample(s, "version %d", version);
  45. return AVERROR_PATCHWELCOME;
  46. }
  47. avio_skip(pb, 4);
  48. if (!st)
  49. return AVERROR(ENOMEM);
  50. codec = st->codec;
  51. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  52. codec->codec_tag = 0;
  53. if (version == 3) {
  54. offset = avio_rl32(pb) + 0x18;
  55. avio_skip(pb, 44);
  56. st->duration = avio_rl32(pb);
  57. avio_skip(pb, 12);
  58. format = avio_rl32(pb);
  59. codec->sample_rate = avio_rl32(pb);
  60. if (codec->sample_rate <= 0)
  61. return AVERROR_INVALIDDATA;
  62. avio_skip(pb, 6);
  63. codec->channels = avio_rl16(pb);
  64. if (!codec->channels)
  65. return AVERROR_INVALIDDATA;
  66. if (format & 0x00000100) {
  67. codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  68. codec->block_align = 4096 * codec->channels;
  69. } else if (format & 0x00400000) {
  70. codec->bits_per_coded_sample = 4;
  71. codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WAV;
  72. codec->block_align = 36 * codec->channels;
  73. } else if (format & 0x00800000) {
  74. codec->codec_id = AV_CODEC_ID_ADPCM_PSX;
  75. codec->block_align = 16 * codec->channels;
  76. } else if (format & 0x02000000) {
  77. codec->codec_id = AV_CODEC_ID_ADPCM_THP;
  78. codec->block_align = 8 * codec->channels;
  79. if (codec->channels > INT_MAX / 32)
  80. return AVERROR_INVALIDDATA;
  81. ff_alloc_extradata(codec, 32 * codec->channels);
  82. if (!codec->extradata)
  83. return AVERROR(ENOMEM);
  84. avio_seek(pb, 0x68, SEEK_SET);
  85. for (c = 0; c < codec->channels; c++) {
  86. avio_read(pb, codec->extradata + 32 * c, 32);
  87. avio_skip(pb, 14);
  88. }
  89. } else {
  90. avpriv_request_sample(s, "format 0x%X", format);
  91. return AVERROR_PATCHWELCOME;
  92. }
  93. } else if (version == 4) {
  94. offset = avio_rl32(pb) + 0x30;
  95. avio_skip(pb, 80);
  96. st->duration = avio_rl32(pb);
  97. format = avio_rb32(pb);
  98. switch(format) {
  99. case 0x40001001:
  100. case 0x00001005:
  101. case 0x40001081:
  102. case 0x40200001:
  103. codec->codec_id = AV_CODEC_ID_XMA2;
  104. break;
  105. case 0x40000802:
  106. codec->codec_id = AV_CODEC_ID_ADPCM_THP;
  107. break;
  108. default:
  109. avpriv_request_sample(s, "format 0x%X", format);
  110. return AVERROR_PATCHWELCOME;
  111. }
  112. codec->sample_rate = avio_rl32(pb);
  113. if (codec->sample_rate <= 0)
  114. return AVERROR_INVALIDDATA;
  115. avio_skip(pb, 6);
  116. codec->channels = avio_rl16(pb);
  117. if (!codec->channels)
  118. return AVERROR_INVALIDDATA;
  119. switch (codec->codec_id) {
  120. case AV_CODEC_ID_XMA2:
  121. ff_alloc_extradata(codec, 34);
  122. if (!codec->extradata)
  123. return AVERROR(ENOMEM);
  124. memset(codec->extradata, 0, 34);
  125. codec->block_align = 2048;
  126. break;
  127. case AV_CODEC_ID_ADPCM_THP:
  128. if (codec->channels > INT_MAX / 32)
  129. return AVERROR_INVALIDDATA;
  130. ff_alloc_extradata(codec, 32 * codec->channels);
  131. if (!codec->extradata)
  132. return AVERROR(ENOMEM);
  133. avio_seek(pb, 0x80, SEEK_SET);
  134. for (c = 0; c < codec->channels; c++) {
  135. avio_read(pb, codec->extradata + 32 * c, 32);
  136. avio_skip(pb, 14);
  137. }
  138. codec->block_align = 8 * codec->channels;
  139. break;
  140. }
  141. } else {
  142. av_assert0(0);
  143. }
  144. avio_skip(pb, offset - avio_tell(pb));
  145. s->internal->data_offset = avio_tell(pb);
  146. avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
  147. return 0;
  148. }
  149. static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
  150. {
  151. AVCodecContext *codec = s->streams[0]->codec;
  152. int64_t pos;
  153. int ret;
  154. if (avio_feof(s->pb))
  155. return AVERROR_EOF;
  156. pos = avio_tell(s->pb);
  157. if (codec->codec_id == AV_CODEC_ID_ADPCM_THP &&
  158. codec->channels > 1) {
  159. int i, ch;
  160. ret = av_new_packet(pkt, codec->block_align);
  161. if (ret < 0)
  162. return ret;
  163. for (i = 0; i < 4; i++) {
  164. for (ch = 0; ch < codec->channels; ch++) {
  165. pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
  166. pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
  167. }
  168. }
  169. ret = 0;
  170. } else {
  171. ret = av_get_packet(s->pb, pkt, codec->block_align);
  172. }
  173. if (codec->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
  174. pkt->duration = (pkt->data[0] >> 2) * 512;
  175. pkt->pos = pos;
  176. pkt->stream_index = 0;
  177. return ret;
  178. }
  179. AVInputFormat ff_fsb_demuxer = {
  180. .name = "fsb",
  181. .long_name = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"),
  182. .read_probe = fsb_probe,
  183. .read_header = fsb_read_header,
  184. .read_packet = fsb_read_packet,
  185. .extensions = "fsb",
  186. .flags = AVFMT_GENERIC_INDEX,
  187. };