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