FFmpeg
Loading...
Searching...
No Matches
vpk.c
Go to the documentation of this file.
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
23#include "avformat.h"
24#include "avio_internal.h"
25#include "demux.h"
26#include "internal.h"
27
28typedef struct VPKDemuxContext {
29 unsigned data_start;
30 unsigned block_count;
31 unsigned current_block;
34
35static int vpk_probe(const AVProbeData *p)
36{
37 if (AV_RL32(p->buf) != MKBETAG('V','P','K',' '))
38 return 0;
39
40 return AVPROBE_SCORE_MAX / 3 * 2;
41}
42
44{
45 VPKDemuxContext *vpk = s->priv_data;
46 unsigned offset;
47 unsigned samples_per_block;
48 AVStream *st;
49
50 vpk->current_block = 0;
52 if (!st)
53 return AVERROR(ENOMEM);
54
55 avio_skip(s->pb, 4);
56 st->duration = avio_rl32(s->pb) * 28 / 16;
57 offset = avio_rl32(s->pb);
60 st->codecpar->block_align = avio_rl32(s->pb);
61 st->codecpar->sample_rate = avio_rl32(s->pb);
62 if (st->codecpar->sample_rate <= 0 || st->codecpar->block_align <= 0)
65 if (st->codecpar->ch_layout.nb_channels <= 0)
67 samples_per_block = ((st->codecpar->block_align / st->codecpar->ch_layout.nb_channels) * 28LL) / 16;
68 if (samples_per_block <= 0)
70 vpk->block_count = (st->duration + (samples_per_block - 1)) / samples_per_block;
71 vpk->last_block_size = (st->duration % samples_per_block) * 16 * st->codecpar->ch_layout.nb_channels / 28;
72
73 if (offset < avio_tell(s->pb))
75 avio_skip(s->pb, offset - avio_tell(s->pb));
76 vpk->data_start = offset;
78
79 return 0;
80}
81
83{
84 AVCodecParameters *par = s->streams[0]->codecpar;
85 VPKDemuxContext *vpk = s->priv_data;
86 int ret, i;
87
88 vpk->current_block++;
89 if (vpk->current_block == vpk->block_count) {
90 unsigned size = vpk->last_block_size / par->ch_layout.nb_channels;
91 unsigned skip = (par->block_align - vpk->last_block_size) / par->ch_layout.nb_channels;
92 uint64_t pos = avio_tell(s->pb);
93
95 if (ret < 0)
96 return ret;
97 for (i = 0; i < par->ch_layout.nb_channels; i++) {
98 ret = ffio_read_size(s->pb, pkt->data + i * size, size);
99 avio_skip(s->pb, skip);
100 if (ret < 0) {
101 return ret;
102 }
103 }
104 pkt->pos = pos;
105 pkt->stream_index = 0;
106 } else if (vpk->current_block < vpk->block_count) {
107 ret = av_get_packet(s->pb, pkt, par->block_align);
108 pkt->stream_index = 0;
109 } else {
110 return AVERROR_EOF;
111 }
112
113 return ret;
114}
115
116static int vpk_read_seek(AVFormatContext *s, int stream_index,
117 int64_t timestamp, int flags)
118{
119 AVStream *st = s->streams[stream_index];
120 AVCodecParameters *par = st->codecpar;
121 VPKDemuxContext *vpk = s->priv_data;
122 int samples_per_block;
123 int64_t ret = 0;
124
125 samples_per_block = av_get_audio_frame_duration2(par, par->block_align);
126 if (samples_per_block > 0)
127 timestamp /= samples_per_block;
128 else
129 return -1;
130 ret = avio_seek(s->pb, vpk->data_start + timestamp * par->block_align, SEEK_SET);
131 if (ret < 0)
132 return ret;
133
134 vpk->current_block = timestamp;
135 avpriv_update_cur_dts(s, st, timestamp * samples_per_block);
136 return 0;
137}
138
140 .p.name = "vpk",
141 .p.long_name = NULL_IF_CONFIG_SMALL("Sony PS2 VPK"),
142 .p.extensions = "vpk",
143 .priv_data_size = sizeof(VPKDemuxContext),
148};
const FFInputFormat ff_vpk_demuxer
Definition vpk.c:139
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition seek.c:37
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ADPCM_PSX
Definition codec_id.h:407
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
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
Definition utils.c:823
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define MKBETAG(a, b, c, d)
Definition macros.h:56
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
unsigned last_block_size
Definition vpk.c:32
unsigned block_count
Definition vpk.c:30
unsigned data_start
Definition vpk.c:29
unsigned current_block
Definition vpk.c:31
int size
static int vpk_read_header(AVFormatContext *s)
Definition vpk.c:43
static int vpk_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition vpk.c:82
static int vpk_probe(const AVProbeData *p)
Definition vpk.c:35
static int vpk_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition vpk.c:116