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.

293 lines
9.2KB

  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. } PPBnkCtx;
  54. enum {
  55. PP_BNK_FLAG_PERSIST = (1 << 0), /*< This is a large file, keep in memory. */
  56. PP_BNK_FLAG_MUSIC = (1 << 1), /*< This is music. */
  57. PP_BNK_FLAG_MASK = (PP_BNK_FLAG_PERSIST | PP_BNK_FLAG_MUSIC)
  58. };
  59. static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
  60. {
  61. hdr->bank_id = AV_RL32(buf + 0);
  62. hdr->sample_rate = AV_RL32(buf + 4);
  63. hdr->always1 = AV_RL32(buf + 8);
  64. hdr->track_count = AV_RL32(buf + 12);
  65. hdr->flags = AV_RL32(buf + 16);
  66. }
  67. static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
  68. {
  69. trk->id = AV_RL32(buf + 0);
  70. trk->size = AV_RL32(buf + 4);
  71. trk->sample_rate = AV_RL32(buf + 8);
  72. trk->always1_1 = AV_RL32(buf + 12);
  73. trk->always1_2 = AV_RL32(buf + 16);
  74. }
  75. static int pp_bnk_probe(const AVProbeData *p)
  76. {
  77. uint32_t sample_rate = AV_RL32(p->buf + 4);
  78. uint32_t track_count = AV_RL32(p->buf + 12);
  79. uint32_t flags = AV_RL32(p->buf + 16);
  80. if (track_count == 0 || track_count > INT_MAX)
  81. return 0;
  82. if ((sample_rate != 5512) && (sample_rate != 11025) &&
  83. (sample_rate != 22050) && (sample_rate != 44100))
  84. return 0;
  85. /* Check the first track header. */
  86. if (AV_RL32(p->buf + 28) != sample_rate)
  87. return 0;
  88. if ((flags & ~PP_BNK_FLAG_MASK) != 0)
  89. return 0;
  90. return AVPROBE_SCORE_MAX / 4 + 1;
  91. }
  92. static int pp_bnk_read_header(AVFormatContext *s)
  93. {
  94. int64_t ret;
  95. AVStream *st;
  96. AVCodecParameters *par;
  97. PPBnkCtx *ctx = s->priv_data;
  98. uint8_t buf[FFMAX(PP_BNK_FILE_HEADER_SIZE, PP_BNK_TRACK_SIZE)];
  99. PPBnkHeader hdr;
  100. if ((ret = avio_read(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
  101. return ret;
  102. else if (ret != PP_BNK_FILE_HEADER_SIZE)
  103. return AVERROR(EIO);
  104. pp_bnk_parse_header(&hdr, buf);
  105. if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
  106. return AVERROR_INVALIDDATA;
  107. if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
  108. return AVERROR_INVALIDDATA;
  109. if (hdr.always1 != 1) {
  110. avpriv_request_sample(s, "Non-one header value");
  111. return AVERROR_PATCHWELCOME;
  112. }
  113. ctx->track_count = hdr.track_count;
  114. if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
  115. return AVERROR(ENOMEM);
  116. /* Parse and validate each track. */
  117. for (int i = 0; i < hdr.track_count; i++) {
  118. PPBnkTrack e;
  119. PPBnkCtxTrack *trk = ctx->tracks + i;
  120. ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
  121. if (ret < 0 && ret != AVERROR_EOF)
  122. goto fail;
  123. /* Short byte-count or EOF, we have a truncated file. */
  124. if (ret != PP_BNK_TRACK_SIZE) {
  125. av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
  126. i, hdr.track_count);
  127. ctx->track_count = i;
  128. break;
  129. }
  130. pp_bnk_parse_track(&e, buf);
  131. /* The individual sample rates of all tracks must match that of the file header. */
  132. if (e.sample_rate != hdr.sample_rate) {
  133. ret = AVERROR_INVALIDDATA;
  134. goto fail;
  135. }
  136. if (e.always1_1 != 1 || e.always1_2 != 1) {
  137. avpriv_request_sample(s, "Non-one track header values");
  138. ret = AVERROR_PATCHWELCOME;
  139. goto fail;
  140. }
  141. trk->data_offset = avio_tell(s->pb);
  142. trk->data_size = e.size;
  143. trk->bytes_read = 0;
  144. /*
  145. * Skip over the data to the next stream header.
  146. * Sometimes avio_skip() doesn't detect EOF. If it doesn't, either:
  147. * - the avio_read() above will, or
  148. * - pp_bnk_read_packet() will read a truncated last track.
  149. */
  150. if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
  151. ctx->track_count = i + 1;
  152. av_log(s, AV_LOG_WARNING,
  153. "Track %d has truncated data, assuming track count == %d\n",
  154. i, ctx->track_count);
  155. break;
  156. } else if (ret < 0) {
  157. goto fail;
  158. }
  159. }
  160. /* File is only a header. */
  161. if (ctx->track_count == 0) {
  162. ret = AVERROR_INVALIDDATA;
  163. goto fail;
  164. }
  165. /* Build the streams. */
  166. for (int i = 0; i < ctx->track_count; i++) {
  167. if (!(st = avformat_new_stream(s, NULL))) {
  168. ret = AVERROR(ENOMEM);
  169. goto fail;
  170. }
  171. par = st->codecpar;
  172. par->codec_type = AVMEDIA_TYPE_AUDIO;
  173. par->codec_id = AV_CODEC_ID_ADPCM_IMA_CUNNING;
  174. par->format = AV_SAMPLE_FMT_S16;
  175. par->channel_layout = AV_CH_LAYOUT_MONO;
  176. par->channels = 1;
  177. par->sample_rate = hdr.sample_rate;
  178. par->bits_per_coded_sample = 4;
  179. par->bits_per_raw_sample = 16;
  180. par->block_align = 1;
  181. par->bit_rate = par->sample_rate * par->bits_per_coded_sample;
  182. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  183. st->start_time = 0;
  184. st->duration = ctx->tracks[i].data_size * 2;
  185. }
  186. return 0;
  187. fail:
  188. av_freep(&ctx->tracks);
  189. return ret;
  190. }
  191. static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
  192. {
  193. PPBnkCtx *ctx = s->priv_data;
  194. /*
  195. * Read a packet from each track, round-robin style.
  196. * This method is nasty, but needed to avoid "Too many packets buffered" errors.
  197. */
  198. for (int i = 0; i < ctx->track_count; i++, ctx->current_track++)
  199. {
  200. int64_t ret;
  201. int size;
  202. PPBnkCtxTrack *trk;
  203. ctx->current_track %= ctx->track_count;
  204. trk = ctx->tracks + ctx->current_track;
  205. if (trk->bytes_read == trk->data_size)
  206. continue;
  207. if ((ret = avio_seek(s->pb, trk->data_offset + trk->bytes_read, SEEK_SET)) < 0)
  208. return ret;
  209. else if (ret != trk->data_offset + trk->bytes_read)
  210. return AVERROR(EIO);
  211. size = FFMIN(trk->data_size - trk->bytes_read, PP_BNK_MAX_READ_SIZE);
  212. if ((ret = av_get_packet(s->pb, pkt, size)) == AVERROR_EOF) {
  213. /* If we've hit EOF, don't attempt this track again. */
  214. trk->data_size = trk->bytes_read;
  215. continue;
  216. } else if (ret < 0) {
  217. return ret;
  218. }
  219. trk->bytes_read += ret;
  220. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  221. pkt->stream_index = ctx->current_track++;
  222. pkt->duration = ret * 2;
  223. return 0;
  224. }
  225. /* If we reach here, we're done. */
  226. return AVERROR_EOF;
  227. }
  228. static int pp_bnk_read_close(AVFormatContext *s)
  229. {
  230. PPBnkCtx *ctx = s->priv_data;
  231. av_freep(&ctx->tracks);
  232. return 0;
  233. }
  234. AVInputFormat ff_pp_bnk_demuxer = {
  235. .name = "pp_bnk",
  236. .long_name = NULL_IF_CONFIG_SMALL("Pro Pinball Series Soundbank"),
  237. .priv_data_size = sizeof(PPBnkCtx),
  238. .read_probe = pp_bnk_probe,
  239. .read_header = pp_bnk_read_header,
  240. .read_packet = pp_bnk_read_packet,
  241. .read_close = pp_bnk_read_close
  242. };