FFmpeg  4.4.5
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 #include "avformat.h"
23 #include "internal.h"
24 #include "rawenc.h"
25 #include "libavutil/intreadwrite.h"
26 
27 #define KVAG_TAG MKTAG('K', 'V', 'A', 'G')
28 #define KVAG_HEADER_SIZE 14
29 #define KVAG_MAX_READ_SIZE 4096
30 
31 typedef struct KVAGHeader {
32  uint32_t magic;
33  uint32_t data_size;
35  uint16_t stereo;
36 } KVAGHeader;
37 
38 #if CONFIG_KVAG_DEMUXER
39 static int kvag_probe(const AVProbeData *p)
40 {
41  if (AV_RL32(p->buf) != KVAG_TAG)
42  return 0;
43 
44  return AVPROBE_SCORE_EXTENSION + 1;
45 }
46 
47 static int kvag_read_header(AVFormatContext *s)
48 {
49  int ret;
50  AVStream *st;
51  KVAGHeader hdr;
52  AVCodecParameters *par;
54 
55  if (!(st = avformat_new_stream(s, NULL)))
56  return AVERROR(ENOMEM);
57 
58  if ((ret = avio_read(s->pb, buf, KVAG_HEADER_SIZE)) < 0)
59  return ret;
60  else if (ret != KVAG_HEADER_SIZE)
61  return AVERROR(EIO);
62 
63  hdr.magic = AV_RL32(buf + 0);
64  hdr.data_size = AV_RL32(buf + 4);
65  hdr.sample_rate = AV_RL32(buf + 8);
66  hdr.stereo = AV_RL16(buf + 12);
67 
68  if (hdr.sample_rate <= 0)
69  return AVERROR_INVALIDDATA;
70 
71  par = st->codecpar;
75 
76  if (hdr.stereo) {
78  par->channels = 2;
79  } else {
81  par->channels = 1;
82  }
83 
84  par->sample_rate = hdr.sample_rate;
85  par->bits_per_coded_sample = 4;
86  par->bits_per_raw_sample = 16;
87  par->block_align = 1;
88  par->bit_rate = par->channels *
89  (uint64_t)par->sample_rate *
91 
92  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
93  st->start_time = 0;
94  st->duration = hdr.data_size *
95  (8 / par->bits_per_coded_sample) /
96  par->channels;
97 
98  return 0;
99 }
100 
101 static int kvag_read_packet(AVFormatContext *s, AVPacket *pkt)
102 {
103  int ret;
104  AVCodecParameters *par = s->streams[0]->codecpar;
105 
106  if ((ret = av_get_packet(s->pb, pkt, KVAG_MAX_READ_SIZE)) < 0)
107  return ret;
108 
110  pkt->stream_index = 0;
111  pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
112 
113  return 0;
114 }
115 
116 static int kvag_seek(AVFormatContext *s, int stream_index,
117  int64_t pts, int flags)
118 {
119  if (pts != 0)
120  return AVERROR(EINVAL);
121 
122  return avio_seek(s->pb, KVAG_HEADER_SIZE, SEEK_SET);
123 }
124 
126  .name = "kvag",
127  .long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
128  .read_probe = kvag_probe,
129  .read_header = kvag_read_header,
130  .read_packet = kvag_read_packet,
131  .read_seek = kvag_seek,
132 };
133 #endif
134 
135 #if CONFIG_KVAG_MUXER
136 static int kvag_write_init(AVFormatContext *s)
137 {
138  AVCodecParameters *par;
139 
140  if (s->nb_streams != 1) {
141  av_log(s, AV_LOG_ERROR, "KVAG files have exactly one stream\n");
142  return AVERROR(EINVAL);
143  }
144 
145  par = s->streams[0]->codecpar;
146 
147  if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_SSI) {
148  av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
149  avcodec_get_name(par->codec_id));
150  return AVERROR(EINVAL);
151  }
152 
153  if (par->channels > 2) {
154  av_log(s, AV_LOG_ERROR, "KVAG files only support up to 2 channels\n");
155  return AVERROR(EINVAL);
156  }
157 
158  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
159  av_log(s, AV_LOG_WARNING, "Stream not seekable, unable to write output file\n");
160  return AVERROR(EINVAL);
161  }
162 
163  return 0;
164 }
165 
166 static int kvag_write_header(AVFormatContext *s)
167 {
169  AVCodecParameters *par = s->streams[0]->codecpar;
170 
171  AV_WL32(buf + 0, KVAG_TAG);
172  AV_WL32(buf + 4, 0); /* Data size, we fix this up later. */
173  AV_WL32(buf + 8, par->sample_rate);
174  AV_WL16(buf + 12, par->channels == 2);
175 
176  avio_write(s->pb, buf, sizeof(buf));
177  return 0;
178 }
179 
180 static int kvag_write_trailer(AVFormatContext *s)
181 {
182  int64_t file_size, data_size;
183 
184  file_size = avio_tell(s->pb);
185  data_size = file_size - KVAG_HEADER_SIZE;
186  if (data_size < UINT32_MAX) {
187  avio_seek(s->pb, 4, SEEK_SET);
188  avio_wl32(s->pb, (uint32_t)data_size);
189  avio_seek(s->pb, file_size, SEEK_SET);
190  } else {
192  "Filesize %"PRId64" invalid for KVAG, output file will be broken\n",
193  file_size);
194  }
195 
196  return 0;
197 }
198 
200  .name = "kvag",
201  .long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
202  .extensions = "vag",
203  .audio_codec = AV_CODEC_ID_ADPCM_IMA_SSI,
204  .video_codec = AV_CODEC_ID_NONE,
205  .init = kvag_write_init,
206  .write_header = kvag_write_header,
207  .write_packet = ff_raw_write_packet,
208  .write_trailer = kvag_write_trailer
209 };
210 #endif
AVInputFormat ff_kvag_demuxer
AVOutputFormat ff_kvag_muxer
uint8_t
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
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:310
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:375
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RL32
Definition: intreadwrite.h:146
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:477
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition: codec_id.h:397
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:411
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define KVAG_MAX_READ_SIZE
Definition: kvag.c:29
#define KVAG_TAG
Definition: kvag.c:27
#define KVAG_HEADER_SIZE
Definition: kvag.c:28
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: codec_par.h:115
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
uint32_t magic
Definition: kvag.c:32
uint32_t data_size
Definition: kvag.c:33
int sample_rate
Definition: kvag.c:34
uint16_t stereo
Definition: kvag.c:35
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
static int64_t pts