FFmpeg
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 #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;
51 
52 #if CONFIG_ALP_DEMUXER
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 
71 static int alp_read_header(AVFormatContext *s)
72 {
73  int ret;
74  AVStream *st;
75  ALPHeader hdr;
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  } else {
102  /* .PCM sound file */
103  hdr.sample_rate = avio_rl32(s->pb);
104  }
105 
106  if (hdr.sample_rate > 44100) {
107  avpriv_request_sample(s, "Sample Rate > 44100");
108  return AVERROR_PATCHWELCOME;
109  }
110 
111  if (!(st = avformat_new_stream(s, NULL)))
112  return AVERROR(ENOMEM);
113 
114  par = st->codecpar;
117  par->format = AV_SAMPLE_FMT_S16;
118  par->sample_rate = hdr.sample_rate;
119  par->channels = hdr.num_channels;
120 
121  if (hdr.num_channels == 1)
123  else if (hdr.num_channels == 2)
125  else
126  return AVERROR_INVALIDDATA;
127 
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 *
132  par->sample_rate *
134 
135  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
136  return 0;
137 }
138 
139 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
140 {
141  int ret;
142  AVCodecParameters *par = s->streams[0]->codecpar;
143 
144  if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
145  return ret;
146 
147  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
148  pkt->stream_index = 0;
149  pkt->duration = ret * 2 / par->channels;
150 
151  return 0;
152 }
153 
155  .name = "alp",
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
160 };
161 #endif
162 
163 #if CONFIG_ALP_MUXER
164 
165 static int alp_write_init(AVFormatContext *s)
166 {
167  ALPMuxContext *alp = s->priv_data;
168  AVCodecParameters *par;
169 
170  if (alp->type == ALP_TYPE_AUTO) {
171  if (av_match_ext(s->url, "pcm"))
172  alp->type = ALP_TYPE_PCM;
173  else
174  alp->type = ALP_TYPE_TUN;
175  }
176 
177  if (s->nb_streams != 1) {
178  av_log(s, AV_LOG_ERROR, "Too many streams\n");
179  return AVERROR(EINVAL);
180  }
181 
182  par = s->streams[0]->codecpar;
183 
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);
188  }
189 
190  if (par->channels > 2) {
191  av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
192  return AVERROR(EINVAL);
193  }
194 
195  if (par->sample_rate > 44100) {
196  av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
197  return AVERROR(EINVAL);
198  }
199 
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);
203  }
204  return 0;
205 }
206 
207 static int alp_write_header(AVFormatContext *s)
208 {
209  ALPMuxContext *alp = s->priv_data;
210  AVCodecParameters *par = s->streams[0]->codecpar;
211 
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);
215  avio_w8(s->pb, 0);
216  avio_w8(s->pb, par->channels);
217  if (alp->type == ALP_TYPE_PCM)
218  avio_wl32(s->pb, par->sample_rate);
219 
220  return 0;
221 }
222 
224 
225 static const AVOption alp_options[] = {
226  {
227  .name = "type",
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,
233  .max = ALP_TYPE_PCM,
234  .flags = AE,
235  .unit = "type",
236  },
237  {
238  .name = "auto",
239  .help = "autodetect based on file extension",
240  .offset = 0,
241  .type = AV_OPT_TYPE_CONST,
242  .default_val = {.i64 = ALP_TYPE_AUTO},
243  .min = 0,
244  .max = 0,
245  .flags = AE,
246  .unit = "type"
247  },
248  {
249  .name = "tun",
250  .help = "force .tun, used for music",
251  .offset = 0,
252  .type = AV_OPT_TYPE_CONST,
253  .default_val = {.i64 = ALP_TYPE_TUN},
254  .min = 0,
255  .max = 0,
256  .flags = AE,
257  .unit = "type"
258  },
259  {
260  .name = "pcm",
261  .help = "force .pcm, used for sfx",
262  .offset = 0,
263  .type = AV_OPT_TYPE_CONST,
264  .default_val = {.i64 = ALP_TYPE_PCM},
265  .min = 0,
266  .max = 0,
267  .flags = AE,
268  .unit = "type"
269  },
270  { NULL }
271 };
272 
273 static const AVClass alp_muxer_class = {
274  .class_name = "alp",
275  .item_name = av_default_item_name,
276  .option = alp_options,
277  .version = LIBAVUTIL_VERSION_INT
278 };
279 
281  .name = "alp",
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)
291 };
292 #endif
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ALPType type
Definition: alp.c:49
AVOption.
Definition: opt.h:248
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4871
#define avpriv_request_sample(...)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:280
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
GLint GLenum type
Definition: opengl_enc.c:104
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
Format I/O context.
Definition: avformat.h:1239
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
const char * name
Definition: opt.h:249
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:379
uint8_t
AVOptions.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:381
#define ALP_MAX_READ_SIZE
Definition: alp.c:30
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4453
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1307
uint32_t header_size
Definition: alp.c:34
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:305
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:38
uint8_t unk1
Definition: alp.c:36
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:278
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:752
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
char * url
input or output URL.
Definition: avformat.h:1335
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
Definition: alp.c:32
#define AE
Definition: alacenc.c:645
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:369
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
uint8_t num_channels
Definition: alp.c:37
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1295
int block_align
Audio only.
Definition: codec_par.h:177
uint32_t magic
Definition: alp.c:33
const char * name
Definition: avformat.h:500
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
Stream structure.
Definition: avformat.h:880
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVIOContext * pb
I/O context.
Definition: avformat.h:1281
ALPType
Definition: alp.c:41
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:203
uint32_t sample_rate
Definition: alp.c:38
Describe the class of an AVClass context structure.
Definition: log.h:67
char adpcm[6]
Definition: alp.c:35
AVInputFormat ff_alp_demuxer
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: codec_par.h:115
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
signed 16 bits
Definition: samplefmt.h:61
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:1200
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:402
AVOutputFormat ff_alp_muxer
#define ALP_TAG
Definition: alp.c:29
void * priv_data
Format private data.
Definition: avformat.h:1267
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1045
int stream_index
Definition: packet.h:365
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
#define AV_CH_LAYOUT_MONO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
This structure stores compressed data.
Definition: packet.h:340
int i
Definition: input.c:407