FFmpeg
pp_bnk.c
Go to the documentation of this file.
1 /*
2  * Pro Pinball Series Soundbank (44c, 22c, 11c, 5c) 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/avassert.h"
26 #include "libavutil/internal.h"
27 
28 #define PP_BNK_MAX_READ_SIZE 4096
29 #define PP_BNK_FILE_HEADER_SIZE 20
30 #define PP_BNK_TRACK_SIZE 20
31 
32 typedef struct PPBnkHeader {
33  uint32_t bank_id; /*< Bank ID, useless for our purposes. */
34  uint32_t sample_rate; /*< Sample rate of the contained tracks. */
35  uint32_t always1; /*< Unknown, always seems to be 1. */
36  uint32_t track_count; /*< Number of tracks in the file. */
37  uint32_t flags; /*< Flags. */
38 } PPBnkHeader;
39 
40 typedef struct PPBnkTrack {
41  uint32_t id; /*< Track ID. Usually track[i].id == track[i-1].id + 1, but not always */
42  uint32_t size; /*< Size of the data in bytes. */
43  uint32_t sample_rate; /*< Sample rate. */
44  uint32_t always1_1; /*< Unknown, always seems to be 1. */
45  uint32_t always1_2; /*< Unknown, always seems to be 1. */
46 } PPBnkTrack;
47 
48 typedef struct PPBnkCtxTrack {
49  int64_t data_offset;
50  uint32_t data_size;
51  uint32_t bytes_read;
53 
54 typedef struct PPBnkCtx {
57  uint32_t current_track;
58  int is_music;
59 } PPBnkCtx;
60 
61 enum {
62  PP_BNK_FLAG_PERSIST = (1 << 0), /*< This is a large file, keep in memory. */
63  PP_BNK_FLAG_MUSIC = (1 << 1), /*< This is music. */
65 };
66 
67 static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
68 {
69  hdr->bank_id = AV_RL32(buf + 0);
70  hdr->sample_rate = AV_RL32(buf + 4);
71  hdr->always1 = AV_RL32(buf + 8);
72  hdr->track_count = AV_RL32(buf + 12);
73  hdr->flags = AV_RL32(buf + 16);
74 }
75 
76 static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
77 {
78  trk->id = AV_RL32(buf + 0);
79  trk->size = AV_RL32(buf + 4);
80  trk->sample_rate = AV_RL32(buf + 8);
81  trk->always1_1 = AV_RL32(buf + 12);
82  trk->always1_2 = AV_RL32(buf + 16);
83 }
84 
85 static int pp_bnk_probe(const AVProbeData *p)
86 {
87  uint32_t sample_rate = AV_RL32(p->buf + 4);
88  uint32_t track_count = AV_RL32(p->buf + 12);
89  uint32_t flags = AV_RL32(p->buf + 16);
90 
91  if (track_count == 0 || track_count > INT_MAX)
92  return 0;
93 
94  if ((sample_rate != 5512) && (sample_rate != 11025) &&
95  (sample_rate != 22050) && (sample_rate != 44100))
96  return 0;
97 
98  /* Check the first track header. */
99  if (AV_RL32(p->buf + 28) != sample_rate)
100  return 0;
101 
102  if ((flags & ~PP_BNK_FLAG_MASK) != 0)
103  return 0;
104 
105  return AVPROBE_SCORE_MAX / 4 + 1;
106 }
107 
109 {
110  int64_t ret;
111  AVStream *st;
112  AVCodecParameters *par;
113  PPBnkCtx *ctx = s->priv_data;
115  PPBnkHeader hdr;
116 
117  if ((ret = avio_read(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
118  return ret;
119  else if (ret != PP_BNK_FILE_HEADER_SIZE)
120  return AVERROR(EIO);
121 
122  pp_bnk_parse_header(&hdr, buf);
123 
124  if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
125  return AVERROR_INVALIDDATA;
126 
127  if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
128  return AVERROR_INVALIDDATA;
129 
130  if (hdr.always1 != 1) {
131  avpriv_request_sample(s, "Non-one header value");
132  return AVERROR_PATCHWELCOME;
133  }
134 
135  ctx->track_count = hdr.track_count;
136 
137  if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
138  return AVERROR(ENOMEM);
139 
140  /* Parse and validate each track. */
141  for (int i = 0; i < hdr.track_count; i++) {
142  PPBnkTrack e;
143  PPBnkCtxTrack *trk = ctx->tracks + i;
144 
145  ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
146  if (ret < 0 && ret != AVERROR_EOF)
147  goto fail;
148 
149  /* Short byte-count or EOF, we have a truncated file. */
150  if (ret != PP_BNK_TRACK_SIZE) {
151  av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
152  i, hdr.track_count);
153  ctx->track_count = i;
154  break;
155  }
156 
157  pp_bnk_parse_track(&e, buf);
158 
159  /* The individual sample rates of all tracks must match that of the file header. */
160  if (e.sample_rate != hdr.sample_rate) {
162  goto fail;
163  }
164 
165  if (e.always1_1 != 1 || e.always1_2 != 1) {
166  avpriv_request_sample(s, "Non-one track header values");
168  goto fail;
169  }
170 
171  trk->data_offset = avio_tell(s->pb);
172  trk->data_size = e.size;
173  trk->bytes_read = 0;
174 
175  /*
176  * Skip over the data to the next stream header.
177  * Sometimes avio_skip() doesn't detect EOF. If it doesn't, either:
178  * - the avio_read() above will, or
179  * - pp_bnk_read_packet() will read a truncated last track.
180  */
181  if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
182  ctx->track_count = i + 1;
184  "Track %d has truncated data, assuming track count == %d\n",
185  i, ctx->track_count);
186  break;
187  } else if (ret < 0) {
188  goto fail;
189  }
190  }
191 
192  /* File is only a header. */
193  if (ctx->track_count == 0) {
195  goto fail;
196  }
197 
198  ctx->is_music = (hdr.flags & PP_BNK_FLAG_MUSIC) &&
199  (ctx->track_count == 2) &&
200  (ctx->tracks[0].data_size == ctx->tracks[1].data_size);
201 
202  /* Build the streams. */
203  for (int i = 0; i < (ctx->is_music ? 1 : ctx->track_count); i++) {
204  if (!(st = avformat_new_stream(s, NULL))) {
205  ret = AVERROR(ENOMEM);
206  goto fail;
207  }
208 
209  par = st->codecpar;
212  par->format = AV_SAMPLE_FMT_S16P;
213 
214  if (ctx->is_music) {
216  par->channels = 2;
217  } else {
219  par->channels = 1;
220  }
221 
222  par->sample_rate = hdr.sample_rate;
223  par->bits_per_coded_sample = 4;
224  par->bits_per_raw_sample = 16;
225  par->block_align = 1;
226  par->bit_rate = par->sample_rate * (int64_t)par->bits_per_coded_sample * par->channels;
227 
228  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
229  st->start_time = 0;
230  st->duration = ctx->tracks[i].data_size * 2;
231  }
232 
233  return 0;
234 
235 fail:
236  av_freep(&ctx->tracks);
237  return ret;
238 }
239 
241 {
242  PPBnkCtx *ctx = s->priv_data;
243 
244  /*
245  * Read a packet from each track, round-robin style.
246  * This method is nasty, but needed to avoid "Too many packets buffered" errors.
247  */
248  for (int i = 0; i < ctx->track_count; i++, ctx->current_track++)
249  {
250  int64_t ret;
251  int size;
252  PPBnkCtxTrack *trk;
253 
254  ctx->current_track %= ctx->track_count;
255 
256  trk = ctx->tracks + ctx->current_track;
257 
258  if (trk->bytes_read == trk->data_size)
259  continue;
260 
261  if ((ret = avio_seek(s->pb, trk->data_offset + trk->bytes_read, SEEK_SET)) < 0)
262  return ret;
263  else if (ret != trk->data_offset + trk->bytes_read)
264  return AVERROR(EIO);
265 
267 
268  if (!ctx->is_music) {
269  ret = av_get_packet(s->pb, pkt, size);
270  if (ret == AVERROR_EOF) {
271  /* If we've hit EOF, don't attempt this track again. */
272  trk->data_size = trk->bytes_read;
273  continue;
274  }
275  } else {
276  if (!pkt->data && (ret = av_new_packet(pkt, size * 2)) < 0)
277  return ret;
278  ret = avio_read(s->pb, pkt->data + size * ctx->current_track, size);
279  if (ret >= 0 && ret != size) {
280  /* Only return stereo packets if both tracks could be read. */
281  ret = AVERROR_EOF;
282  }
283  }
284  if (ret < 0)
285  return ret;
286 
287  trk->bytes_read += ret;
289  pkt->stream_index = ctx->current_track;
290  pkt->duration = ret * 2;
291 
292  if (ctx->is_music) {
293  if (pkt->stream_index == 0)
294  continue;
295 
296  pkt->stream_index = 0;
297  }
298 
299  ctx->current_track++;
300  return 0;
301  }
302 
303  /* If we reach here, we're done. */
304  return AVERROR_EOF;
305 }
306 
308 {
309  PPBnkCtx *ctx = s->priv_data;
310 
311  av_freep(&ctx->tracks);
312 
313  return 0;
314 }
315 
316 static int pp_bnk_seek(AVFormatContext *s, int stream_index,
317  int64_t pts, int flags)
318 {
319  PPBnkCtx *ctx = s->priv_data;
320 
321  if (pts != 0)
322  return AVERROR(EINVAL);
323 
324  if (ctx->is_music) {
325  av_assert0(stream_index == 0);
326  ctx->tracks[0].bytes_read = 0;
327  ctx->tracks[1].bytes_read = 0;
328  } else {
329  ctx->tracks[stream_index].bytes_read = 0;
330  }
331 
332  return 0;
333 }
334 
336  .name = "pp_bnk",
337  .long_name = NULL_IF_CONFIG_SMALL("Pro Pinball Series Soundbank"),
338  .priv_data_size = sizeof(PPBnkCtx),
344 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
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:4509
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
PPBnkCtx::is_music
int is_music
Definition: pp_bnk.c:58
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
pp_bnk_parse_header
static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
Definition: pp_bnk.c:67
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
PPBnkTrack::always1_2
uint32_t always1_2
Definition: pp_bnk.c:45
pp_bnk_seek
static int pp_bnk_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: pp_bnk.c:316
AV_CODEC_ID_ADPCM_IMA_CUNNING
@ AV_CODEC_ID_ADPCM_IMA_CUNNING
Definition: codec_id.h:402
AVPacket::data
uint8_t * data
Definition: packet.h:369
PPBnkCtx::track_count
int track_count
Definition: pp_bnk.c:55
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
PPBnkHeader::track_count
uint32_t track_count
Definition: pp_bnk.c:36
PPBnkTrack
Definition: pp_bnk.c:40
sample_rate
sample_rate
Definition: ffmpeg_filter.c:170
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
PPBnkCtxTrack
Definition: pp_bnk.c:48
PPBnkCtxTrack::data_size
uint32_t data_size
Definition: pp_bnk.c:50
fail
#define fail()
Definition: checkasm.h:133
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
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
pts
static int64_t pts
Definition: transcode_aac.c:652
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
PPBnkTrack::always1_1
uint32_t always1_1
Definition: pp_bnk.c:44
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:640
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:411
pp_bnk_probe
static int pp_bnk_probe(const AVProbeData *p)
Definition: pp_bnk.c:85
ff_pp_bnk_demuxer
AVInputFormat ff_pp_bnk_demuxer
Definition: pp_bnk.c:335
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
PPBnkTrack::id
uint32_t id
Definition: pp_bnk.c:41
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
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
PPBnkHeader::always1
uint32_t always1
Definition: pp_bnk.c:35
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
ctx
AVFormatContext * ctx
Definition: movenc.c:48
PPBnkCtx::current_track
uint32_t current_track
Definition: pp_bnk.c:57
pp_bnk_read_close
static int pp_bnk_read_close(AVFormatContext *s)
Definition: pp_bnk.c:307
PPBnkHeader::bank_id
uint32_t bank_id
Definition: pp_bnk.c:33
PPBnkCtx::tracks
PPBnkCtxTrack * tracks
Definition: pp_bnk.c:56
PP_BNK_MAX_READ_SIZE
#define PP_BNK_MAX_READ_SIZE
Definition: pp_bnk.c:28
PPBnkTrack::size
uint32_t size
Definition: pp_bnk.c:42
AVFormatContext
Format I/O context.
Definition: avformat.h:1232
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
PP_BNK_FLAG_PERSIST
@ PP_BNK_FLAG_PERSIST
Definition: pp_bnk.c:62
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
PPBnkTrack::sample_rate
uint32_t sample_rate
Definition: pp_bnk.c:43
PP_BNK_FLAG_MUSIC
@ PP_BNK_FLAG_MUSIC
Definition: pp_bnk.c:63
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
pp_bnk_parse_track
static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
Definition: pp_bnk.c:76
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:117
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
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:4945
size
int size
Definition: twinvq_data.h:10344
PP_BNK_TRACK_SIZE
#define PP_BNK_TRACK_SIZE
Definition: pp_bnk.c:30
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
PPBnkHeader
Definition: pp_bnk.c:32
PPBnkCtx
Definition: pp_bnk.c:54
PPBnkCtxTrack::bytes_read
uint32_t bytes_read
Definition: pp_bnk.c:51
pp_bnk_read_header
static int pp_bnk_read_header(AVFormatContext *s)
Definition: pp_bnk.c:108
i
int i
Definition: input.c:407
PP_BNK_FLAG_MASK
@ PP_BNK_FLAG_MASK
Definition: pp_bnk.c:64
internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:177
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
uint8_t
uint8_t
Definition: audio_convert.c:194
PPBnkHeader::sample_rate
uint32_t sample_rate
Definition: pp_bnk.c:34
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:310
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:873
PPBnkCtxTrack::data_offset
int64_t data_offset
Definition: pp_bnk.c:49
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
PP_BNK_FILE_HEADER_SIZE
#define PP_BNK_FILE_HEADER_SIZE
Definition: pp_bnk.c:29
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
PPBnkHeader::flags
uint32_t flags
Definition: pp_bnk.c:37
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
pp_bnk_read_packet
static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pp_bnk.c:240
AVPacket::stream_index
int stream_index
Definition: packet.h:371
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
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:346
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:912