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.

325 lines
10KB

  1. /*
  2. * Pro Pinball Series Soundbank (44c, 22c, 11c, 5c) demuxer.
  3. *
  4. * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/internal.h"
  27. #define PP_BNK_MAX_READ_SIZE 4096
  28. #define PP_BNK_FILE_HEADER_SIZE 20
  29. #define PP_BNK_TRACK_SIZE 20
  30. typedef struct PPBnkHeader {
  31. uint32_t bank_id; /*< Bank ID, useless for our purposes. */
  32. uint32_t sample_rate; /*< Sample rate of the contained tracks. */
  33. uint32_t always1; /*< Unknown, always seems to be 1. */
  34. uint32_t track_count; /*< Number of tracks in the file. */
  35. uint32_t flags; /*< Flags. */
  36. } PPBnkHeader;
  37. typedef struct PPBnkTrack {
  38. uint32_t id; /*< Track ID. Usually track[i].id == track[i-1].id + 1, but not always */
  39. uint32_t size; /*< Size of the data in bytes. */
  40. uint32_t sample_rate; /*< Sample rate. */
  41. uint32_t always1_1; /*< Unknown, always seems to be 1. */
  42. uint32_t always1_2; /*< Unknown, always seems to be 1. */
  43. } PPBnkTrack;
  44. typedef struct PPBnkCtxTrack {
  45. int64_t data_offset;
  46. uint32_t data_size;
  47. uint32_t bytes_read;
  48. } PPBnkCtxTrack;
  49. typedef struct PPBnkCtx {
  50. int track_count;
  51. PPBnkCtxTrack *tracks;
  52. uint32_t current_track;
  53. int is_music;
  54. } PPBnkCtx;
  55. enum {
  56. PP_BNK_FLAG_PERSIST = (1 << 0), /*< This is a large file, keep in memory. */
  57. PP_BNK_FLAG_MUSIC = (1 << 1), /*< This is music. */
  58. PP_BNK_FLAG_MASK = (PP_BNK_FLAG_PERSIST | PP_BNK_FLAG_MUSIC)
  59. };
  60. static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
  61. {
  62. hdr->bank_id = AV_RL32(buf + 0);
  63. hdr->sample_rate = AV_RL32(buf + 4);
  64. hdr->always1 = AV_RL32(buf + 8);
  65. hdr->track_count = AV_RL32(buf + 12);
  66. hdr->flags = AV_RL32(buf + 16);
  67. }
  68. static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
  69. {
  70. trk->id = AV_RL32(buf + 0);
  71. trk->size = AV_RL32(buf + 4);
  72. trk->sample_rate = AV_RL32(buf + 8);
  73. trk->always1_1 = AV_RL32(buf + 12);
  74. trk->always1_2 = AV_RL32(buf + 16);
  75. }
  76. static int pp_bnk_probe(const AVProbeData *p)
  77. {
  78. uint32_t sample_rate = AV_RL32(p->buf + 4);
  79. uint32_t track_count = AV_RL32(p->buf + 12);
  80. uint32_t flags = AV_RL32(p->buf + 16);
  81. if (track_count == 0 || track_count > INT_MAX)
  82. return 0;
  83. if ((sample_rate != 5512) && (sample_rate != 11025) &&
  84. (sample_rate != 22050) && (sample_rate != 44100))
  85. return 0;
  86. /* Check the first track header. */
  87. if (AV_RL32(p->buf + 28) != sample_rate)
  88. return 0;
  89. if ((flags & ~PP_BNK_FLAG_MASK) != 0)
  90. return 0;
  91. return AVPROBE_SCORE_MAX / 4 + 1;
  92. }
  93. static int pp_bnk_read_header(AVFormatContext *s)
  94. {
  95. int64_t ret;
  96. AVStream *st;
  97. AVCodecParameters *par;
  98. PPBnkCtx *ctx = s->priv_data;
  99. uint8_t buf[FFMAX(PP_BNK_FILE_HEADER_SIZE, PP_BNK_TRACK_SIZE)];
  100. PPBnkHeader hdr;
  101. if ((ret = avio_read(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
  102. return ret;
  103. else if (ret != PP_BNK_FILE_HEADER_SIZE)
  104. return AVERROR(EIO);
  105. pp_bnk_parse_header(&hdr, buf);
  106. if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
  107. return AVERROR_INVALIDDATA;
  108. if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
  109. return AVERROR_INVALIDDATA;
  110. if (hdr.always1 != 1) {
  111. avpriv_request_sample(s, "Non-one header value");
  112. return AVERROR_PATCHWELCOME;
  113. }
  114. ctx->track_count = hdr.track_count;
  115. if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
  116. return AVERROR(ENOMEM);
  117. /* Parse and validate each track. */
  118. for (int i = 0; i < hdr.track_count; i++) {
  119. PPBnkTrack e;
  120. PPBnkCtxTrack *trk = ctx->tracks + i;
  121. ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
  122. if (ret < 0 && ret != AVERROR_EOF)
  123. goto fail;
  124. /* Short byte-count or EOF, we have a truncated file. */
  125. if (ret != PP_BNK_TRACK_SIZE) {
  126. av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
  127. i, hdr.track_count);
  128. ctx->track_count = i;
  129. break;
  130. }
  131. pp_bnk_parse_track(&e, buf);
  132. /* The individual sample rates of all tracks must match that of the file header. */
  133. if (e.sample_rate != hdr.sample_rate) {
  134. ret = AVERROR_INVALIDDATA;
  135. goto fail;
  136. }
  137. if (e.always1_1 != 1 || e.always1_2 != 1) {
  138. avpriv_request_sample(s, "Non-one track header values");
  139. ret = AVERROR_PATCHWELCOME;
  140. goto fail;
  141. }
  142. trk->data_offset = avio_tell(s->pb);
  143. trk->data_size = e.size;
  144. trk->bytes_read = 0;
  145. /*
  146. * Skip over the data to the next stream header.
  147. * Sometimes avio_skip() doesn't detect EOF. If it doesn't, either:
  148. * - the avio_read() above will, or
  149. * - pp_bnk_read_packet() will read a truncated last track.
  150. */
  151. if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
  152. ctx->track_count = i + 1;
  153. av_log(s, AV_LOG_WARNING,
  154. "Track %d has truncated data, assuming track count == %d\n",
  155. i, ctx->track_count);
  156. break;
  157. } else if (ret < 0) {
  158. goto fail;
  159. }
  160. }
  161. /* File is only a header. */
  162. if (ctx->track_count == 0) {
  163. ret = AVERROR_INVALIDDATA;
  164. goto fail;
  165. }
  166. ctx->is_music = (hdr.flags & PP_BNK_FLAG_MUSIC) &&
  167. (ctx->track_count == 2) &&
  168. (ctx->tracks[0].data_size == ctx->tracks[1].data_size);
  169. /* Build the streams. */
  170. for (int i = 0; i < (ctx->is_music ? 1 : ctx->track_count); i++) {
  171. if (!(st = avformat_new_stream(s, NULL))) {
  172. ret = AVERROR(ENOMEM);
  173. goto fail;
  174. }
  175. par = st->codecpar;
  176. par->codec_type = AVMEDIA_TYPE_AUDIO;
  177. par->codec_id = AV_CODEC_ID_ADPCM_IMA_CUNNING;
  178. par->format = AV_SAMPLE_FMT_S16P;
  179. if (ctx->is_music) {
  180. par->channel_layout = AV_CH_LAYOUT_STEREO;
  181. par->channels = 2;
  182. } else {
  183. par->channel_layout = AV_CH_LAYOUT_MONO;
  184. par->channels = 1;
  185. }
  186. par->sample_rate = hdr.sample_rate;
  187. par->bits_per_coded_sample = 4;
  188. par->bits_per_raw_sample = 16;
  189. par->block_align = 1;
  190. par->bit_rate = par->sample_rate * par->bits_per_coded_sample * par->channels;
  191. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  192. st->start_time = 0;
  193. st->duration = ctx->tracks[i].data_size * 2;
  194. }
  195. return 0;
  196. fail:
  197. av_freep(&ctx->tracks);
  198. return ret;
  199. }
  200. static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
  201. {
  202. PPBnkCtx *ctx = s->priv_data;
  203. /*
  204. * Read a packet from each track, round-robin style.
  205. * This method is nasty, but needed to avoid "Too many packets buffered" errors.
  206. */
  207. for (int i = 0; i < ctx->track_count; i++, ctx->current_track++)
  208. {
  209. int64_t ret;
  210. int size;
  211. PPBnkCtxTrack *trk;
  212. ctx->current_track %= ctx->track_count;
  213. trk = ctx->tracks + ctx->current_track;
  214. if (trk->bytes_read == trk->data_size)
  215. continue;
  216. if ((ret = avio_seek(s->pb, trk->data_offset + trk->bytes_read, SEEK_SET)) < 0)
  217. return ret;
  218. else if (ret != trk->data_offset + trk->bytes_read)
  219. return AVERROR(EIO);
  220. size = FFMIN(trk->data_size - trk->bytes_read, PP_BNK_MAX_READ_SIZE);
  221. if (!ctx->is_music) {
  222. ret = av_get_packet(s->pb, pkt, size);
  223. if (ret == AVERROR_EOF) {
  224. /* If we've hit EOF, don't attempt this track again. */
  225. trk->data_size = trk->bytes_read;
  226. continue;
  227. }
  228. } else {
  229. if (!pkt->data && (ret = av_new_packet(pkt, size * 2)) < 0)
  230. return ret;
  231. ret = avio_read(s->pb, pkt->data + size * ctx->current_track, size);
  232. if (ret >= 0 && ret != size) {
  233. /* Only return stereo packets if both tracks could be read. */
  234. ret = AVERROR_EOF;
  235. }
  236. }
  237. if (ret < 0)
  238. return ret;
  239. trk->bytes_read += ret;
  240. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  241. pkt->stream_index = ctx->current_track;
  242. pkt->duration = ret * 2;
  243. if (ctx->is_music) {
  244. if (pkt->stream_index == 0)
  245. continue;
  246. pkt->stream_index = 0;
  247. }
  248. ctx->current_track++;
  249. return 0;
  250. }
  251. /* If we reach here, we're done. */
  252. return AVERROR_EOF;
  253. }
  254. static int pp_bnk_read_close(AVFormatContext *s)
  255. {
  256. PPBnkCtx *ctx = s->priv_data;
  257. av_freep(&ctx->tracks);
  258. return 0;
  259. }
  260. AVInputFormat ff_pp_bnk_demuxer = {
  261. .name = "pp_bnk",
  262. .long_name = NULL_IF_CONFIG_SMALL("Pro Pinball Series Soundbank"),
  263. .priv_data_size = sizeof(PPBnkCtx),
  264. .read_probe = pp_bnk_probe,
  265. .read_header = pp_bnk_read_header,
  266. .read_packet = pp_bnk_read_packet,
  267. .read_close = pp_bnk_read_close
  268. };