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