FFmpeg
apm.c
Go to the documentation of this file.
1 /*
2  * Rayman 2 APM 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 "riff.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 
28 #define APM_FILE_HEADER_SIZE 20
29 #define APM_VS12_CHUNK_SIZE 76
30 #define APM_MAX_READ_SIZE 4096
31 
32 #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
33 #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
34 
35 typedef struct APMState {
43 } APMState;
44 
45 typedef struct APMVS12Chunk {
46  uint32_t magic;
47  uint32_t file_size;
48  uint32_t data_size;
49  uint32_t unk1;
50  uint32_t unk2;
52  uint32_t pad[7];
53 } APMVS12Chunk;
54 
55 static void apm_parse_vs12(APMVS12Chunk *vs12, const uint8_t *buf)
56 {
57  vs12->magic = AV_RL32(buf + 0);
58  vs12->file_size = AV_RL32(buf + 4);
59  vs12->data_size = AV_RL32(buf + 8);
60  vs12->unk1 = AV_RL32(buf + 12);
61  vs12->unk2 = AV_RL32(buf + 16);
62 
63  vs12->state.has_saved = AV_RL32(buf + 20);
64  vs12->state.predictor_r = AV_RL32(buf + 24);
65  vs12->state.step_index_r = AV_RL32(buf + 28);
66  vs12->state.saved_r = AV_RL32(buf + 32);
67  vs12->state.predictor_l = AV_RL32(buf + 36);
68  vs12->state.step_index_l = AV_RL32(buf + 40);
69  vs12->state.saved_l = AV_RL32(buf + 44);
70 
71  for (int i = 0; i < FF_ARRAY_ELEMS(vs12->pad); i++)
72  vs12->pad[i] = AV_RL32(buf + 48 + (i * 4));
73 }
74 
75 static int apm_probe(const AVProbeData *p)
76 {
77  if (p->buf_size < 100)
78  return 0;
79 
80  if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
81  return 0;
82 
83  if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
84  return 0;
85 
86  return AVPROBE_SCORE_MAX - 1;
87 }
88 
90 {
91  int64_t ret;
92  AVStream *st;
93  APMVS12Chunk vs12;
95 
96  if (!(st = avformat_new_stream(s, NULL)))
97  return AVERROR(ENOMEM);
98 
99  /* The header starts with a WAVEFORMATEX */
100  if ((ret = ff_get_wav_header(s, s->pb, st->codecpar, APM_FILE_HEADER_SIZE, 0)) < 0)
101  return ret;
102 
103  if (st->codecpar->bits_per_coded_sample != 4)
104  return AVERROR_INVALIDDATA;
105 
106  if (st->codecpar->codec_tag != 0x2000)
107  return AVERROR_INVALIDDATA;
108 
109  /* ff_get_wav_header() does most of the work, but we need to fix a few things. */
111  st->codecpar->codec_tag = 0;
112 
113  if (st->codecpar->channels == 2)
115  else if (st->codecpar->channels == 1)
117  else
118  return AVERROR_INVALIDDATA;
119 
121  st->codecpar->bits_per_raw_sample = 16;
122  st->codecpar->bit_rate = st->codecpar->channels *
123  st->codecpar->sample_rate *
125 
126  if ((ret = avio_read(s->pb, buf, APM_VS12_CHUNK_SIZE)) < 0)
127  return ret;
128  else if (ret != APM_VS12_CHUNK_SIZE)
129  return AVERROR(EIO);
130 
131  apm_parse_vs12(&vs12, buf);
132 
133  if (vs12.magic != APM_TAG_VS12) {
134  return AVERROR_INVALIDDATA;
135  }
136 
137  if (vs12.state.has_saved) {
138  avpriv_request_sample(s, "Saved Samples");
139  return AVERROR_PATCHWELCOME;
140  }
141 
142  if (avio_rl32(s->pb) != APM_TAG_DATA)
143  return AVERROR_INVALIDDATA;
144 
145  if ((ret = ff_alloc_extradata(st->codecpar, 16)) < 0)
146  return ret;
147 
148  AV_WL32(st->codecpar->extradata + 0, vs12.state.predictor_l);
149  AV_WL32(st->codecpar->extradata + 4, vs12.state.step_index_l);
150  AV_WL32(st->codecpar->extradata + 8, vs12.state.predictor_r);
151  AV_WL32(st->codecpar->extradata + 12, vs12.state.step_index_r);
152 
153  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
154  st->start_time = 0;
155  st->duration = vs12.data_size *
156  (8 / st->codecpar->bits_per_coded_sample) /
157  st->codecpar->channels;
158  return 0;
159 }
160 
162 {
163  int ret;
164  AVCodecParameters *par = s->streams[0]->codecpar;
165 
166  /*
167  * For future reference: if files with the `has_saved` field set ever
168  * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
169  * that should be sent to the decoder before the actual data.
170  */
171 
172  if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
173  return ret;
174 
176  pkt->stream_index = 0;
177  pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
178 
179  return 0;
180 }
181 
183  .name = "apm",
184  .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
185  .read_probe = apm_probe,
186  .read_header = apm_read_header,
187  .read_packet = apm_read_packet
188 };
ff_apm_demuxer
AVInputFormat ff_apm_demuxer
Definition: apm.c:182
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
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
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
APMState::predictor_r
int32_t predictor_r
Definition: apm.c:37
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
APMState::saved_l
int32_t saved_l
Definition: apm.c:42
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:373
APMVS12Chunk::data_size
uint32_t data_size
Definition: apm.c:48
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
apm_read_packet
static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: apm.c:161
apm_probe
static int apm_probe(const AVProbeData *p)
Definition: apm.c:75
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
APMState
Definition: apm.c:35
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
APMVS12Chunk::pad
uint32_t pad[7]
Definition: apm.c:52
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
APMVS12Chunk::state
APMState state
Definition: apm.c:51
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:914
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
APM_FILE_HEADER_SIZE
#define APM_FILE_HEADER_SIZE
Definition: apm.c:28
AVInputFormat
Definition: avformat.h:636
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:389
APMState::step_index_r
int32_t step_index_r
Definition: apm.c:38
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
APMState::step_index_l
int32_t step_index_l
Definition: apm.c:41
APMVS12Chunk::magic
uint32_t magic
Definition: apm.c:46
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
apm_parse_vs12
static void apm_parse_vs12(APMVS12Chunk *vs12, const uint8_t *buf)
Definition: apm.c:55
APM_VS12_CHUNK_SIZE
#define APM_VS12_CHUNK_SIZE
Definition: apm.c:29
int32_t
int32_t
Definition: audio_convert.c:194
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
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
APMVS12Chunk::unk1
uint32_t unk1
Definition: apm.c:49
APM_MAX_READ_SIZE
#define APM_MAX_READ_SIZE
Definition: apm.c:30
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
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
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
APM_TAG_DATA
#define APM_TAG_DATA
Definition: apm.c:33
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
APMState::saved_r
int32_t saved_r
Definition: apm.c:39
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
APMVS12Chunk
Definition: apm.c:45
internal.h
APM_TAG_VS12
#define APM_TAG_VS12
Definition: apm.c:32
AV_CODEC_ID_ADPCM_IMA_APM
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition: codec_id.h:386
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
ff_get_wav_header
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
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
apm_read_header
static int apm_read_header(AVFormatContext *s)
Definition: apm.c:89
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:865
APMVS12Chunk::file_size
uint32_t file_size
Definition: apm.c:47
avformat.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
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
APMState::has_saved
int32_t has_saved
Definition: apm.c:36
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
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
riff.h
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
APMVS12Chunk::unk2
uint32_t unk2
Definition: apm.c:50
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:904
APMState::predictor_l
int32_t predictor_l
Definition: apm.c:40
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:3328