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 "avio_internal.h"
24 #include "demux.h"
25 #include "internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/avassert.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/mem.h"
31 
32 #define PP_BNK_MAX_READ_SIZE 4096
33 #define PP_BNK_FILE_HEADER_SIZE 20
34 #define PP_BNK_TRACK_SIZE 20
35 
36 typedef struct PPBnkHeader {
37  uint32_t bank_id; /*< Bank ID, useless for our purposes. */
38  uint32_t sample_rate; /*< Sample rate of the contained tracks. */
39  uint32_t always1; /*< Unknown, always seems to be 1. */
40  uint32_t track_count; /*< Number of tracks in the file. */
41  uint32_t flags; /*< Flags. */
42 } PPBnkHeader;
43 
44 typedef struct PPBnkTrack {
45  uint32_t id; /*< Track ID. Usually track[i].id == track[i-1].id + 1, but not always */
46  uint32_t size; /*< Size of the data in bytes. */
47  uint32_t sample_rate; /*< Sample rate. */
48  uint32_t always1_1; /*< Unknown, always seems to be 1. */
49  uint32_t always1_2; /*< Unknown, always seems to be 1. */
50 } PPBnkTrack;
51 
52 typedef struct PPBnkCtxTrack {
54  uint32_t data_size;
55  uint32_t bytes_read;
57 
58 typedef struct PPBnkCtx {
61  uint32_t current_track;
62  int is_music;
63 } PPBnkCtx;
64 
65 enum {
66  PP_BNK_FLAG_PERSIST = (1 << 0), /*< This is a large file, keep in memory. */
67  PP_BNK_FLAG_MUSIC = (1 << 1), /*< This is music. */
69 };
70 
71 static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
72 {
73  hdr->bank_id = AV_RL32(buf + 0);
74  hdr->sample_rate = AV_RL32(buf + 4);
75  hdr->always1 = AV_RL32(buf + 8);
76  hdr->track_count = AV_RL32(buf + 12);
77  hdr->flags = AV_RL32(buf + 16);
78 }
79 
80 static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
81 {
82  trk->id = AV_RL32(buf + 0);
83  trk->size = AV_RL32(buf + 4);
84  trk->sample_rate = AV_RL32(buf + 8);
85  trk->always1_1 = AV_RL32(buf + 12);
86  trk->always1_2 = AV_RL32(buf + 16);
87 }
88 
89 static int pp_bnk_probe(const AVProbeData *p)
90 {
91  uint32_t sample_rate = AV_RL32(p->buf + 4);
92  uint32_t track_count = AV_RL32(p->buf + 12);
93  uint32_t flags = AV_RL32(p->buf + 16);
94 
95  if (track_count == 0 || track_count > INT_MAX)
96  return 0;
97 
98  if ((sample_rate != 5512) && (sample_rate != 11025) &&
99  (sample_rate != 22050) && (sample_rate != 44100))
100  return 0;
101 
102  /* Check the first track header. */
103  if (AV_RL32(p->buf + 28) != sample_rate)
104  return 0;
105 
106  if ((flags & ~PP_BNK_FLAG_MASK) != 0)
107  return 0;
108 
109  return AVPROBE_SCORE_MAX / 4 + 1;
110 }
111 
113 {
114  int64_t ret;
115  AVStream *st;
116  AVCodecParameters *par;
117  PPBnkCtx *ctx = s->priv_data;
119  PPBnkHeader hdr;
120 
121  if ((ret = ffio_read_size(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
122  return ret;
123 
124  pp_bnk_parse_header(&hdr, buf);
125 
126  if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
127  return AVERROR_INVALIDDATA;
128 
129  if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
130  return AVERROR_INVALIDDATA;
131 
132  if (hdr.always1 != 1) {
133  avpriv_request_sample(s, "Non-one header value");
134  return AVERROR_PATCHWELCOME;
135  }
136 
137  ctx->track_count = hdr.track_count;
138 
139  if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
140  return AVERROR(ENOMEM);
141 
142  /* Parse and validate each track. */
143  for (int i = 0; i < hdr.track_count; i++) {
144  PPBnkTrack e;
145  PPBnkCtxTrack *trk = ctx->tracks + i;
146 
147  ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
148  if (ret < 0 && ret != AVERROR_EOF)
149  return ret;
150 
151  /* Short byte-count or EOF, we have a truncated file. */
152  if (ret != PP_BNK_TRACK_SIZE) {
153  av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
154  i, hdr.track_count);
155  ctx->track_count = i;
156  break;
157  }
158 
159  pp_bnk_parse_track(&e, buf);
160 
161  /* The individual sample rates of all tracks must match that of the file header. */
162  if (e.sample_rate != hdr.sample_rate)
163  return AVERROR_INVALIDDATA;
164 
165  if (e.always1_1 != 1 || e.always1_2 != 1) {
166  avpriv_request_sample(s, "Non-one track header values");
167  return AVERROR_PATCHWELCOME;
168  }
169 
170  trk->data_offset = avio_tell(s->pb);
171  trk->data_size = e.size;
172  trk->bytes_read = 0;
173 
174  /*
175  * Skip over the data to the next stream header.
176  * Sometimes avio_skip() doesn't detect EOF. If it doesn't, either:
177  * - the avio_read() above will, or
178  * - pp_bnk_read_packet() will read a truncated last track.
179  */
180  if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
181  ctx->track_count = i + 1;
183  "Track %d has truncated data, assuming track count == %d\n",
184  i, ctx->track_count);
185  break;
186  } else if (ret < 0) {
187  return ret;
188  }
189  }
190 
191  /* File is only a header. */
192  if (ctx->track_count == 0)
193  return AVERROR_INVALIDDATA;
194 
195  ctx->is_music = (hdr.flags & PP_BNK_FLAG_MUSIC) &&
196  (ctx->track_count == 2) &&
197  (ctx->tracks[0].data_size == ctx->tracks[1].data_size);
198 
199  /* Build the streams. */
200  for (int i = 0; i < (ctx->is_music ? 1 : ctx->track_count); i++) {
201  if (!(st = avformat_new_stream(s, NULL)))
202  return AVERROR(ENOMEM);
203 
204  par = st->codecpar;
207  par->format = AV_SAMPLE_FMT_S16P;
208 
209  av_channel_layout_default(&par->ch_layout, ctx->is_music + 1);
210  par->sample_rate = hdr.sample_rate;
211  par->bits_per_coded_sample = 4;
212  par->block_align = 1;
213  par->bit_rate = par->sample_rate * (int64_t)par->bits_per_coded_sample *
214  par->ch_layout.nb_channels;
215 
216  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
217  st->start_time = 0;
218  st->duration = ctx->tracks[i].data_size * 2;
219  }
220 
221  return 0;
222 }
223 
225 {
226  PPBnkCtx *ctx = s->priv_data;
227 
228  /*
229  * Read a packet from each track, round-robin style.
230  * This method is nasty, but needed to avoid "Too many packets buffered" errors.
231  */
232  for (int i = 0; i < ctx->track_count; i++, ctx->current_track++)
233  {
234  int64_t ret;
235  int size;
236  PPBnkCtxTrack *trk;
237 
238  ctx->current_track %= ctx->track_count;
239 
240  trk = ctx->tracks + ctx->current_track;
241 
242  if (trk->bytes_read == trk->data_size)
243  continue;
244 
245  if ((ret = avio_seek(s->pb, trk->data_offset + trk->bytes_read, SEEK_SET)) < 0)
246  return ret;
247  else if (ret != trk->data_offset + trk->bytes_read)
248  return AVERROR_INVALIDDATA;
249 
251 
252  if (!ctx->is_music) {
253  ret = av_get_packet(s->pb, pkt, size);
254  if (ret == AVERROR_EOF) {
255  /* If we've hit EOF, don't attempt this track again. */
256  trk->data_size = trk->bytes_read;
257  continue;
258  }
259  } else {
260  if (!pkt->data && (ret = av_new_packet(pkt, size * 2)) < 0)
261  return ret;
262  ret = avio_read(s->pb, pkt->data + size * ctx->current_track, size);
263  if (ret >= 0 && ret != size) {
264  /* Only return stereo packets if both tracks could be read. */
265  ret = AVERROR_EOF;
266  }
267  }
268  if (ret < 0)
269  return ret;
270 
271  trk->bytes_read += ret;
273  pkt->stream_index = ctx->current_track;
274  pkt->duration = ret * 2;
275 
276  if (ctx->is_music) {
277  if (pkt->stream_index == 0)
278  continue;
279 
280  pkt->stream_index = 0;
281  }
282 
283  ctx->current_track++;
284  return 0;
285  }
286 
287  /* If we reach here, we're done. */
288  return AVERROR_EOF;
289 }
290 
292 {
293  PPBnkCtx *ctx = s->priv_data;
294 
295  av_freep(&ctx->tracks);
296 
297  return 0;
298 }
299 
300 static int pp_bnk_seek(AVFormatContext *s, int stream_index,
301  int64_t pts, int flags)
302 {
303  PPBnkCtx *ctx = s->priv_data;
304 
305  if (pts != 0)
306  return AVERROR(EINVAL);
307 
308  if (ctx->is_music) {
309  av_assert0(stream_index == 0);
310  ctx->tracks[0].bytes_read = 0;
311  ctx->tracks[1].bytes_read = 0;
312  } else {
313  ctx->tracks[stream_index].bytes_read = 0;
314  }
315 
316  return 0;
317 }
318 
320  .p.name = "pp_bnk",
321  .p.long_name = NULL_IF_CONFIG_SMALL("Pro Pinball Series Soundbank"),
322  .priv_data_size = sizeof(PPBnkCtx),
323  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
329 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
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
PPBnkCtx::is_music
int is_music
Definition: pp_bnk.c:62
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.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
pp_bnk_parse_header
static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
Definition: pp_bnk.c:71
int64_t
long long int64_t
Definition: coverity.c:34
PPBnkTrack::always1_2
uint32_t always1_2
Definition: pp_bnk.c:49
pp_bnk_seek
static int pp_bnk_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: pp_bnk.c:300
AV_CODEC_ID_ADPCM_IMA_CUNNING
@ AV_CODEC_ID_ADPCM_IMA_CUNNING
Definition: codec_id.h:424
AVPacket::data
uint8_t * data
Definition: packet.h:588
PPBnkCtx::track_count
int track_count
Definition: pp_bnk.c:59
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:606
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
PPBnkHeader::track_count
uint32_t track_count
Definition: pp_bnk.c:40
PPBnkTrack
Definition: pp_bnk.c:44
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
PP_BNK_FLAG_MASK
@ PP_BNK_FLAG_MASK
Definition: pp_bnk.c:68
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:777
PPBnkCtxTrack
Definition: pp_bnk.c:52
PPBnkCtxTrack::data_size
uint32_t data_size
Definition: pp_bnk.c:54
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
pts
static int64_t pts
Definition: transcode_aac.c:644
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:803
PPBnkTrack::always1_1
uint32_t always1_1
Definition: pp_bnk.c:48
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:644
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
pp_bnk_probe
static int pp_bnk_probe(const AVProbeData *p)
Definition: pp_bnk.c:89
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
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: packet.c:98
PPBnkTrack::id
uint32_t id
Definition: pp_bnk.c:45
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:549
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
PPBnkHeader::always1
uint32_t always1
Definition: pp_bnk.c:39
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
ctx
AVFormatContext * ctx
Definition: movenc.c:49
PPBnkCtx::current_track
uint32_t current_track
Definition: pp_bnk.c:61
pp_bnk_read_close
static int pp_bnk_read_close(AVFormatContext *s)
Definition: pp_bnk.c:291
PPBnkHeader::bank_id
uint32_t bank_id
Definition: pp_bnk.c:37
PPBnkCtx::tracks
PPBnkCtxTrack * tracks
Definition: pp_bnk.c:60
PP_BNK_MAX_READ_SIZE
#define PP_BNK_MAX_READ_SIZE
Definition: pp_bnk.c:32
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
PPBnkTrack::size
uint32_t size
Definition: pp_bnk.c:46
AVFormatContext
Format I/O context.
Definition: avformat.h:1264
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:767
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
PPBnkTrack::sample_rate
uint32_t sample_rate
Definition: pp_bnk.c:47
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
pp_bnk_parse_track
static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
Definition: pp_bnk.c:80
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
size
int size
Definition: twinvq_data.h:10344
PP_BNK_TRACK_SIZE
#define PP_BNK_TRACK_SIZE
Definition: pp_bnk.c:34
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:51
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:594
read_header
static int read_header(FFV1Context *f, RangeCoder *c)
Definition: ffv1dec.c:498
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
PPBnkHeader
Definition: pp_bnk.c:36
PPBnkCtx
Definition: pp_bnk.c:58
PPBnkCtxTrack::bytes_read
uint32_t bytes_read
Definition: pp_bnk.c:55
pp_bnk_read_header
static int pp_bnk_read_header(AVFormatContext *s)
Definition: pp_bnk.c:112
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:839
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_internal.h
internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
ff_pp_bnk_demuxer
const FFInputFormat ff_pp_bnk_demuxer
Definition: pp_bnk.c:319
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
PPBnkHeader::sample_rate
uint32_t sample_rate
Definition: pp_bnk.c:38
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:98
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:744
PPBnkCtxTrack::data_offset
int64_t data_offset
Definition: pp_bnk.c:53
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
PP_BNK_FILE_HEADER_SIZE
#define PP_BNK_FILE_HEADER_SIZE
Definition: pp_bnk.c:33
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
PP_BNK_FLAG_PERSIST
@ PP_BNK_FLAG_PERSIST
Definition: pp_bnk.c:66
channel_layout.h
PPBnkHeader::flags
uint32_t flags
Definition: pp_bnk.c:41
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:615
pp_bnk_read_packet
static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pp_bnk.c:224
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AVPacket::stream_index
int stream_index
Definition: packet.h:590
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:321
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
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
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:565
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FFInputFormat
Definition: demux.h:47
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
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:665
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:793
PP_BNK_FLAG_MUSIC
@ PP_BNK_FLAG_MUSIC
Definition: pp_bnk.c:67