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.

207 lines
6.5KB

  1. /*
  2. * Rayman 2 APM 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/internal.h"
  25. #include "libavutil/intreadwrite.h"
  26. #define APM_FILE_HEADER_SIZE 18
  27. #define APM_FILE_EXTRADATA_SIZE 80
  28. #define APM_MAX_READ_SIZE 4096
  29. #define APM_TAG_CODEC 0x2000
  30. #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
  31. #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
  32. typedef struct APMState {
  33. int32_t has_saved;
  34. int32_t predictor_r;
  35. int32_t step_index_r;
  36. int32_t saved_r;
  37. int32_t predictor_l;
  38. int32_t step_index_l;
  39. int32_t saved_l;
  40. } APMState;
  41. typedef struct APMVS12Chunk {
  42. uint32_t magic;
  43. uint32_t file_size;
  44. uint32_t data_size;
  45. uint32_t unk1;
  46. uint32_t unk2;
  47. APMState state;
  48. uint32_t pad[7];
  49. uint32_t data;
  50. } APMVS12Chunk;
  51. static void apm_parse_vs12(APMVS12Chunk *vs12, const uint8_t *buf)
  52. {
  53. vs12->magic = AV_RL32(buf + 0);
  54. vs12->file_size = AV_RL32(buf + 4);
  55. vs12->data_size = AV_RL32(buf + 8);
  56. vs12->unk1 = AV_RL32(buf + 12);
  57. vs12->unk2 = AV_RL32(buf + 16);
  58. vs12->state.has_saved = AV_RL32(buf + 20);
  59. vs12->state.predictor_r = AV_RL32(buf + 24);
  60. vs12->state.step_index_r = AV_RL32(buf + 28);
  61. vs12->state.saved_r = AV_RL32(buf + 32);
  62. vs12->state.predictor_l = AV_RL32(buf + 36);
  63. vs12->state.step_index_l = AV_RL32(buf + 40);
  64. vs12->state.saved_l = AV_RL32(buf + 44);
  65. for (int i = 0; i < FF_ARRAY_ELEMS(vs12->pad); i++)
  66. vs12->pad[i] = AV_RL32(buf + 48 + (i * 4));
  67. vs12->data = AV_RL32(buf + 76);
  68. }
  69. static int apm_probe(const AVProbeData *p)
  70. {
  71. if (AV_RL16(p->buf) != APM_TAG_CODEC)
  72. return 0;
  73. if (p->buf_size < 100)
  74. return 0;
  75. if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
  76. return 0;
  77. if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
  78. return 0;
  79. return AVPROBE_SCORE_MAX - 1;
  80. }
  81. static int apm_read_header(AVFormatContext *s)
  82. {
  83. int64_t ret;
  84. AVStream *st;
  85. APMVS12Chunk vs12;
  86. uint8_t buf[APM_FILE_EXTRADATA_SIZE];
  87. if (!(st = avformat_new_stream(s, NULL)))
  88. return AVERROR(ENOMEM);
  89. /*
  90. * This is 98% a WAVEFORMATEX, but there's something screwy with the extradata
  91. * that ff_get_wav_header() can't (and shouldn't) handle properly.
  92. */
  93. if (avio_rl16(s->pb) != APM_TAG_CODEC)
  94. return AVERROR_INVALIDDATA;
  95. st->codecpar->channels = avio_rl16(s->pb);
  96. st->codecpar->sample_rate = avio_rl32(s->pb);
  97. /* Skip the bitrate, it's usually wrong anyway. */
  98. if ((ret = avio_skip(s->pb, 4)) < 0)
  99. return ret;
  100. st->codecpar->block_align = avio_rl16(s->pb);
  101. st->codecpar->bits_per_coded_sample = avio_rl16(s->pb);
  102. if (avio_rl32(s->pb) != APM_FILE_EXTRADATA_SIZE)
  103. return AVERROR_INVALIDDATA;
  104. /* I've never seen files greater than this. */
  105. if (st->codecpar->sample_rate > 44100)
  106. return AVERROR_INVALIDDATA;
  107. if (st->codecpar->bits_per_coded_sample != 4)
  108. return AVERROR_INVALIDDATA;
  109. if (st->codecpar->channels == 2)
  110. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  111. else if (st->codecpar->channels == 1)
  112. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  113. else
  114. return AVERROR_INVALIDDATA;
  115. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  116. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_APM;
  117. st->codecpar->format = AV_SAMPLE_FMT_S16;
  118. st->codecpar->bits_per_raw_sample = 16;
  119. st->codecpar->bit_rate = st->codecpar->channels *
  120. st->codecpar->sample_rate *
  121. st->codecpar->bits_per_coded_sample;
  122. if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
  123. return ret;
  124. else if (ret != APM_FILE_EXTRADATA_SIZE)
  125. return AVERROR(EIO);
  126. apm_parse_vs12(&vs12, buf);
  127. if (vs12.magic != APM_TAG_VS12 || vs12.data != APM_TAG_DATA)
  128. return AVERROR_INVALIDDATA;
  129. if (vs12.state.has_saved) {
  130. avpriv_request_sample(s, "Saved Samples");
  131. return AVERROR_PATCHWELCOME;
  132. }
  133. if ((ret = ff_alloc_extradata(st->codecpar, 16)) < 0)
  134. return ret;
  135. AV_WL32(st->codecpar->extradata + 0, vs12.state.predictor_l);
  136. AV_WL32(st->codecpar->extradata + 4, vs12.state.step_index_l);
  137. AV_WL32(st->codecpar->extradata + 8, vs12.state.predictor_r);
  138. AV_WL32(st->codecpar->extradata + 12, vs12.state.step_index_r);
  139. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  140. st->start_time = 0;
  141. st->duration = vs12.data_size *
  142. (8 / st->codecpar->bits_per_coded_sample) /
  143. st->codecpar->channels;
  144. return 0;
  145. }
  146. static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
  147. {
  148. int ret;
  149. AVCodecParameters *par = s->streams[0]->codecpar;
  150. /*
  151. * For future reference: if files with the `has_saved` field set ever
  152. * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
  153. * that should be sent to the decoder before the actual data.
  154. */
  155. if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
  156. return ret;
  157. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  158. pkt->stream_index = 0;
  159. pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
  160. return 0;
  161. }
  162. AVInputFormat ff_apm_demuxer = {
  163. .name = "apm",
  164. .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
  165. .read_probe = apm_probe,
  166. .read_header = apm_read_header,
  167. .read_packet = apm_read_packet
  168. };