1 /*
2 * LEGO Racers ALP (.tun & .pcm) demuxer
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 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/internal.h"
26
27 #define ALP_TAG MKTAG('A', 'L', 'P', ' ')
28 #define ALP_MAX_READ_SIZE 4096
29
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. */
37 } ALPHeader;
38
alp_probe(const AVProbeData * p)39 static int alp_probe(const AVProbeData *p)
40 {
41 uint32_t i;
42
43 if (AV_RL32(p->buf) != ALP_TAG)
44 return 0;
45
46 /* Only allowed header sizes are 8 and 12. */
47 i = AV_RL32(p->buf + 4);
48 if (i != 8 && i != 12)
49 return 0;
50
51 if (strncmp("ADPCM", p->buf + 8, 6) != 0)
52 return 0;
53
54 return AVPROBE_SCORE_MAX - 1;
55 }
56
alp_read_header(AVFormatContext * s)57 static int alp_read_header(AVFormatContext *s)
58 {
59 int ret;
60 AVStream *st;
61 ALPHeader hdr;
62 AVCodecParameters *par;
63
64 if ((hdr.magic = avio_rl32(s->pb)) != ALP_TAG)
65 return AVERROR_INVALIDDATA;
66
67 hdr.header_size = avio_rl32(s->pb);
68
69 if (hdr.header_size != 8 && hdr.header_size != 12) {
70 return AVERROR_INVALIDDATA;
71 }
72
73 if ((ret = avio_read(s->pb, hdr.adpcm, sizeof(hdr.adpcm))) < 0)
74 return ret;
75 else if (ret != sizeof(hdr.adpcm))
76 return AVERROR(EIO);
77
78 if (strncmp("ADPCM", hdr.adpcm, sizeof(hdr.adpcm)) != 0)
79 return AVERROR_INVALIDDATA;
80
81 hdr.unk1 = avio_r8(s->pb);
82 hdr.num_channels = avio_r8(s->pb);
83
84 if (hdr.header_size == 8) {
85 /* .TUN music file */
86 hdr.sample_rate = 22050;
87 } else {
88 /* .PCM sound file */
89 hdr.sample_rate = avio_rl32(s->pb);
90 }
91
92 if (hdr.sample_rate > 44100) {
93 avpriv_request_sample(s, "Sample Rate > 44100");
94 return AVERROR_PATCHWELCOME;
95 }
96
97 if (!(st = avformat_new_stream(s, NULL)))
98 return AVERROR(ENOMEM);
99
100 par = st->codecpar;
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;
106
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;
111 else
112 return AVERROR_INVALIDDATA;
113
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 *
118 par->sample_rate *
119 par->bits_per_coded_sample;
120
121 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
122 return 0;
123 }
124
alp_read_packet(AVFormatContext * s,AVPacket * pkt)125 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
126 {
127 int ret;
128 AVCodecParameters *par = s->streams[0]->codecpar;
129
130 if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
131 return ret;
132
133 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
134 pkt->stream_index = 0;
135 pkt->duration = ret * 2 / par->channels;
136
137 return 0;
138 }
139
140 AVInputFormat ff_alp_demuxer = {
141 .name = "alp",
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
146 };
147