FFmpeg
Loading...
Searching...
No Matches
alp.c
Go to the documentation of this file.
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
23#include "config_components.h"
24
26#include "avformat.h"
27#include "avio_internal.h"
28#include "demux.h"
29#include "internal.h"
30#include "mux.h"
31#include "rawenc.h"
33#include "libavutil/internal.h"
34#include "libavutil/opt.h"
35
36#define ALP_TAG MKTAG('A', 'L', 'P', ' ')
37#define ALP_MAX_READ_SIZE 4096
38
39typedef struct ALPHeader {
40 uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */
41 uint32_t header_size; /*< Header size (after this). */
42 char adpcm[6]; /*< "ADPCM" */
43 uint8_t unk1; /*< Unknown */
44 uint8_t num_channels; /*< Channel Count. */
45 uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */
46} ALPHeader;
47
48typedef enum ALPType {
49 ALP_TYPE_AUTO = 0, /*< Autodetect based on file extension. */
50 ALP_TYPE_TUN = 1, /*< Force a .TUN file. */
51 ALP_TYPE_PCM = 2, /*< Force a .PCM file. */
52} ALPType;
53
54typedef struct ALPMuxContext {
55 const AVClass *class;
58
59#if CONFIG_ALP_DEMUXER
60static int alp_probe(const AVProbeData *p)
61{
62 uint32_t i;
63
64 if (AV_RL32(p->buf) != ALP_TAG)
65 return 0;
66
67 /* Only allowed header sizes are 8 and 12. */
68 i = AV_RL32(p->buf + 4);
69 if (i != 8 && i != 12)
70 return 0;
71
72 if (strncmp("ADPCM", p->buf + 8, 6) != 0)
73 return 0;
74
75 return AVPROBE_SCORE_MAX - 1;
76}
77
78static int alp_read_header(AVFormatContext *s)
79{
80 int ret;
81 AVStream *st;
82 ALPHeader *hdr = s->priv_data;
84
85 if ((hdr->magic = avio_rl32(s->pb)) != ALP_TAG)
87
88 hdr->header_size = avio_rl32(s->pb);
89
90 if (hdr->header_size != 8 && hdr->header_size != 12) {
92 }
93
94 if ((ret = ffio_read_size(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
95 return ret;
96
97 if (strncmp("ADPCM", hdr->adpcm, sizeof(hdr->adpcm)) != 0)
99
100 hdr->unk1 = avio_r8(s->pb);
101 hdr->num_channels = avio_r8(s->pb);
102
103 if (hdr->header_size == 8) {
104 /* .TUN music file */
105 hdr->sample_rate = 22050;
106
107 } else {
108 /* .PCM sound file */
109 hdr->sample_rate = avio_rl32(s->pb);
110 }
111
112 if (hdr->sample_rate > 44100) {
113 avpriv_request_sample(s, "Sample Rate > 44100");
115 }
116
117 if (!(st = avformat_new_stream(s, NULL)))
118 return AVERROR(ENOMEM);
119
120 par = st->codecpar;
124 par->sample_rate = hdr->sample_rate;
125
126 if (hdr->num_channels > 2 || hdr->num_channels == 0)
127 return AVERROR_INVALIDDATA;
128
130 par->bits_per_coded_sample = 4;
131 par->block_align = 1;
132 par->bit_rate = par->ch_layout.nb_channels *
133 par->sample_rate *
135
136 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
137 return 0;
138}
139
140static 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->ch_layout.nb_channels;
151
152 return 0;
153}
154
155static 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
167 .p.name = "alp",
168 .p.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
179static int alp_write_init(AVFormatContext *s)
180{
181 ALPMuxContext *alp = s->priv_data;
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 par = s->streams[0]->codecpar;
192
193 if (par->ch_layout.nb_channels > 2) {
194 av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
195 return AVERROR(EINVAL);
196 }
197
198 if (par->sample_rate > 44100) {
199 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
200 return AVERROR(EINVAL);
201 }
202
203 if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) {
204 av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n");
205 return AVERROR(EINVAL);
206 }
207 return 0;
208}
209
210static int alp_write_header(AVFormatContext *s)
211{
212 ALPMuxContext *alp = s->priv_data;
213 AVCodecParameters *par = s->streams[0]->codecpar;
214
215 avio_wl32(s->pb, ALP_TAG);
216 avio_wl32(s->pb, alp->type == ALP_TYPE_PCM ? 12 : 8);
217 avio_write(s->pb, "ADPCM", 6);
218 avio_w8(s->pb, 0);
219 avio_w8(s->pb, par->ch_layout.nb_channels);
220 if (alp->type == ALP_TYPE_PCM)
221 avio_wl32(s->pb, par->sample_rate);
222
223 return 0;
224}
225
227
228static const AVOption alp_options[] = {
229 {
230 .name = "type",
231 .help = "set file type",
232 .offset = offsetof(ALPMuxContext, type),
233 .type = AV_OPT_TYPE_INT,
234 .default_val = {.i64 = ALP_TYPE_AUTO},
235 .min = ALP_TYPE_AUTO,
236 .max = ALP_TYPE_PCM,
237 .flags = AE,
238 .unit = "type",
239 },
240 {
241 .name = "auto",
242 .help = "autodetect based on file extension",
243 .offset = 0,
244 .type = AV_OPT_TYPE_CONST,
245 .default_val = {.i64 = ALP_TYPE_AUTO},
246 .min = 0,
247 .max = 0,
248 .flags = AE,
249 .unit = "type"
250 },
251 {
252 .name = "tun",
253 .help = "force .tun, used for music",
254 .offset = 0,
255 .type = AV_OPT_TYPE_CONST,
256 .default_val = {.i64 = ALP_TYPE_TUN},
257 .min = 0,
258 .max = 0,
259 .flags = AE,
260 .unit = "type"
261 },
262 {
263 .name = "pcm",
264 .help = "force .pcm, used for sfx",
265 .offset = 0,
266 .type = AV_OPT_TYPE_CONST,
267 .default_val = {.i64 = ALP_TYPE_PCM},
268 .min = 0,
269 .max = 0,
270 .flags = AE,
271 .unit = "type"
272 },
273 { NULL }
274};
275
276static const AVClass alp_muxer_class = {
277 .class_name = "alp",
278 .item_name = av_default_item_name,
279 .option = alp_options,
280 .version = LIBAVUTIL_VERSION_INT
281};
282
284 .p.name = "alp",
285 .p.long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
286 .p.extensions = "tun,pcm",
287 .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_ALP,
288 .p.video_codec = AV_CODEC_ID_NONE,
289 .p.subtitle_codec = AV_CODEC_ID_NONE,
290 .p.priv_class = &alp_muxer_class,
291 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
293 .init = alp_write_init,
294 .write_header = alp_write_header,
295 .write_packet = ff_raw_write_packet,
296 .priv_data_size = sizeof(ALPMuxContext)
297};
298#endif
#define AE
Definition alacenc.c:622
const FFOutputFormat ff_alp_muxer
const FFInputFormat ff_alp_demuxer
#define ALP_TAG
Definition alp.c:36
#define ALP_MAX_READ_SIZE
Definition alp.c:37
ALPType
Definition alp.c:48
@ ALP_TYPE_TUN
Definition alp.c:50
@ ALP_TYPE_PCM
Definition alp.c:51
@ ALP_TYPE_AUTO
Definition alp.c:49
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_AUDIO_PARAM
Definition opt.h:356
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition codec_id.h:416
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
cl_device_type type
#define AV_RL32(p)
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
AVOptions.
uint32_t magic
Definition alp.c:40
char adpcm[6]
Definition alp.c:42
uint32_t header_size
Definition alp.c:41
uint8_t unk1
Definition alp.c:43
uint32_t sample_rate
Definition alp.c:45
uint8_t num_channels
Definition alp.c:44
ALPType type
Definition alp.c:56
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define avpriv_request_sample(...)
#define av_log(a,...)
static int64_t pts