2 * LEGO Racers ALP (.tun & .pcm) demuxer
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/intreadwrite.h"
25 #include "libavutil/internal.h"
27 #define ALP_TAG MKTAG('A', 'L', 'P', ' ')
28 #define ALP_MAX_READ_SIZE 4096
30 typedef struct ALPHeader {
31 uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */
32 uint32_t header_size; /*< Header size (after this). */
33 char adpcm[6]; /*< "ADPCM" */
34 uint8_t unk1; /*< Unknown */
35 uint8_t num_channels; /*< Channel Count. */
36 uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */
39 static int alp_probe(const AVProbeData *p)
43 if (AV_RL32(p->buf) != ALP_TAG)
46 /* Only allowed header sizes are 8 and 12. */
47 i = AV_RL32(p->buf + 4);
48 if (i != 8 && i != 12)
51 if (strncmp("ADPCM", p->buf + 8, 6) != 0)
54 return AVPROBE_SCORE_EXTENSION + 1;
57 static int alp_read_header(AVFormatContext *s)
62 AVCodecParameters *par;
64 if ((hdr.magic = avio_rl32(s->pb)) != ALP_TAG)
65 return AVERROR_INVALIDDATA;
67 hdr.header_size = avio_rl32(s->pb);
69 if (hdr.header_size != 8 && hdr.header_size != 12) {
70 return AVERROR_INVALIDDATA;
73 if ((ret = avio_read(s->pb, hdr.adpcm, sizeof(hdr.adpcm))) < 0)
75 else if (ret != sizeof(hdr.adpcm))
78 if (strncmp("ADPCM", hdr.adpcm, sizeof(hdr.adpcm)) != 0)
79 return AVERROR_INVALIDDATA;
81 hdr.unk1 = avio_r8(s->pb);
82 hdr.num_channels = avio_r8(s->pb);
84 if (hdr.header_size == 8) {
86 hdr.sample_rate = 11025 * hdr.num_channels;
89 hdr.sample_rate = avio_rl32(s->pb);
92 if (hdr.sample_rate > 44100) {
93 avpriv_request_sample(s, "Sample Rate > 44100");
94 return AVERROR_PATCHWELCOME;
97 if (!(st = avformat_new_stream(s, NULL)))
98 return AVERROR(ENOMEM);
101 par->codec_type = AVMEDIA_TYPE_AUDIO;
102 par->codec_id = AV_CODEC_ID_ADPCM_IMA_ALP;
103 par->format = AV_SAMPLE_FMT_S16;
104 par->sample_rate = hdr.sample_rate;
105 par->channels = hdr.num_channels;
107 if (hdr.num_channels == 1)
108 par->channel_layout = AV_CH_LAYOUT_MONO;
109 else if (hdr.num_channels == 2)
110 par->channel_layout = AV_CH_LAYOUT_STEREO;
112 return AVERROR_INVALIDDATA;
114 par->bits_per_coded_sample = 4;
115 par->bits_per_raw_sample = 16;
116 par->block_align = 1;
117 par->bit_rate = par->channels *
119 par->bits_per_coded_sample;
121 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
125 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
128 AVCodecParameters *par = s->streams[0]->codecpar;
130 if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
133 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
134 pkt->stream_index = 0;
135 pkt->duration = ret * 2 / par->channels;
140 AVInputFormat ff_alp_demuxer = {
142 .long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
143 .read_probe = alp_probe,
144 .read_header = alp_read_header,
145 .read_packet = alp_read_packet