• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * LEGO Racers ALP (.tun & .pcm) (de)muxer
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 "rawenc.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 
29 #define ALP_TAG            MKTAG('A', 'L', 'P', ' ')
30 #define ALP_MAX_READ_SIZE  4096
31 
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. */
39 } ALPHeader;
40 
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. */
45 } ALPType;
46 
47 typedef struct ALPMuxContext {
48     const AVClass *class;
49     ALPType type;
50 } ALPMuxContext;
51 
52 #if CONFIG_ALP_DEMUXER
alp_probe(const AVProbeData * p)53 static int alp_probe(const AVProbeData *p)
54 {
55     uint32_t i;
56 
57     if (AV_RL32(p->buf) != ALP_TAG)
58         return 0;
59 
60     /* Only allowed header sizes are 8 and 12. */
61     i = AV_RL32(p->buf + 4);
62     if (i != 8 && i != 12)
63         return 0;
64 
65     if (strncmp("ADPCM", p->buf + 8, 6) != 0)
66         return 0;
67 
68     return AVPROBE_SCORE_MAX - 1;
69 }
70 
alp_read_header(AVFormatContext * s)71 static int alp_read_header(AVFormatContext *s)
72 {
73     int ret;
74     AVStream *st;
75     ALPHeader *hdr = s->priv_data;
76     AVCodecParameters *par;
77 
78     if ((hdr->magic = avio_rl32(s->pb)) != ALP_TAG)
79         return AVERROR_INVALIDDATA;
80 
81     hdr->header_size = avio_rl32(s->pb);
82 
83     if (hdr->header_size != 8 && hdr->header_size != 12) {
84         return AVERROR_INVALIDDATA;
85     }
86 
87     if ((ret = avio_read(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
88         return ret;
89     else if (ret != sizeof(hdr->adpcm))
90         return AVERROR(EIO);
91 
92     if (strncmp("ADPCM", hdr->adpcm, sizeof(hdr->adpcm)) != 0)
93         return AVERROR_INVALIDDATA;
94 
95     hdr->unk1                   = avio_r8(s->pb);
96     hdr->num_channels           = avio_r8(s->pb);
97 
98     if (hdr->header_size == 8) {
99         /* .TUN music file */
100         hdr->sample_rate        = 22050;
101 
102     } else {
103         /* .PCM sound file */
104         hdr->sample_rate        = avio_rl32(s->pb);
105     }
106 
107     if (hdr->sample_rate > 44100) {
108         avpriv_request_sample(s, "Sample Rate > 44100");
109         return AVERROR_PATCHWELCOME;
110     }
111 
112     if (!(st = avformat_new_stream(s, NULL)))
113         return AVERROR(ENOMEM);
114 
115     par                         = st->codecpar;
116     par->codec_type             = AVMEDIA_TYPE_AUDIO;
117     par->codec_id               = AV_CODEC_ID_ADPCM_IMA_ALP;
118     par->format                 = AV_SAMPLE_FMT_S16;
119     par->sample_rate            = hdr->sample_rate;
120     par->channels               = hdr->num_channels;
121 
122     if (hdr->num_channels == 1)
123         par->channel_layout     = AV_CH_LAYOUT_MONO;
124     else if (hdr->num_channels == 2)
125         par->channel_layout     = AV_CH_LAYOUT_STEREO;
126     else
127         return AVERROR_INVALIDDATA;
128 
129     par->bits_per_coded_sample  = 4;
130     par->bits_per_raw_sample    = 16;
131     par->block_align            = 1;
132     par->bit_rate               = par->channels *
133                                   par->sample_rate *
134                                   par->bits_per_coded_sample;
135 
136     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
137     return 0;
138 }
139 
alp_read_packet(AVFormatContext * s,AVPacket * pkt)140 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
141 {
142     int ret;
143     AVCodecParameters *par = s->streams[0]->codecpar;
144 
145     if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
146         return ret;
147 
148     pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
149     pkt->stream_index   = 0;
150     pkt->duration       = ret * 2 / par->channels;
151 
152     return 0;
153 }
154 
alp_seek(AVFormatContext * s,int stream_index,int64_t pts,int flags)155 static int alp_seek(AVFormatContext *s, int stream_index,
156                      int64_t pts, int flags)
157 {
158     const ALPHeader *hdr = s->priv_data;
159 
160     if (pts != 0)
161         return AVERROR(EINVAL);
162 
163     return avio_seek(s->pb, hdr->header_size + 8, SEEK_SET);
164 }
165 
166 AVInputFormat ff_alp_demuxer = {
167     .name           = "alp",
168     .long_name      = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
169     .priv_data_size = sizeof(ALPHeader),
170     .read_probe     = alp_probe,
171     .read_header    = alp_read_header,
172     .read_packet    = alp_read_packet,
173     .read_seek      = alp_seek,
174 };
175 #endif
176 
177 #if CONFIG_ALP_MUXER
178 
alp_write_init(AVFormatContext * s)179 static int alp_write_init(AVFormatContext *s)
180 {
181     ALPMuxContext *alp = s->priv_data;
182     AVCodecParameters *par;
183 
184     if (alp->type == ALP_TYPE_AUTO) {
185         if (av_match_ext(s->url, "pcm"))
186             alp->type = ALP_TYPE_PCM;
187         else
188             alp->type = ALP_TYPE_TUN;
189     }
190 
191     if (s->nb_streams != 1) {
192         av_log(s, AV_LOG_ERROR, "Too many streams\n");
193         return AVERROR(EINVAL);
194     }
195 
196     par = s->streams[0]->codecpar;
197 
198     if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_ALP) {
199         av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
200                avcodec_get_name(par->codec_id));
201         return AVERROR(EINVAL);
202     }
203 
204     if (par->channels > 2) {
205         av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
206         return AVERROR(EINVAL);
207     }
208 
209     if (par->sample_rate > 44100) {
210         av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
211         return AVERROR(EINVAL);
212     }
213 
214     if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) {
215         av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n");
216         return AVERROR(EINVAL);
217     }
218     return 0;
219 }
220 
alp_write_header(AVFormatContext * s)221 static int alp_write_header(AVFormatContext *s)
222 {
223     ALPMuxContext *alp = s->priv_data;
224     AVCodecParameters *par = s->streams[0]->codecpar;
225 
226     avio_wl32(s->pb,  ALP_TAG);
227     avio_wl32(s->pb,  alp->type == ALP_TYPE_PCM ? 12 : 8);
228     avio_write(s->pb, "ADPCM", 6);
229     avio_w8(s->pb,    0);
230     avio_w8(s->pb,    par->channels);
231     if (alp->type == ALP_TYPE_PCM)
232         avio_wl32(s->pb, par->sample_rate);
233 
234     return 0;
235 }
236 
237 enum { AE = AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM };
238 
239 static const AVOption alp_options[] = {
240     {
241         .name        = "type",
242         .help        = "set file type",
243         .offset      = offsetof(ALPMuxContext, type),
244         .type        = AV_OPT_TYPE_INT,
245         .default_val = {.i64 = ALP_TYPE_AUTO},
246         .min         = ALP_TYPE_AUTO,
247         .max         = ALP_TYPE_PCM,
248         .flags       = AE,
249         .unit        = "type",
250     },
251     {
252         .name        = "auto",
253         .help        = "autodetect based on file extension",
254         .offset      = 0,
255         .type        = AV_OPT_TYPE_CONST,
256         .default_val = {.i64 = ALP_TYPE_AUTO},
257         .min         = 0,
258         .max         = 0,
259         .flags       = AE,
260         .unit        = "type"
261     },
262     {
263         .name        = "tun",
264         .help        = "force .tun, used for music",
265         .offset      = 0,
266         .type        = AV_OPT_TYPE_CONST,
267         .default_val = {.i64 = ALP_TYPE_TUN},
268         .min         = 0,
269         .max         = 0,
270         .flags       = AE,
271         .unit        = "type"
272     },
273     {
274         .name        = "pcm",
275         .help        = "force .pcm, used for sfx",
276         .offset      = 0,
277         .type        = AV_OPT_TYPE_CONST,
278         .default_val = {.i64 = ALP_TYPE_PCM},
279         .min         = 0,
280         .max         = 0,
281         .flags       = AE,
282         .unit        = "type"
283     },
284     { NULL }
285 };
286 
287 static const AVClass alp_muxer_class = {
288     .class_name = "alp",
289     .item_name  = av_default_item_name,
290     .option     = alp_options,
291     .version    = LIBAVUTIL_VERSION_INT
292 };
293 
294 AVOutputFormat ff_alp_muxer = {
295     .name           = "alp",
296     .long_name      = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
297     .extensions     = "tun,pcm",
298     .audio_codec    = AV_CODEC_ID_ADPCM_IMA_ALP,
299     .video_codec    = AV_CODEC_ID_NONE,
300     .init           = alp_write_init,
301     .write_header   = alp_write_header,
302     .write_packet   = ff_raw_write_packet,
303     .priv_class     = &alp_muxer_class,
304     .priv_data_size = sizeof(ALPMuxContext)
305 };
306 #endif
307