FFmpeg
alp.c
Go to the documentation of this file.
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 
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 
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;
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)
109  else if (hdr.num_channels == 2)
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 *
120 
121  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
122  return 0;
123 }
124 
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 
134  pkt->stream_index = 0;
135  pkt->duration = ret * 2 / par->channels;
136 
137  return 0;
138 }
139 
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 };
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
ALPHeader::magic
uint32_t magic
Definition: alp.c:31
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
ff_alp_demuxer
AVInputFormat ff_alp_demuxer
Definition: alp.c:140
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
alp_read_header
static int alp_read_header(AVFormatContext *s)
Definition: alp.c:57
ALPHeader::sample_rate
uint32_t sample_rate
Definition: alp.c:36
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:373
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AVCodecParameters::bits_per_raw_sample
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: codec_par.h:115
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
AVInputFormat
Definition: avformat.h:636
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:389
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
ALPHeader::adpcm
char adpcm[6]
Definition: alp.c:33
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:641
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVFormatContext
Format I/O context.
Definition: avformat.h:1335
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1012
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
alp_read_packet
static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: alp.c:125
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
ALPHeader
Definition: alp.c:30
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:747
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:188
AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition: codec_id.h:387
avpriv_set_pts_info
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:4948
alp_probe
static int alp_probe(const AVProbeData *p)
Definition: alp.c:39
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:616
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
ALPHeader::header_size
uint32_t header_size
Definition: alp.c:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:177
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
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:307
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:865
avformat.h
ALP_MAX_READ_SIZE
#define ALP_MAX_READ_SIZE
Definition: alp.c:28
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
ALPHeader::num_channels
uint8_t num_channels
Definition: alp.c:35
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:625
AVPacket::stream_index
int stream_index
Definition: packet.h:357
ALPHeader::unk1
uint8_t unk1
Definition: alp.c:34
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ALP_TAG
#define ALP_TAG
Definition: alp.c:27