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 
23 #include "config_components.h"
24 
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "mux.h"
30 #include "rawenc.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/opt.h"
34 
35 #define ALP_TAG MKTAG('A', 'L', 'P', ' ')
36 #define ALP_MAX_READ_SIZE 4096
37 
38 typedef struct ALPHeader {
39  uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */
40  uint32_t header_size; /*< Header size (after this). */
41  char adpcm[6]; /*< "ADPCM" */
42  uint8_t unk1; /*< Unknown */
43  uint8_t num_channels; /*< Channel Count. */
44  uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */
45 } ALPHeader;
46 
47 typedef enum ALPType {
48  ALP_TYPE_AUTO = 0, /*< Autodetect based on file extension. */
49  ALP_TYPE_TUN = 1, /*< Force a .TUN file. */
50  ALP_TYPE_PCM = 2, /*< Force a .PCM file. */
51 } ALPType;
52 
53 typedef struct ALPMuxContext {
54  const AVClass *class;
57 
58 #if CONFIG_ALP_DEMUXER
59 static int alp_probe(const AVProbeData *p)
60 {
61  uint32_t i;
62 
63  if (AV_RL32(p->buf) != ALP_TAG)
64  return 0;
65 
66  /* Only allowed header sizes are 8 and 12. */
67  i = AV_RL32(p->buf + 4);
68  if (i != 8 && i != 12)
69  return 0;
70 
71  if (strncmp("ADPCM", p->buf + 8, 6) != 0)
72  return 0;
73 
74  return AVPROBE_SCORE_MAX - 1;
75 }
76 
77 static int alp_read_header(AVFormatContext *s)
78 {
79  int ret;
80  AVStream *st;
81  ALPHeader *hdr = s->priv_data;
82  AVCodecParameters *par;
83 
84  if ((hdr->magic = avio_rl32(s->pb)) != ALP_TAG)
85  return AVERROR_INVALIDDATA;
86 
87  hdr->header_size = avio_rl32(s->pb);
88 
89  if (hdr->header_size != 8 && hdr->header_size != 12) {
90  return AVERROR_INVALIDDATA;
91  }
92 
93  if ((ret = avio_read(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
94  return ret;
95  else if (ret != sizeof(hdr->adpcm))
96  return AVERROR(EIO);
97 
98  if (strncmp("ADPCM", hdr->adpcm, sizeof(hdr->adpcm)) != 0)
99  return AVERROR_INVALIDDATA;
100 
101  hdr->unk1 = avio_r8(s->pb);
102  hdr->num_channels = avio_r8(s->pb);
103 
104  if (hdr->header_size == 8) {
105  /* .TUN music file */
106  hdr->sample_rate = 22050;
107 
108  } else {
109  /* .PCM sound file */
110  hdr->sample_rate = avio_rl32(s->pb);
111  }
112 
113  if (hdr->sample_rate > 44100) {
114  avpriv_request_sample(s, "Sample Rate > 44100");
115  return AVERROR_PATCHWELCOME;
116  }
117 
118  if (!(st = avformat_new_stream(s, NULL)))
119  return AVERROR(ENOMEM);
120 
121  par = st->codecpar;
124  par->format = AV_SAMPLE_FMT_S16;
125  par->sample_rate = hdr->sample_rate;
126 
127  if (hdr->num_channels > 2 || hdr->num_channels == 0)
128  return AVERROR_INVALIDDATA;
129 
131  par->bits_per_coded_sample = 4;
132  par->block_align = 1;
133  par->bit_rate = par->ch_layout.nb_channels *
134  par->sample_rate *
136 
137  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
138  return 0;
139 }
140 
141 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
142 {
143  int ret;
144  AVCodecParameters *par = s->streams[0]->codecpar;
145 
146  if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
147  return ret;
148 
150  pkt->stream_index = 0;
151  pkt->duration = ret * 2 / par->ch_layout.nb_channels;
152 
153  return 0;
154 }
155 
156 static int alp_seek(AVFormatContext *s, int stream_index,
157  int64_t pts, int flags)
158 {
159  const ALPHeader *hdr = s->priv_data;
160 
161  if (pts != 0)
162  return AVERROR(EINVAL);
163 
164  return avio_seek(s->pb, hdr->header_size + 8, SEEK_SET);
165 }
166 
168  .p.name = "alp",
169  .p.long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
170  .priv_data_size = sizeof(ALPHeader),
171  .read_probe = alp_probe,
172  .read_header = alp_read_header,
173  .read_packet = alp_read_packet,
174  .read_seek = alp_seek,
175 };
176 #endif
177 
178 #if CONFIG_ALP_MUXER
179 
180 static int alp_write_init(AVFormatContext *s)
181 {
182  ALPMuxContext *alp = s->priv_data;
183  AVCodecParameters *par;
184 
185  if (alp->type == ALP_TYPE_AUTO) {
186  if (av_match_ext(s->url, "pcm"))
187  alp->type = ALP_TYPE_PCM;
188  else
189  alp->type = ALP_TYPE_TUN;
190  }
191 
192  par = s->streams[0]->codecpar;
193 
194  if (par->ch_layout.nb_channels > 2) {
195  av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
196  return AVERROR(EINVAL);
197  }
198 
199  if (par->sample_rate > 44100) {
200  av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
201  return AVERROR(EINVAL);
202  }
203 
204  if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) {
205  av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n");
206  return AVERROR(EINVAL);
207  }
208  return 0;
209 }
210 
211 static int alp_write_header(AVFormatContext *s)
212 {
213  ALPMuxContext *alp = s->priv_data;
214  AVCodecParameters *par = s->streams[0]->codecpar;
215 
216  avio_wl32(s->pb, ALP_TAG);
217  avio_wl32(s->pb, alp->type == ALP_TYPE_PCM ? 12 : 8);
218  avio_write(s->pb, "ADPCM", 6);
219  avio_w8(s->pb, 0);
220  avio_w8(s->pb, par->ch_layout.nb_channels);
221  if (alp->type == ALP_TYPE_PCM)
222  avio_wl32(s->pb, par->sample_rate);
223 
224  return 0;
225 }
226 
228 
229 static const AVOption alp_options[] = {
230  {
231  .name = "type",
232  .help = "set file type",
233  .offset = offsetof(ALPMuxContext, type),
234  .type = AV_OPT_TYPE_INT,
235  .default_val = {.i64 = ALP_TYPE_AUTO},
236  .min = ALP_TYPE_AUTO,
237  .max = ALP_TYPE_PCM,
238  .flags = AE,
239  .unit = "type",
240  },
241  {
242  .name = "auto",
243  .help = "autodetect based on file extension",
244  .offset = 0,
245  .type = AV_OPT_TYPE_CONST,
246  .default_val = {.i64 = ALP_TYPE_AUTO},
247  .min = 0,
248  .max = 0,
249  .flags = AE,
250  .unit = "type"
251  },
252  {
253  .name = "tun",
254  .help = "force .tun, used for music",
255  .offset = 0,
256  .type = AV_OPT_TYPE_CONST,
257  .default_val = {.i64 = ALP_TYPE_TUN},
258  .min = 0,
259  .max = 0,
260  .flags = AE,
261  .unit = "type"
262  },
263  {
264  .name = "pcm",
265  .help = "force .pcm, used for sfx",
266  .offset = 0,
267  .type = AV_OPT_TYPE_CONST,
268  .default_val = {.i64 = ALP_TYPE_PCM},
269  .min = 0,
270  .max = 0,
271  .flags = AE,
272  .unit = "type"
273  },
274  { NULL }
275 };
276 
277 static const AVClass alp_muxer_class = {
278  .class_name = "alp",
279  .item_name = av_default_item_name,
280  .option = alp_options,
281  .version = LIBAVUTIL_VERSION_INT
282 };
283 
285  .p.name = "alp",
286  .p.long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
287  .p.extensions = "tun,pcm",
288  .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_ALP,
289  .p.video_codec = AV_CODEC_ID_NONE,
290  .p.subtitle_codec = AV_CODEC_ID_NONE,
291  .p.priv_class = &alp_muxer_class,
292  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
294  .init = alp_write_init,
295  .write_header = alp_write_header,
296  .write_packet = ff_raw_write_packet,
297  .priv_data_size = sizeof(ALPMuxContext)
298 };
299 #endif
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
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
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
ALPType
ALPType
Definition: alp.c:47
ALPHeader::magic
uint32_t magic
Definition: alp.c:39
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int64_t
long long int64_t
Definition: coverity.c:34
AVOption
AVOption.
Definition: opt.h:429
ALPHeader::sample_rate
uint32_t sample_rate
Definition: alp.c:44
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#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
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:862
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
ALPMuxContext
Definition: alp.c:53
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pts
static int64_t pts
Definition: transcode_aac.c:644
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:357
ff_alp_demuxer
const FFInputFormat ff_alp_demuxer
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:595
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
ALP_TYPE_AUTO
@ ALP_TYPE_AUTO
Definition: alp.c:48
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
ALPHeader::adpcm
char adpcm[6]
Definition: alp.c:41
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_match_ext
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
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:61
ff_alp_muxer
const FFOutputFormat ff_alp_muxer
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
AE
#define AE
Definition: alacenc.c:622
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:352
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
ALPHeader
Definition: alp.c:38
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition: codec_id.h:418
AVOption::name
const char * name
Definition: opt.h:430
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:357
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
ALPHeader::header_size
uint32_t header_size
Definition: alp.c:40
ALP_TYPE_TUN
@ ALP_TYPE_TUN
Definition: alp.c:49
rawenc.h
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:834
ALP_TYPE_PCM
@ ALP_TYPE_PCM
Definition: alp.c:50
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#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
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
demux.h
av_get_packet
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
AVClass::class_name
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:80
avformat.h
ALP_MAX_READ_SIZE
#define ALP_MAX_READ_SIZE
Definition: alp.c:36
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
channel_layout.h
ALPMuxContext::type
ALPType type
Definition: alp.c:55
ALPHeader::num_channels
uint8_t num_channels
Definition: alp.c:43
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
ALPHeader::unk1
uint8_t unk1
Definition: alp.c:42
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVCodecParameters::format
int format
Definition: codec_par.h:92
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFInputFormat
Definition: demux.h:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ALP_TAG
#define ALP_TAG
Definition: alp.c:35
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
mux.h