FFmpeg
Loading...
Searching...
No Matches
kvag.c
Go to the documentation of this file.
1/*
2 * Simon & Schuster Interactive VAG (de)muxer
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
23#include "config_components.h"
24
26#include "avformat.h"
27#include "avio_internal.h"
28#include "demux.h"
29#include "internal.h"
30#include "mux.h"
31#include "rawenc.h"
33
34#define KVAG_TAG MKTAG('K', 'V', 'A', 'G')
35#define KVAG_HEADER_SIZE 14
36#define KVAG_MAX_READ_SIZE 4096
37
38typedef struct KVAGHeader {
39 uint32_t magic;
40 uint32_t data_size;
42 uint16_t stereo;
44
45#if CONFIG_KVAG_DEMUXER
46static int kvag_probe(const AVProbeData *p)
47{
48 if (AV_RL32(p->buf) != KVAG_TAG)
49 return 0;
50
51 return AVPROBE_SCORE_EXTENSION + 1;
52}
53
54static int kvag_read_header(AVFormatContext *s)
55{
56 int ret;
57 AVStream *st;
58 KVAGHeader hdr;
60 uint8_t buf[KVAG_HEADER_SIZE];
61
62 if (!(st = avformat_new_stream(s, NULL)))
63 return AVERROR(ENOMEM);
64
65 if ((ret = ffio_read_size(s->pb, buf, KVAG_HEADER_SIZE)) < 0)
66 return ret;
67
68 hdr.magic = AV_RL32(buf + 0);
69 hdr.data_size = AV_RL32(buf + 4);
70 hdr.sample_rate = AV_RL32(buf + 8);
71 hdr.stereo = AV_RL16(buf + 12);
72
73 if (hdr.sample_rate <= 0)
75
76 par = st->codecpar;
80
82 par->sample_rate = hdr.sample_rate;
83 par->bits_per_coded_sample = 4;
84 par->block_align = 1;
85 par->bit_rate = par->ch_layout.nb_channels *
86 (uint64_t)par->sample_rate *
88
89 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
90 st->start_time = 0;
91 st->duration = hdr.data_size *
92 (8 / par->bits_per_coded_sample) /
94
95 return 0;
96}
97
98static int kvag_read_packet(AVFormatContext *s, AVPacket *pkt)
99{
100 int ret;
101 AVCodecParameters *par = s->streams[0]->codecpar;
102
103 if ((ret = av_get_packet(s->pb, pkt, KVAG_MAX_READ_SIZE)) < 0)
104 return ret;
105
106 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
107 pkt->stream_index = 0;
108 pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->ch_layout.nb_channels;
109
110 return 0;
111}
112
113static int kvag_seek(AVFormatContext *s, int stream_index,
114 int64_t pts, int flags)
115{
116 if (pts != 0)
117 return AVERROR(EINVAL);
118
119 return avio_seek(s->pb, KVAG_HEADER_SIZE, SEEK_SET);
120}
121
123 .p.name = "kvag",
124 .p.long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
125 .read_probe = kvag_probe,
126 .read_header = kvag_read_header,
127 .read_packet = kvag_read_packet,
128 .read_seek = kvag_seek,
129};
130#endif
131
132#if CONFIG_KVAG_MUXER
133static int kvag_write_init(AVFormatContext *s)
134{
135 AVCodecParameters *par = s->streams[0]->codecpar;
136
137 if (par->ch_layout.nb_channels > 2) {
138 av_log(s, AV_LOG_ERROR, "KVAG files only support up to 2 channels\n");
139 return AVERROR(EINVAL);
140 }
141
142 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
143 av_log(s, AV_LOG_WARNING, "Stream not seekable, unable to write output file\n");
144 return AVERROR(EINVAL);
145 }
146
147 return 0;
148}
149
150static int kvag_write_header(AVFormatContext *s)
151{
152 uint8_t buf[KVAG_HEADER_SIZE];
153 AVCodecParameters *par = s->streams[0]->codecpar;
154
155 AV_WL32(buf + 0, KVAG_TAG);
156 AV_WL32(buf + 4, 0); /* Data size, we fix this up later. */
157 AV_WL32(buf + 8, par->sample_rate);
158 AV_WL16(buf + 12, par->ch_layout.nb_channels == 2);
159
160 avio_write(s->pb, buf, sizeof(buf));
161 return 0;
162}
163
164static int kvag_write_trailer(AVFormatContext *s)
165{
166 int64_t file_size, data_size;
167
168 file_size = avio_tell(s->pb);
169 data_size = file_size - KVAG_HEADER_SIZE;
170 if (data_size < UINT32_MAX) {
171 avio_seek(s->pb, 4, SEEK_SET);
172 avio_wl32(s->pb, (uint32_t)data_size);
173 avio_seek(s->pb, file_size, SEEK_SET);
174 } else {
176 "Filesize %"PRId64" invalid for KVAG, output file will be broken\n",
177 file_size);
178 }
179
180 return 0;
181}
182
184 .p.name = "kvag",
185 .p.long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
186 .p.extensions = "vag",
187 .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_SSI,
188 .p.video_codec = AV_CODEC_ID_NONE,
189 .p.subtitle_codec = AV_CODEC_ID_NONE,
190 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
192 .init = kvag_write_init,
193 .write_header = kvag_write_header,
194 .write_packet = ff_raw_write_packet,
195 .write_trailer = kvag_write_trailer
196};
197#endif
const FFInputFormat ff_kvag_demuxer
const FFOutputFormat ff_kvag_muxer
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_EXTENSION
score for file extension
Definition avformat.h:481
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
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition codec_id.h:413
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RL16(p)
#define AV_WL16(p, v)
#define KVAG_MAX_READ_SIZE
Definition kvag.c:36
#define KVAG_TAG
Definition kvag.c:34
#define KVAG_HEADER_SIZE
Definition kvag.c:35
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
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
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
uint32_t magic
Definition kvag.c:39
uint32_t data_size
Definition kvag.c:40
int sample_rate
Definition kvag.c:41
uint16_t stereo
Definition kvag.c:42
#define av_log(a,...)
static int64_t pts