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
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
28 #define APM_FILE_HEADER_SIZE 20
29 #define APM_VS12_CHUNK_SIZE 76
30 #define APM_MAX_READ_SIZE 4096
32 #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
33 #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
35 typedef struct APMState {
45 typedef struct APMVS12Chunk {
55 static void apm_parse_vs12(APMVS12Chunk *vs12, const uint8_t *buf)
57 vs12->magic = AV_RL32(buf + 0);
58 vs12->file_size = AV_RL32(buf + 4);
59 vs12->data_size = AV_RL32(buf + 8);
60 vs12->unk1 = AV_RL32(buf + 12);
61 vs12->unk2 = AV_RL32(buf + 16);
63 vs12->state.has_saved = AV_RL32(buf + 20);
64 vs12->state.predictor_r = AV_RL32(buf + 24);
65 vs12->state.step_index_r = AV_RL32(buf + 28);
66 vs12->state.saved_r = AV_RL32(buf + 32);
67 vs12->state.predictor_l = AV_RL32(buf + 36);
68 vs12->state.step_index_l = AV_RL32(buf + 40);
69 vs12->state.saved_l = AV_RL32(buf + 44);
71 for (int i = 0; i < FF_ARRAY_ELEMS(vs12->pad); i++)
72 vs12->pad[i] = AV_RL32(buf + 48 + (i * 4));
75 static int apm_probe(const AVProbeData *p)
77 if (p->buf_size < 100)
80 if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
83 if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
86 return AVPROBE_SCORE_MAX - 1;
89 static int apm_read_header(AVFormatContext *s)
94 uint8_t buf[APM_VS12_CHUNK_SIZE];
96 if (!(st = avformat_new_stream(s, NULL)))
97 return AVERROR(ENOMEM);
99 /* The header starts with a WAVEFORMATEX */
100 if ((ret = ff_get_wav_header(s, s->pb, st->codecpar, APM_FILE_HEADER_SIZE, 0)) < 0)
103 if (st->codecpar->bits_per_coded_sample != 4)
104 return AVERROR_INVALIDDATA;
106 if (st->codecpar->codec_tag != 0x2000)
107 return AVERROR_INVALIDDATA;
109 /* ff_get_wav_header() does most of the work, but we need to fix a few things. */
110 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_APM;
111 st->codecpar->codec_tag = 0;
113 if (st->codecpar->channels == 2)
114 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
115 else if (st->codecpar->channels == 1)
116 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
118 return AVERROR_INVALIDDATA;
120 st->codecpar->format = AV_SAMPLE_FMT_S16;
121 st->codecpar->bits_per_raw_sample = 16;
122 st->codecpar->bit_rate = st->codecpar->channels *
123 st->codecpar->sample_rate *
124 st->codecpar->bits_per_coded_sample;
126 if ((ret = avio_read(s->pb, buf, APM_VS12_CHUNK_SIZE)) < 0)
128 else if (ret != APM_VS12_CHUNK_SIZE)
131 apm_parse_vs12(&vs12, buf);
133 if (vs12.magic != APM_TAG_VS12) {
134 return AVERROR_INVALIDDATA;
137 if (vs12.state.has_saved) {
138 avpriv_request_sample(s, "Saved Samples");
139 return AVERROR_PATCHWELCOME;
142 if (avio_rl32(s->pb) != APM_TAG_DATA)
143 return AVERROR_INVALIDDATA;
145 if ((ret = ff_alloc_extradata(st->codecpar, 16)) < 0)
148 AV_WL32(st->codecpar->extradata + 0, vs12.state.predictor_l);
149 AV_WL32(st->codecpar->extradata + 4, vs12.state.step_index_l);
150 AV_WL32(st->codecpar->extradata + 8, vs12.state.predictor_r);
151 AV_WL32(st->codecpar->extradata + 12, vs12.state.step_index_r);
153 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
155 st->duration = vs12.data_size *
156 (8 / st->codecpar->bits_per_coded_sample) /
157 st->codecpar->channels;
161 static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
164 AVCodecParameters *par = s->streams[0]->codecpar;
167 * For future reference: if files with the `has_saved` field set ever
168 * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
169 * that should be sent to the decoder before the actual data.
172 if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
175 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
176 pkt->stream_index = 0;
177 pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
182 AVInputFormat ff_apm_demuxer = {
184 .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
185 .read_probe = apm_probe,
186 .read_header = apm_read_header,
187 .read_packet = apm_read_packet