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.

318 lines
9.4KB

  1. /*
  2. * Rayman 2 APM (De)muxer
  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 "rawenc.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/intreadwrite.h"
  28. #define APM_FILE_HEADER_SIZE 18
  29. #define APM_FILE_EXTRADATA_SIZE 80
  30. #define APM_EXTRADATA_SIZE 28
  31. #define APM_MAX_READ_SIZE 4096
  32. #define APM_TAG_CODEC 0x2000
  33. #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
  34. #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
  35. typedef struct APMState {
  36. int32_t has_saved;
  37. int32_t predictor_r;
  38. int32_t step_index_r;
  39. int32_t saved_r;
  40. int32_t predictor_l;
  41. int32_t step_index_l;
  42. int32_t saved_l;
  43. } APMState;
  44. typedef struct APMExtraData {
  45. uint32_t magic;
  46. uint32_t file_size;
  47. uint32_t data_size;
  48. uint32_t unk1;
  49. uint32_t unk2;
  50. APMState state;
  51. uint32_t unk3[7];
  52. uint32_t data;
  53. } APMExtraData;
  54. #if CONFIG_APM_DEMUXER
  55. static void apm_parse_extradata(APMExtraData *ext, const uint8_t *buf)
  56. {
  57. ext->magic = AV_RL32(buf + 0);
  58. ext->file_size = AV_RL32(buf + 4);
  59. ext->data_size = AV_RL32(buf + 8);
  60. ext->unk1 = AV_RL32(buf + 12);
  61. ext->unk2 = AV_RL32(buf + 16);
  62. ext->state.has_saved = AV_RL32(buf + 20);
  63. ext->state.predictor_r = AV_RL32(buf + 24);
  64. ext->state.step_index_r = AV_RL32(buf + 28);
  65. ext->state.saved_r = AV_RL32(buf + 32);
  66. ext->state.predictor_l = AV_RL32(buf + 36);
  67. ext->state.step_index_l = AV_RL32(buf + 40);
  68. ext->state.saved_l = AV_RL32(buf + 44);
  69. for (int i = 0; i < FF_ARRAY_ELEMS(ext->unk3); i++)
  70. ext->unk3[i] = AV_RL32(buf + 48 + (i * 4));
  71. ext->data = AV_RL32(buf + 76);
  72. }
  73. static int apm_probe(const AVProbeData *p)
  74. {
  75. if (AV_RL16(p->buf) != APM_TAG_CODEC)
  76. return 0;
  77. if (p->buf_size < 100)
  78. return 0;
  79. if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
  80. return 0;
  81. if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
  82. return 0;
  83. return AVPROBE_SCORE_MAX - 1;
  84. }
  85. static int apm_read_header(AVFormatContext *s)
  86. {
  87. int64_t ret;
  88. AVStream *st;
  89. APMExtraData extradata;
  90. AVCodecParameters *par;
  91. uint8_t buf[APM_FILE_EXTRADATA_SIZE];
  92. if (!(st = avformat_new_stream(s, NULL)))
  93. return AVERROR(ENOMEM);
  94. /*
  95. * This is 98% a WAVEFORMATEX, but there's something screwy with the extradata
  96. * that ff_get_wav_header() can't (and shouldn't) handle properly.
  97. */
  98. if (avio_rl16(s->pb) != APM_TAG_CODEC)
  99. return AVERROR_INVALIDDATA;
  100. par = st->codecpar;
  101. par->channels = avio_rl16(s->pb);
  102. par->sample_rate = avio_rl32(s->pb);
  103. /* Skip the bitrate, it's usually wrong anyway. */
  104. if ((ret = avio_skip(s->pb, 4)) < 0)
  105. return ret;
  106. par->block_align = avio_rl16(s->pb);
  107. par->bits_per_coded_sample = avio_rl16(s->pb);
  108. if (avio_rl32(s->pb) != APM_FILE_EXTRADATA_SIZE)
  109. return AVERROR_INVALIDDATA;
  110. /* 8 = bits per sample * max channels */
  111. if (par->sample_rate > (INT_MAX / 8))
  112. return AVERROR_INVALIDDATA;
  113. if (par->bits_per_coded_sample != 4)
  114. return AVERROR_INVALIDDATA;
  115. if (par->channels == 2)
  116. par->channel_layout = AV_CH_LAYOUT_STEREO;
  117. else if (par->channels == 1)
  118. par->channel_layout = AV_CH_LAYOUT_MONO;
  119. else
  120. return AVERROR_INVALIDDATA;
  121. par->codec_type = AVMEDIA_TYPE_AUDIO;
  122. par->codec_id = AV_CODEC_ID_ADPCM_IMA_APM;
  123. par->format = AV_SAMPLE_FMT_S16;
  124. par->bits_per_raw_sample = 16;
  125. par->bit_rate = par->channels *
  126. par->sample_rate *
  127. par->bits_per_coded_sample;
  128. if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
  129. return ret;
  130. else if (ret != APM_FILE_EXTRADATA_SIZE)
  131. return AVERROR(EIO);
  132. apm_parse_extradata(&extradata, buf);
  133. if (extradata.magic != APM_TAG_VS12 || extradata.data != APM_TAG_DATA)
  134. return AVERROR_INVALIDDATA;
  135. if (extradata.state.has_saved) {
  136. avpriv_request_sample(s, "Saved Samples");
  137. return AVERROR_PATCHWELCOME;
  138. }
  139. if ((ret = ff_alloc_extradata(par, APM_EXTRADATA_SIZE)) < 0)
  140. return ret;
  141. /* Use the entire state as extradata. */
  142. memcpy(par->extradata, buf + 20, APM_EXTRADATA_SIZE);
  143. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  144. st->start_time = 0;
  145. st->duration = extradata.data_size *
  146. (8 / par->bits_per_coded_sample) /
  147. par->channels;
  148. return 0;
  149. }
  150. static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
  151. {
  152. int ret;
  153. AVCodecParameters *par = s->streams[0]->codecpar;
  154. /*
  155. * For future reference: if files with the `has_saved` field set ever
  156. * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
  157. * that should be sent to the decoder before the actual data.
  158. */
  159. if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
  160. return ret;
  161. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  162. pkt->stream_index = 0;
  163. pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
  164. return 0;
  165. }
  166. AVInputFormat ff_apm_demuxer = {
  167. .name = "apm",
  168. .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
  169. .read_probe = apm_probe,
  170. .read_header = apm_read_header,
  171. .read_packet = apm_read_packet
  172. };
  173. #endif
  174. #if CONFIG_APM_MUXER
  175. static int apm_write_init(AVFormatContext *s)
  176. {
  177. AVCodecParameters *par;
  178. if (s->nb_streams != 1) {
  179. av_log(s, AV_LOG_ERROR, "APM files have exactly one stream\n");
  180. return AVERROR(EINVAL);
  181. }
  182. par = s->streams[0]->codecpar;
  183. if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_APM) {
  184. av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
  185. avcodec_get_name(par->codec_id));
  186. return AVERROR(EINVAL);
  187. }
  188. if (par->channels > 2) {
  189. av_log(s, AV_LOG_ERROR, "APM files only support up to 2 channels\n");
  190. return AVERROR(EINVAL);
  191. }
  192. if (par->sample_rate > (INT_MAX / 8)) {
  193. av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
  194. return AVERROR(EINVAL);
  195. }
  196. if (par->extradata_size != APM_EXTRADATA_SIZE) {
  197. av_log(s, AV_LOG_ERROR, "Invalid/missing extradata\n");
  198. return AVERROR(EINVAL);
  199. }
  200. if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  201. av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
  202. return AVERROR(EINVAL);
  203. }
  204. return 0;
  205. }
  206. static int apm_write_header(AVFormatContext *s)
  207. {
  208. uint8_t buf[APM_FILE_EXTRADATA_SIZE] = { 0 };
  209. AVCodecParameters *par = s->streams[0]->codecpar;
  210. /*
  211. * Bodge a WAVEFORMATEX manually, ff_put_wav_header() can't
  212. * be used because of the extra 2 bytes.
  213. */
  214. avio_wl16(s->pb, APM_TAG_CODEC);
  215. avio_wl16(s->pb, par->channels);
  216. avio_wl32(s->pb, par->sample_rate);
  217. /* This is the wrong calculation, but it's what the orginal files have. */
  218. avio_wl32(s->pb, par->sample_rate * par->channels * 2);
  219. avio_wl16(s->pb, par->block_align);
  220. avio_wl16(s->pb, par->bits_per_coded_sample);
  221. avio_wl32(s->pb, APM_FILE_EXTRADATA_SIZE);
  222. /*
  223. * Build the extradata. Assume the codec's given us correct data.
  224. * File and data sizes are fixed later.
  225. */
  226. AV_WL32(buf + 0, APM_TAG_VS12); /* magic */
  227. AV_WL32(buf + 12, 0xFFFFFFFF); /* unk1 */
  228. memcpy( buf + 20, par->extradata, APM_EXTRADATA_SIZE);
  229. AV_WL32(buf + 76, APM_TAG_DATA); /* data */
  230. avio_write(s->pb, buf, APM_FILE_EXTRADATA_SIZE);
  231. return 0;
  232. }
  233. static int apm_write_trailer(AVFormatContext *s)
  234. {
  235. int64_t file_size, data_size;
  236. file_size = avio_tell(s->pb);
  237. data_size = file_size - (APM_FILE_HEADER_SIZE + 2 + APM_FILE_EXTRADATA_SIZE);
  238. if (file_size >= UINT32_MAX) {
  239. av_log(s, AV_LOG_ERROR,
  240. "Filesize %"PRId64" invalid for APM, output file will be broken\n",
  241. file_size);
  242. return AVERROR(ERANGE);
  243. }
  244. avio_seek(s->pb, 24, SEEK_SET);
  245. avio_wl32(s->pb, (uint32_t)file_size);
  246. avio_wl32(s->pb, (uint32_t)data_size);
  247. return 0;
  248. }
  249. AVOutputFormat ff_apm_muxer = {
  250. .name = "apm",
  251. .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
  252. .extensions = "apm",
  253. .audio_codec = AV_CODEC_ID_ADPCM_IMA_APM,
  254. .video_codec = AV_CODEC_ID_NONE,
  255. .init = apm_write_init,
  256. .write_header = apm_write_header,
  257. .write_packet = ff_raw_write_packet,
  258. .write_trailer = apm_write_trailer
  259. };
  260. #endif