FFmpeg
apm.c
Go to the documentation of this file.
1 /*
2  * Rayman 2 APM (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 
25 #include "avformat.h"
26 #include "demux.h"
27 #include "internal.h"
28 #include "mux.h"
29 #include "rawenc.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/intreadwrite.h"
33 
34 #define APM_FILE_HEADER_SIZE 20
35 #define APM_FILE_EXTRADATA_SIZE 80
36 #define APM_EXTRADATA_SIZE 28
37 
38 #define APM_MAX_READ_SIZE 4096
39 
40 #define APM_TAG_CODEC 0x2000
41 #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
42 #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
43 
44 typedef struct APMState {
52 } APMState;
53 
54 typedef struct APMExtraData {
55  uint32_t magic;
56  uint32_t file_size;
57  uint32_t data_size;
58  uint32_t unk1;
59  uint32_t unk2;
61  uint32_t unk3[7];
62  uint32_t data;
63 } APMExtraData;
64 
65 #if CONFIG_APM_DEMUXER
66 static void apm_parse_extradata(APMExtraData *ext, const uint8_t *buf)
67 {
68  ext->magic = AV_RL32(buf + 0);
69  ext->file_size = AV_RL32(buf + 4);
70  ext->data_size = AV_RL32(buf + 8);
71  ext->unk1 = AV_RL32(buf + 12);
72  ext->unk2 = AV_RL32(buf + 16);
73 
74  ext->state.has_saved = AV_RL32(buf + 20);
75  ext->state.predictor_r = AV_RL32(buf + 24);
76  ext->state.step_index_r = AV_RL32(buf + 28);
77  ext->state.saved_r = AV_RL32(buf + 32);
78  ext->state.predictor_l = AV_RL32(buf + 36);
79  ext->state.step_index_l = AV_RL32(buf + 40);
80  ext->state.saved_l = AV_RL32(buf + 44);
81 
82  for (int i = 0; i < FF_ARRAY_ELEMS(ext->unk3); i++)
83  ext->unk3[i] = AV_RL32(buf + 48 + (i * 4));
84 
85  ext->data = AV_RL32(buf + 76);
86 }
87 
88 static int apm_probe(const AVProbeData *p)
89 {
90  if (AV_RL16(p->buf) != APM_TAG_CODEC)
91  return 0;
92 
93  if (p->buf_size < 100)
94  return 0;
95 
96  if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
97  return 0;
98 
99  if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
100  return 0;
101 
102  return AVPROBE_SCORE_MAX - 1;
103 }
104 
105 static int apm_read_header(AVFormatContext *s)
106 {
107  int64_t ret;
108  AVStream *st;
109  APMExtraData extradata;
110  AVCodecParameters *par;
111  uint8_t buf[APM_FILE_EXTRADATA_SIZE];
112  int channels;
113 
114  if (!(st = avformat_new_stream(s, NULL)))
115  return AVERROR(ENOMEM);
116 
117  /*
118  * This is 98% a WAVEFORMATEX, but there's something screwy with the extradata
119  * that ff_get_wav_header() can't (and shouldn't) handle properly.
120  */
121  if (avio_rl16(s->pb) != APM_TAG_CODEC)
122  return AVERROR_INVALIDDATA;
123 
124  par = st->codecpar;
125  channels = avio_rl16(s->pb);
126  par->sample_rate = avio_rl32(s->pb);
127 
128  /* Skip the bitrate, it's usually wrong anyway. */
129  if ((ret = avio_skip(s->pb, 4)) < 0)
130  return ret;
131 
132  par->block_align = avio_rl16(s->pb);
133  par->bits_per_coded_sample = avio_rl16(s->pb);
134 
135  if (avio_rl32(s->pb) != APM_FILE_EXTRADATA_SIZE)
136  return AVERROR_INVALIDDATA;
137 
138  /* 8 = bits per sample * max channels */
139  if (par->sample_rate > (INT_MAX / 8))
140  return AVERROR_INVALIDDATA;
141 
142  if (par->bits_per_coded_sample != 4)
143  return AVERROR_INVALIDDATA;
144 
145  if (channels > 2 || channels == 0)
146  return AVERROR_INVALIDDATA;
147 
151  par->format = AV_SAMPLE_FMT_S16;
152  par->bit_rate = par->ch_layout.nb_channels *
153  (int64_t)par->sample_rate *
155 
156  if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
157  return ret;
158  else if (ret != APM_FILE_EXTRADATA_SIZE)
159  return AVERROR(EIO);
160 
161  apm_parse_extradata(&extradata, buf);
162 
163  if (extradata.magic != APM_TAG_VS12 || extradata.data != APM_TAG_DATA)
164  return AVERROR_INVALIDDATA;
165 
166  if (extradata.state.has_saved) {
167  avpriv_request_sample(s, "Saved Samples");
168  return AVERROR_PATCHWELCOME;
169  }
170 
171  if ((ret = ff_alloc_extradata(par, APM_EXTRADATA_SIZE)) < 0)
172  return ret;
173 
174  /* Use the entire state as extradata. */
175  memcpy(par->extradata, buf + 20, APM_EXTRADATA_SIZE);
176 
177  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
178  st->start_time = 0;
179  st->duration = extradata.data_size *
180  (8 / par->bits_per_coded_sample) /
181  par->ch_layout.nb_channels;
182  return 0;
183 }
184 
185 static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
186 {
187  int ret;
188  AVCodecParameters *par = s->streams[0]->codecpar;
189 
190  /*
191  * For future reference: if files with the `has_saved` field set ever
192  * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
193  * that should be sent to the decoder before the actual data.
194  */
195 
196  if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
197  return ret;
198 
200  pkt->stream_index = 0;
202 
203  return 0;
204 }
205 
207  .p.name = "apm",
208  .p.long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
209  .read_probe = apm_probe,
210  .read_header = apm_read_header,
211  .read_packet = apm_read_packet
212 };
213 #endif
214 
215 #if CONFIG_APM_MUXER
216 static int apm_write_init(AVFormatContext *s)
217 {
218  AVCodecParameters *par = s->streams[0]->codecpar;
219 
220  if (par->ch_layout.nb_channels > 2) {
221  av_log(s, AV_LOG_ERROR, "APM files only support up to 2 channels\n");
222  return AVERROR(EINVAL);
223  }
224 
225  if (par->sample_rate > (INT_MAX / 8)) {
226  av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
227  return AVERROR(EINVAL);
228  }
229 
230  if (par->extradata_size != APM_EXTRADATA_SIZE) {
231  av_log(s, AV_LOG_ERROR, "Invalid/missing extradata\n");
232  return AVERROR(EINVAL);
233  }
234 
235  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
236  av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
237  return AVERROR(EINVAL);
238  }
239 
240  return 0;
241 }
242 
243 static int apm_write_header(AVFormatContext *s)
244 {
245  uint8_t buf[APM_FILE_EXTRADATA_SIZE] = { 0 };
246  AVCodecParameters *par = s->streams[0]->codecpar;
247 
248  /*
249  * Bodge a WAVEFORMATEX manually, ff_put_wav_header() can't
250  * be used because of the extra 2 bytes.
251  */
252  avio_wl16(s->pb, APM_TAG_CODEC);
253  avio_wl16(s->pb, par->ch_layout.nb_channels);
254  avio_wl32(s->pb, par->sample_rate);
255  /* This is the wrong calculation, but it's what the orginal files have. */
256  avio_wl32(s->pb, par->sample_rate * par->ch_layout.nb_channels * 2);
257  avio_wl16(s->pb, par->block_align);
258  avio_wl16(s->pb, par->bits_per_coded_sample);
260 
261  /*
262  * Build the extradata. Assume the codec's given us correct data.
263  * File and data sizes are fixed later.
264  */
265  AV_WL32(buf + 0, APM_TAG_VS12); /* magic */
266  AV_WL32(buf + 12, 0xFFFFFFFF); /* unk1 */
267  memcpy( buf + 20, par->extradata, APM_EXTRADATA_SIZE);
268  AV_WL32(buf + 76, APM_TAG_DATA); /* data */
269 
271  return 0;
272 }
273 
274 static int apm_write_trailer(AVFormatContext *s)
275 {
276  int64_t file_size, data_size;
277 
278  file_size = avio_tell(s->pb);
279  data_size = file_size - (APM_FILE_HEADER_SIZE + APM_FILE_EXTRADATA_SIZE);
280 
281  if (file_size >= UINT32_MAX) {
283  "Filesize %"PRId64" invalid for APM, output file will be broken\n",
284  file_size);
285  return AVERROR(ERANGE);
286  }
287 
288  avio_seek(s->pb, 24, SEEK_SET);
289  avio_wl32(s->pb, (uint32_t)file_size);
290  avio_wl32(s->pb, (uint32_t)data_size);
291 
292  return 0;
293 }
294 
296  .p.name = "apm",
297  .p.long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
298  .p.extensions = "apm",
299  .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_APM,
300  .p.video_codec = AV_CODEC_ID_NONE,
301  .p.subtitle_codec = AV_CODEC_ID_NONE,
302  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
304  .init = apm_write_init,
305  .write_header = apm_write_header,
306  .write_packet = ff_raw_write_packet,
307  .write_trailer = apm_write_trailer
308 };
309 #endif
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
APMExtraData::unk3
uint32_t unk3[7]
Definition: apm.c:61
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
APMState::predictor_r
int32_t predictor_r
Definition: apm.c:46
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
APMExtraData::state
APMState state
Definition: apm.c:60
APMState::saved_l
int32_t saved_l
Definition: apm.c:51
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
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:313
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:436
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
APMState
Definition: apm.c:44
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:853
APMExtraData::data_size
uint32_t data_size
Definition: apm.c:57
APMExtraData
Definition: apm.c:54
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:713
APM_FILE_HEADER_SIZE
#define APM_FILE_HEADER_SIZE
Definition: apm.c:34
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:578
APMExtraData::data
uint32_t data
Definition: apm.c:62
APMState::step_index_r
int32_t step_index_r
Definition: apm.c:47
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_apm_muxer
const FFOutputFormat ff_apm_muxer
APMState::step_index_l
int32_t step_index_l
Definition: apm.c:50
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
channels
channels
Definition: aptx.h:31
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:61
APM_MAX_READ_SIZE
#define APM_MAX_READ_SIZE
Definition: apm.c:38
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
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:106
APM_TAG_DATA
#define APM_TAG_DATA
Definition: apm.c:42
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:200
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:356
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
rawenc.h
APMState::saved_r
int32_t saved_r
Definition: apm.c:48
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:830
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:255
internal.h
APM_TAG_CODEC
#define APM_TAG_CODEC
Definition: apm.c:40
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
APM_TAG_VS12
#define APM_TAG_VS12
Definition: apm.c:41
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_CODEC_ID_ADPCM_IMA_APM
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition: codec_id.h:412
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
demux.h
ff_apm_demuxer
const FFInputFormat ff_apm_demuxer
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
APMExtraData::file_size
uint32_t file_size
Definition: apm.c:56
channel_layout.h
APMExtraData::unk2
uint32_t unk2
Definition: apm.c:59
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
APMState::has_saved
int32_t has_saved
Definition: apm.c:45
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
APM_EXTRADATA_SIZE
#define APM_EXTRADATA_SIZE
Definition: apm.c:36
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
APM_FILE_EXTRADATA_SIZE
#define APM_FILE_EXTRADATA_SIZE
Definition: apm.c:35
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVCodecParameters::format
int format
Definition: codec_par.h:92
APMExtraData::unk1
uint32_t unk1
Definition: apm.c:58
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:499
FFInputFormat
Definition: demux.h:37
int32_t
int32_t
Definition: audioconvert.c:56
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
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:792
APMExtraData::magic
uint32_t magic
Definition: apm.c:55
APMState::predictor_l
int32_t predictor_l
Definition: apm.c:49
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:239
mux.h