Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/vpk.c
Line
Count
Source
1
/*
2
 * VPK demuxer
3
 * Copyright (c) 2015 Paul B Mahol
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "libavutil/intreadwrite.h"
23
#include "avformat.h"
24
#include "avio_internal.h"
25
#include "demux.h"
26
#include "internal.h"
27
28
typedef struct VPKDemuxContext {
29
    unsigned data_start;
30
    unsigned block_count;
31
    unsigned current_block;
32
    unsigned last_block_size;
33
} VPKDemuxContext;
34
35
static int vpk_probe(const AVProbeData *p)
36
964k
{
37
964k
    if (AV_RL32(p->buf) != MKBETAG('V','P','K',' '))
38
964k
        return 0;
39
40
264
    return AVPROBE_SCORE_MAX / 3 * 2;
41
964k
}
42
43
static int vpk_read_header(AVFormatContext *s)
44
1.63k
{
45
1.63k
    VPKDemuxContext *vpk = s->priv_data;
46
1.63k
    unsigned offset;
47
1.63k
    unsigned samples_per_block;
48
1.63k
    AVStream *st;
49
50
1.63k
    vpk->current_block = 0;
51
1.63k
    st = avformat_new_stream(s, NULL);
52
1.63k
    if (!st)
53
0
        return AVERROR(ENOMEM);
54
55
1.63k
    avio_skip(s->pb, 4);
56
1.63k
    st->duration           = avio_rl32(s->pb) * 28 / 16;
57
1.63k
    offset                 = avio_rl32(s->pb);
58
1.63k
    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
59
1.63k
    st->codecpar->codec_id    = AV_CODEC_ID_ADPCM_PSX;
60
1.63k
    st->codecpar->block_align = avio_rl32(s->pb);
61
1.63k
    st->codecpar->sample_rate = avio_rl32(s->pb);
62
1.63k
    if (st->codecpar->sample_rate <= 0 || st->codecpar->block_align <= 0)
63
220
        return AVERROR_INVALIDDATA;
64
1.41k
    st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb);
65
1.41k
    if (st->codecpar->ch_layout.nb_channels <= 0)
66
43
        return AVERROR_INVALIDDATA;
67
1.36k
    samples_per_block      = ((st->codecpar->block_align / st->codecpar->ch_layout.nb_channels) * 28LL) / 16;
68
1.36k
    if (samples_per_block <= 0)
69
5
        return AVERROR_INVALIDDATA;
70
1.36k
    vpk->block_count       = (st->duration + (samples_per_block - 1)) / samples_per_block;
71
1.36k
    vpk->last_block_size   = (st->duration % samples_per_block) * 16 * st->codecpar->ch_layout.nb_channels / 28;
72
73
1.36k
    if (offset < avio_tell(s->pb))
74
6
        return AVERROR_INVALIDDATA;
75
1.35k
    avio_skip(s->pb, offset - avio_tell(s->pb));
76
1.35k
    vpk->data_start = offset;
77
1.35k
    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
78
79
1.35k
    return 0;
80
1.36k
}
81
82
static int vpk_read_packet(AVFormatContext *s, AVPacket *pkt)
83
58.2k
{
84
58.2k
    AVCodecParameters *par = s->streams[0]->codecpar;
85
58.2k
    VPKDemuxContext *vpk = s->priv_data;
86
58.2k
    int ret, i;
87
88
58.2k
    vpk->current_block++;
89
58.2k
    if (vpk->current_block == vpk->block_count) {
90
349
        unsigned size, skip;
91
349
        uint64_t pos;
92
93
349
        if (par->ch_layout.nb_channels <= 0)
94
0
            return AVERROR_INVALIDDATA;
95
349
        size = vpk->last_block_size / par->ch_layout.nb_channels;
96
349
        skip = (par->block_align - vpk->last_block_size) / par->ch_layout.nb_channels;
97
349
        pos = avio_tell(s->pb);
98
99
349
        ret = av_new_packet(pkt, vpk->last_block_size);
100
349
        if (ret < 0)
101
2
            return ret;
102
415k
        for (i = 0; i < par->ch_layout.nb_channels; i++) {
103
415k
            ret = ffio_read_size(s->pb, pkt->data + i * size, size);
104
415k
            avio_skip(s->pb, skip);
105
415k
            if (ret < 0) {
106
284
                return ret;
107
284
            }
108
415k
        }
109
63
        pkt->pos = pos;
110
63
        pkt->stream_index = 0;
111
57.8k
    } else if (vpk->current_block < vpk->block_count) {
112
57.5k
        ret = av_get_packet(s->pb, pkt, par->block_align);
113
57.5k
        pkt->stream_index = 0;
114
57.5k
    } else {
115
334
        return AVERROR_EOF;
116
334
    }
117
118
57.6k
    return ret;
119
58.2k
}
120
121
static int vpk_read_seek(AVFormatContext *s, int stream_index,
122
                         int64_t timestamp, int flags)
123
0
{
124
0
    AVStream *st = s->streams[stream_index];
125
0
    AVCodecParameters *par = st->codecpar;
126
0
    VPKDemuxContext *vpk = s->priv_data;
127
0
    int samples_per_block;
128
0
    int64_t ret = 0;
129
130
0
    samples_per_block = av_get_audio_frame_duration2(par, par->block_align);
131
0
    if (samples_per_block > 0)
132
0
        timestamp /= samples_per_block;
133
0
    else
134
0
        return -1;
135
0
    ret = avio_seek(s->pb, vpk->data_start + timestamp * par->block_align, SEEK_SET);
136
0
    if (ret < 0)
137
0
        return ret;
138
139
0
    vpk->current_block = timestamp;
140
0
    avpriv_update_cur_dts(s, st, timestamp * samples_per_block);
141
0
    return 0;
142
0
}
143
144
const FFInputFormat ff_vpk_demuxer = {
145
    .p.name         = "vpk",
146
    .p.long_name    = NULL_IF_CONFIG_SMALL("Sony PS2 VPK"),
147
    .p.extensions   = "vpk",
148
    .priv_data_size = sizeof(VPKDemuxContext),
149
    .read_probe     = vpk_probe,
150
    .read_header    = vpk_read_header,
151
    .read_packet    = vpk_read_packet,
152
    .read_seek      = vpk_read_seek,
153
};