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.

147 lines
4.3KB

  1. /*
  2. * LEGO Racers ALP (.tun & .pcm) 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/internal.h"
  26. #define ALP_TAG MKTAG('A', 'L', 'P', ' ')
  27. #define ALP_MAX_READ_SIZE 4096
  28. typedef struct ALPHeader {
  29. uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */
  30. uint32_t header_size; /*< Header size (after this). */
  31. char adpcm[6]; /*< "ADPCM" */
  32. uint8_t unk1; /*< Unknown */
  33. uint8_t num_channels; /*< Channel Count. */
  34. uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */
  35. } ALPHeader;
  36. static int alp_probe(const AVProbeData *p)
  37. {
  38. uint32_t i;
  39. if (AV_RL32(p->buf) != ALP_TAG)
  40. return 0;
  41. /* Only allowed header sizes are 8 and 12. */
  42. i = AV_RL32(p->buf + 4);
  43. if (i != 8 && i != 12)
  44. return 0;
  45. if (strncmp("ADPCM", p->buf + 8, 6) != 0)
  46. return 0;
  47. return AVPROBE_SCORE_MAX - 1;
  48. }
  49. static int alp_read_header(AVFormatContext *s)
  50. {
  51. int ret;
  52. AVStream *st;
  53. ALPHeader hdr;
  54. AVCodecParameters *par;
  55. if ((hdr.magic = avio_rl32(s->pb)) != ALP_TAG)
  56. return AVERROR_INVALIDDATA;
  57. hdr.header_size = avio_rl32(s->pb);
  58. if (hdr.header_size != 8 && hdr.header_size != 12) {
  59. return AVERROR_INVALIDDATA;
  60. }
  61. if ((ret = avio_read(s->pb, hdr.adpcm, sizeof(hdr.adpcm))) < 0)
  62. return ret;
  63. else if (ret != sizeof(hdr.adpcm))
  64. return AVERROR(EIO);
  65. if (strncmp("ADPCM", hdr.adpcm, sizeof(hdr.adpcm)) != 0)
  66. return AVERROR_INVALIDDATA;
  67. hdr.unk1 = avio_r8(s->pb);
  68. hdr.num_channels = avio_r8(s->pb);
  69. if (hdr.header_size == 8) {
  70. /* .TUN music file */
  71. hdr.sample_rate = 22050;
  72. } else {
  73. /* .PCM sound file */
  74. hdr.sample_rate = avio_rl32(s->pb);
  75. }
  76. if (hdr.sample_rate > 44100) {
  77. avpriv_request_sample(s, "Sample Rate > 44100");
  78. return AVERROR_PATCHWELCOME;
  79. }
  80. if (!(st = avformat_new_stream(s, NULL)))
  81. return AVERROR(ENOMEM);
  82. par = st->codecpar;
  83. par->codec_type = AVMEDIA_TYPE_AUDIO;
  84. par->codec_id = AV_CODEC_ID_ADPCM_IMA_ALP;
  85. par->format = AV_SAMPLE_FMT_S16;
  86. par->sample_rate = hdr.sample_rate;
  87. par->channels = hdr.num_channels;
  88. if (hdr.num_channels == 1)
  89. par->channel_layout = AV_CH_LAYOUT_MONO;
  90. else if (hdr.num_channels == 2)
  91. par->channel_layout = AV_CH_LAYOUT_STEREO;
  92. else
  93. return AVERROR_INVALIDDATA;
  94. par->bits_per_coded_sample = 4;
  95. par->bits_per_raw_sample = 16;
  96. par->block_align = 1;
  97. par->bit_rate = par->channels *
  98. par->sample_rate *
  99. par->bits_per_coded_sample;
  100. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  101. return 0;
  102. }
  103. static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
  104. {
  105. int ret;
  106. AVCodecParameters *par = s->streams[0]->codecpar;
  107. if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
  108. return ret;
  109. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  110. pkt->stream_index = 0;
  111. pkt->duration = ret * 2 / par->channels;
  112. return 0;
  113. }
  114. AVInputFormat ff_alp_demuxer = {
  115. .name = "alp",
  116. .long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
  117. .read_probe = alp_probe,
  118. .read_header = alp_read_header,
  119. .read_packet = alp_read_packet
  120. };