Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/pp_bnk.c
Line
Count
Source
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"
28
#include "libavutil/channel_layout.h"
29
#include "libavutil/internal.h"
30
#include "libavutil/mem.h"
31
32
#define PP_BNK_MAX_READ_SIZE    4096
33
1.65k
#define PP_BNK_FILE_HEADER_SIZE 20
34
49.6k
#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 {
53
    int64_t         data_offset;
54
    uint32_t        data_size;
55
    uint32_t        bytes_read;
56
} PPBnkCtxTrack;
57
58
typedef struct PPBnkCtx {
59
    int             track_count;
60
    PPBnkCtxTrack   *tracks;
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. */
68
    PP_BNK_FLAG_MASK    = (PP_BNK_FLAG_PERSIST | PP_BNK_FLAG_MUSIC)
69
};
70
71
static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
72
1.52k
{
73
1.52k
    hdr->bank_id        = AV_RL32(buf +  0);
74
1.52k
    hdr->sample_rate    = AV_RL32(buf +  4);
75
1.52k
    hdr->always1        = AV_RL32(buf +  8);
76
1.52k
    hdr->track_count    = AV_RL32(buf + 12);
77
1.52k
    hdr->flags          = AV_RL32(buf + 16);
78
1.52k
}
79
80
static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
81
24.4k
{
82
24.4k
    trk->id             = AV_RL32(buf +  0);
83
24.4k
    trk->size           = AV_RL32(buf +  4);
84
24.4k
    trk->sample_rate    = AV_RL32(buf +  8);
85
24.4k
    trk->always1_1      = AV_RL32(buf + 12);
86
24.4k
    trk->always1_2      = AV_RL32(buf + 16);
87
24.4k
}
88
89
static int pp_bnk_probe(const AVProbeData *p)
90
959k
{
91
959k
    uint32_t sample_rate = AV_RL32(p->buf +  4);
92
959k
    uint32_t track_count = AV_RL32(p->buf + 12);
93
959k
    uint32_t flags       = AV_RL32(p->buf + 16);
94
95
959k
    if (track_count == 0 || track_count > INT_MAX)
96
411k
        return 0;
97
98
547k
    if ((sample_rate !=  5512) && (sample_rate != 11025) &&
99
546k
        (sample_rate != 22050) && (sample_rate != 44100))
100
546k
        return 0;
101
102
    /* Check the first track header. */
103
936
    if (AV_RL32(p->buf + 28) != sample_rate)
104
577
        return 0;
105
106
359
    if ((flags & ~PP_BNK_FLAG_MASK) != 0)
107
139
        return 0;
108
109
220
    return AVPROBE_SCORE_MAX / 4 + 1;
110
359
}
111
112
static int pp_bnk_read_header(AVFormatContext *s)
113
1.65k
{
114
1.65k
    int64_t ret;
115
1.65k
    AVStream *st;
116
1.65k
    AVCodecParameters *par;
117
1.65k
    PPBnkCtx *ctx = s->priv_data;
118
1.65k
    uint8_t buf[FFMAX(PP_BNK_FILE_HEADER_SIZE, PP_BNK_TRACK_SIZE)];
119
1.65k
    PPBnkHeader hdr;
120
121
1.65k
    if ((ret = ffio_read_size(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
122
127
        return ret;
123
124
1.52k
    pp_bnk_parse_header(&hdr, buf);
125
126
1.52k
    if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
127
39
        return AVERROR_INVALIDDATA;
128
129
1.48k
    if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
130
32
        return AVERROR_INVALIDDATA;
131
132
1.45k
    if (hdr.always1 != 1) {
133
77
        avpriv_request_sample(s, "Non-one header value");
134
77
        return AVERROR_PATCHWELCOME;
135
77
    }
136
137
1.37k
    ctx->track_count = hdr.track_count;
138
139
1.37k
    if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
140
36
        return AVERROR(ENOMEM);
141
142
    /* Parse and validate each track. */
143
24.9k
    for (int i = 0; i < hdr.track_count; i++) {
144
24.8k
        PPBnkTrack e;
145
24.8k
        PPBnkCtxTrack *trk = ctx->tracks + i;
146
147
24.8k
        ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
148
24.8k
        if (ret < 0 && ret != AVERROR_EOF)
149
0
            return ret;
150
151
        /* Short byte-count or EOF, we have a truncated file. */
152
24.8k
        if (ret != PP_BNK_TRACK_SIZE) {
153
416
            av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
154
416
                   i, hdr.track_count);
155
416
            ctx->track_count = i;
156
416
            break;
157
416
        }
158
159
24.4k
        pp_bnk_parse_track(&e, buf);
160
161
        /* The individual sample rates of all tracks must match that of the file header. */
162
24.4k
        if (e.sample_rate != hdr.sample_rate)
163
68
            return AVERROR_INVALIDDATA;
164
165
24.3k
        if (e.always1_1 != 1 || e.always1_2 != 1) {
166
83
            avpriv_request_sample(s, "Non-one track header values");
167
83
            return AVERROR_PATCHWELCOME;
168
83
        }
169
170
24.2k
        trk->data_offset = avio_tell(s->pb);
171
24.2k
        trk->data_size   = e.size;
172
24.2k
        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
24.2k
        if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
181
611
            ctx->track_count = i + 1;
182
611
            av_log(s, AV_LOG_WARNING,
183
611
                   "Track %d has truncated data, assuming track count == %d\n",
184
611
                   i, ctx->track_count);
185
611
            break;
186
23.6k
        } else if (ret < 0) {
187
32
            return ret;
188
32
        }
189
24.2k
    }
190
191
    /* File is only a header. */
192
1.15k
    if (ctx->track_count == 0)
193
45
        return AVERROR_INVALIDDATA;
194
195
1.11k
    ctx->is_music = (hdr.flags & PP_BNK_FLAG_MUSIC) &&
196
591
                    (ctx->track_count == 2) &&
197
130
                    (ctx->tracks[0].data_size == ctx->tracks[1].data_size);
198
199
    /* Build the streams. */
200
23.9k
    for (int i = 0; i < (ctx->is_music ? 1 : ctx->track_count); i++) {
201
22.8k
        if (!(st = avformat_new_stream(s, NULL)))
202
2
            return AVERROR(ENOMEM);
203
204
22.8k
        par                         = st->codecpar;
205
22.8k
        par->codec_type             = AVMEDIA_TYPE_AUDIO;
206
22.8k
        par->codec_id               = AV_CODEC_ID_ADPCM_IMA_CUNNING;
207
22.8k
        par->format                 = AV_SAMPLE_FMT_S16P;
208
209
22.8k
        av_channel_layout_default(&par->ch_layout, ctx->is_music + 1);
210
22.8k
        par->sample_rate            = hdr.sample_rate;
211
22.8k
        par->bits_per_coded_sample  = 4;
212
22.8k
        par->block_align            = 1;
213
22.8k
        par->bit_rate               = par->sample_rate * (int64_t)par->bits_per_coded_sample *
214
22.8k
                                      par->ch_layout.nb_channels;
215
216
22.8k
        avpriv_set_pts_info(st, 64, 1, par->sample_rate);
217
22.8k
        st->start_time              = 0;
218
22.8k
        st->duration                = ctx->tracks[i].data_size * 2;
219
22.8k
    }
220
221
1.11k
    return 0;
222
1.11k
}
223
224
static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
225
13.3k
{
226
13.3k
    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
51.6k
    for (int i = 0; i < ctx->track_count; i++, ctx->current_track++)
233
49.9k
    {
234
49.9k
        int64_t ret;
235
49.9k
        int size;
236
49.9k
        PPBnkCtxTrack *trk;
237
238
49.9k
        ctx->current_track %= ctx->track_count;
239
240
49.9k
        trk = ctx->tracks + ctx->current_track;
241
242
49.9k
        if (trk->bytes_read == trk->data_size)
243
37.4k
            continue;
244
245
12.5k
        if ((ret = avio_seek(s->pb, trk->data_offset + trk->bytes_read, SEEK_SET)) < 0)
246
324
            return ret;
247
12.2k
        else if (ret != trk->data_offset + trk->bytes_read)
248
0
            return AVERROR_INVALIDDATA;
249
250
12.2k
        size = FFMIN(trk->data_size - trk->bytes_read, PP_BNK_MAX_READ_SIZE);
251
252
12.2k
        if (!ctx->is_music) {
253
12.0k
            ret = av_get_packet(s->pb, pkt, size);
254
12.0k
            if (ret == AVERROR_EOF) {
255
                /* If we've hit EOF, don't attempt this track again. */
256
802
                trk->data_size = trk->bytes_read;
257
802
                continue;
258
802
            }
259
12.0k
        } else {
260
174
            if (!pkt->data && (ret = av_new_packet(pkt, size * 2)) < 0)
261
0
                return ret;
262
174
            ret = avio_read(s->pb, pkt->data + size * ctx->current_track, size);
263
174
            if (ret >= 0 && ret != size) {
264
                /* Only return stereo packets if both tracks could be read. */
265
36
                ret = AVERROR_EOF;
266
36
            }
267
174
        }
268
11.4k
        if (ret < 0)
269
61
            return ret;
270
271
11.3k
        trk->bytes_read    += ret;
272
11.3k
        pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
273
11.3k
        pkt->stream_index   = ctx->current_track;
274
11.3k
        pkt->duration       = ret * 2;
275
276
11.3k
        if (ctx->is_music) {
277
113
            if (pkt->stream_index == 0)
278
71
                continue;
279
280
42
            pkt->stream_index = 0;
281
42
        }
282
283
11.2k
        ctx->current_track++;
284
11.2k
        return 0;
285
11.3k
    }
286
287
    /* If we reach here, we're done. */
288
1.70k
    return AVERROR_EOF;
289
13.3k
}
290
291
static int pp_bnk_read_close(AVFormatContext *s)
292
1.65k
{
293
1.65k
    PPBnkCtx *ctx = s->priv_data;
294
295
1.65k
    av_freep(&ctx->tracks);
296
297
1.65k
    return 0;
298
1.65k
}
299
300
static int pp_bnk_seek(AVFormatContext *s, int stream_index,
301
                       int64_t pts, int flags)
302
0
{
303
0
    PPBnkCtx *ctx = s->priv_data;
304
305
0
    if (pts != 0)
306
0
        return AVERROR(EINVAL);
307
308
0
    if (ctx->is_music) {
309
0
        av_assert0(stream_index == 0);
310
0
        ctx->tracks[0].bytes_read = 0;
311
0
        ctx->tracks[1].bytes_read = 0;
312
0
    } else {
313
0
        ctx->tracks[stream_index].bytes_read = 0;
314
0
    }
315
316
0
    return 0;
317
0
}
318
319
const FFInputFormat ff_pp_bnk_demuxer = {
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,
324
    .read_probe     = pp_bnk_probe,
325
    .read_header    = pp_bnk_read_header,
326
    .read_packet    = pp_bnk_read_packet,
327
    .read_close     = pp_bnk_read_close,
328
    .read_seek      = pp_bnk_seek,
329
};