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.

208 lines
6.2KB

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