4 * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/internal.h"
25 #include "libavutil/intreadwrite.h"
27 #define APM_FILE_HEADER_SIZE 18
28 #define APM_FILE_EXTRADATA_SIZE 80
29 #define APM_EXTRADATA_SIZE 28
31 #define APM_MAX_READ_SIZE 4096
33 #define APM_TAG_CODEC 0x2000
34 #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
35 #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
37 typedef struct APMState {
47 typedef struct APMExtraData {
58 static void apm_parse_extradata(APMExtraData *ext, const uint8_t *buf)
60 ext->magic = AV_RL32(buf + 0);
61 ext->file_size = AV_RL32(buf + 4);
62 ext->data_size = AV_RL32(buf + 8);
63 ext->unk1 = AV_RL32(buf + 12);
64 ext->unk2 = AV_RL32(buf + 16);
66 ext->state.has_saved = AV_RL32(buf + 20);
67 ext->state.predictor_r = AV_RL32(buf + 24);
68 ext->state.step_index_r = AV_RL32(buf + 28);
69 ext->state.saved_r = AV_RL32(buf + 32);
70 ext->state.predictor_l = AV_RL32(buf + 36);
71 ext->state.step_index_l = AV_RL32(buf + 40);
72 ext->state.saved_l = AV_RL32(buf + 44);
74 for (int i = 0; i < FF_ARRAY_ELEMS(ext->unk3); i++)
75 ext->unk3[i] = AV_RL32(buf + 48 + (i * 4));
77 ext->data = AV_RL32(buf + 76);
80 static int apm_probe(const AVProbeData *p)
82 if (AV_RL16(p->buf) != APM_TAG_CODEC)
85 if (p->buf_size < 100)
88 if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
91 if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
94 return AVPROBE_SCORE_MAX - 1;
97 static int apm_read_header(AVFormatContext *s)
101 APMExtraData extradata;
102 AVCodecParameters *par;
103 uint8_t buf[APM_FILE_EXTRADATA_SIZE];
105 if (!(st = avformat_new_stream(s, NULL)))
106 return AVERROR(ENOMEM);
109 * This is 98% a WAVEFORMATEX, but there's something screwy with the extradata
110 * that ff_get_wav_header() can't (and shouldn't) handle properly.
112 if (avio_rl16(s->pb) != APM_TAG_CODEC)
113 return AVERROR_INVALIDDATA;
116 par->channels = avio_rl16(s->pb);
117 par->sample_rate = avio_rl32(s->pb);
119 /* Skip the bitrate, it's usually wrong anyway. */
120 if ((ret = avio_skip(s->pb, 4)) < 0)
123 par->block_align = avio_rl16(s->pb);
124 par->bits_per_coded_sample = avio_rl16(s->pb);
126 if (avio_rl32(s->pb) != APM_FILE_EXTRADATA_SIZE)
127 return AVERROR_INVALIDDATA;
129 /* 8 = bits per sample * max channels */
130 if (par->sample_rate > (INT_MAX / 8))
131 return AVERROR_INVALIDDATA;
133 if (par->bits_per_coded_sample != 4)
134 return AVERROR_INVALIDDATA;
136 if (par->channels == 2)
137 par->channel_layout = AV_CH_LAYOUT_STEREO;
138 else if (par->channels == 1)
139 par->channel_layout = AV_CH_LAYOUT_MONO;
141 return AVERROR_INVALIDDATA;
143 par->codec_type = AVMEDIA_TYPE_AUDIO;
144 par->codec_id = AV_CODEC_ID_ADPCM_IMA_APM;
145 par->format = AV_SAMPLE_FMT_S16;
146 par->bits_per_raw_sample = 16;
147 par->bit_rate = par->channels *
149 par->bits_per_coded_sample;
151 if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
153 else if (ret != APM_FILE_EXTRADATA_SIZE)
156 apm_parse_extradata(&extradata, buf);
158 if (extradata.magic != APM_TAG_VS12 || extradata.data != APM_TAG_DATA)
159 return AVERROR_INVALIDDATA;
161 if (extradata.state.has_saved) {
162 avpriv_request_sample(s, "Saved Samples");
163 return AVERROR_PATCHWELCOME;
166 if ((ret = ff_alloc_extradata(par, APM_EXTRADATA_SIZE)) < 0)
169 /* Use the entire state as extradata. */
170 memcpy(par->extradata, buf + 20, APM_EXTRADATA_SIZE);
172 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
174 st->duration = extradata.data_size *
175 (8 / par->bits_per_coded_sample) /
180 static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
183 AVCodecParameters *par = s->streams[0]->codecpar;
186 * For future reference: if files with the `has_saved` field set ever
187 * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
188 * that should be sent to the decoder before the actual data.
191 if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
194 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
195 pkt->stream_index = 0;
196 pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
201 AVInputFormat ff_apm_demuxer = {
203 .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
204 .read_probe = apm_probe,
205 .read_header = apm_read_header,
206 .read_packet = apm_read_packet