2 * LEGO Racers ALP (.tun & .pcm) (de)muxer
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/intreadwrite.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
29 #define ALP_TAG MKTAG('A', 'L', 'P', ' ')
30 #define ALP_MAX_READ_SIZE 4096
32 typedef struct ALPHeader {
33 uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */
34 uint32_t header_size; /*< Header size (after this). */
35 char adpcm[6]; /*< "ADPCM" */
36 uint8_t unk1; /*< Unknown */
37 uint8_t num_channels; /*< Channel Count. */
38 uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */
41 typedef enum ALPType {
42 ALP_TYPE_AUTO = 0, /*< Autodetect based on file extension. */
43 ALP_TYPE_TUN = 1, /*< Force a .TUN file. */
44 ALP_TYPE_PCM = 2, /*< Force a .PCM file. */
47 typedef struct ALPMuxContext {
52 #if CONFIG_ALP_DEMUXER
53 static int alp_probe(const AVProbeData *p)
57 if (AV_RL32(p->buf) != ALP_TAG)
60 /* Only allowed header sizes are 8 and 12. */
61 i = AV_RL32(p->buf + 4);
62 if (i != 8 && i != 12)
65 if (strncmp("ADPCM", p->buf + 8, 6) != 0)
68 return AVPROBE_SCORE_MAX - 1;
71 static int alp_read_header(AVFormatContext *s)
76 AVCodecParameters *par;
78 if ((hdr.magic = avio_rl32(s->pb)) != ALP_TAG)
79 return AVERROR_INVALIDDATA;
81 hdr.header_size = avio_rl32(s->pb);
83 if (hdr.header_size != 8 && hdr.header_size != 12) {
84 return AVERROR_INVALIDDATA;
87 if ((ret = avio_read(s->pb, hdr.adpcm, sizeof(hdr.adpcm))) < 0)
89 else if (ret != sizeof(hdr.adpcm))
92 if (strncmp("ADPCM", hdr.adpcm, sizeof(hdr.adpcm)) != 0)
93 return AVERROR_INVALIDDATA;
95 hdr.unk1 = avio_r8(s->pb);
96 hdr.num_channels = avio_r8(s->pb);
98 if (hdr.header_size == 8) {
100 hdr.sample_rate = 22050;
102 /* .PCM sound file */
103 hdr.sample_rate = avio_rl32(s->pb);
106 if (hdr.sample_rate > 44100) {
107 avpriv_request_sample(s, "Sample Rate > 44100");
108 return AVERROR_PATCHWELCOME;
111 if (!(st = avformat_new_stream(s, NULL)))
112 return AVERROR(ENOMEM);
115 par->codec_type = AVMEDIA_TYPE_AUDIO;
116 par->codec_id = AV_CODEC_ID_ADPCM_IMA_ALP;
117 par->format = AV_SAMPLE_FMT_S16;
118 par->sample_rate = hdr.sample_rate;
119 par->channels = hdr.num_channels;
121 if (hdr.num_channels == 1)
122 par->channel_layout = AV_CH_LAYOUT_MONO;
123 else if (hdr.num_channels == 2)
124 par->channel_layout = AV_CH_LAYOUT_STEREO;
126 return AVERROR_INVALIDDATA;
128 par->bits_per_coded_sample = 4;
129 par->bits_per_raw_sample = 16;
130 par->block_align = 1;
131 par->bit_rate = par->channels *
133 par->bits_per_coded_sample;
135 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
139 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
142 AVCodecParameters *par = s->streams[0]->codecpar;
144 if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
147 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
148 pkt->stream_index = 0;
149 pkt->duration = ret * 2 / par->channels;
154 AVInputFormat ff_alp_demuxer = {
156 .long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
157 .read_probe = alp_probe,
158 .read_header = alp_read_header,
159 .read_packet = alp_read_packet
165 static int alp_write_init(AVFormatContext *s)
167 ALPMuxContext *alp = s->priv_data;
168 AVCodecParameters *par;
170 if (alp->type == ALP_TYPE_AUTO) {
171 if (av_match_ext(s->url, "pcm"))
172 alp->type = ALP_TYPE_PCM;
174 alp->type = ALP_TYPE_TUN;
177 if (s->nb_streams != 1) {
178 av_log(s, AV_LOG_ERROR, "Too many streams\n");
179 return AVERROR(EINVAL);
182 par = s->streams[0]->codecpar;
184 if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_ALP) {
185 av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
186 avcodec_get_name(par->codec_id));
187 return AVERROR(EINVAL);
190 if (par->channels > 2) {
191 av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
192 return AVERROR(EINVAL);
195 if (par->sample_rate > 44100) {
196 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
197 return AVERROR(EINVAL);
200 if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) {
201 av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n");
202 return AVERROR(EINVAL);
207 static int alp_write_header(AVFormatContext *s)
209 ALPMuxContext *alp = s->priv_data;
210 AVCodecParameters *par = s->streams[0]->codecpar;
212 avio_wl32(s->pb, ALP_TAG);
213 avio_wl32(s->pb, alp->type == ALP_TYPE_PCM ? 12 : 8);
214 avio_write(s->pb, "ADPCM", 6);
216 avio_w8(s->pb, par->channels);
217 if (alp->type == ALP_TYPE_PCM)
218 avio_wl32(s->pb, par->sample_rate);
223 enum { AE = AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM };
225 static const AVOption alp_options[] = {
228 .help = "set file type",
229 .offset = offsetof(ALPMuxContext, type),
230 .type = AV_OPT_TYPE_INT,
231 .default_val = {.i64 = ALP_TYPE_AUTO},
232 .min = ALP_TYPE_AUTO,
239 .help = "autodetect based on file extension",
241 .type = AV_OPT_TYPE_CONST,
242 .default_val = {.i64 = ALP_TYPE_AUTO},
250 .help = "force .tun, used for music",
252 .type = AV_OPT_TYPE_CONST,
253 .default_val = {.i64 = ALP_TYPE_TUN},
261 .help = "force .pcm, used for sfx",
263 .type = AV_OPT_TYPE_CONST,
264 .default_val = {.i64 = ALP_TYPE_PCM},
273 static const AVClass alp_muxer_class = {
275 .item_name = av_default_item_name,
276 .option = alp_options,
277 .version = LIBAVUTIL_VERSION_INT
280 AVOutputFormat ff_alp_muxer = {
282 .long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
283 .extensions = "tun,pcm",
284 .audio_codec = AV_CODEC_ID_ADPCM_IMA_ALP,
285 .video_codec = AV_CODEC_ID_NONE,
286 .init = alp_write_init,
287 .write_header = alp_write_header,
288 .write_packet = ff_raw_write_packet,
289 .priv_class = &alp_muxer_class,
290 .priv_data_size = sizeof(ALPMuxContext)