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;
34 uint32_t sample_rate;
35 uint16_t stereo;
36 } KVAGHeader;
37
38 #if CONFIG_KVAG_DEMUXER
kvag_probe(const AVProbeData * p)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
kvag_read_header(AVFormatContext * s)47 static int kvag_read_header(AVFormatContext *s)
48 {
49 int ret;
50 AVStream *st;
51 KVAGHeader hdr;
52 AVCodecParameters *par;
53 uint8_t buf[KVAG_HEADER_SIZE];
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 par = st->codecpar;
69 par->codec_type = AVMEDIA_TYPE_AUDIO;
70 par->codec_id = AV_CODEC_ID_ADPCM_IMA_SSI;
71 par->format = AV_SAMPLE_FMT_S16;
72
73 if (hdr.stereo) {
74 par->channel_layout = AV_CH_LAYOUT_STEREO;
75 par->channels = 2;
76 } else {
77 par->channel_layout = AV_CH_LAYOUT_MONO;
78 par->channels = 1;
79 }
80
81 par->sample_rate = hdr.sample_rate;
82 par->bits_per_coded_sample = 4;
83 par->bits_per_raw_sample = 16;
84 par->block_align = 1;
85 par->bit_rate = par->channels *
86 (uint64_t)par->sample_rate *
87 par->bits_per_coded_sample;
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) /
93 par->channels;
94
95 return 0;
96 }
97
kvag_read_packet(AVFormatContext * s,AVPacket * pkt)98 static 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->channels;
109
110 return 0;
111 }
112
kvag_seek(AVFormatContext * s,int stream_index,int64_t pts,int flags)113 static 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
122 AVInputFormat ff_kvag_demuxer = {
123 .name = "kvag",
124 .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
kvag_write_init(AVFormatContext * s)133 static int kvag_write_init(AVFormatContext *s)
134 {
135 AVCodecParameters *par;
136
137 if (s->nb_streams != 1) {
138 av_log(s, AV_LOG_ERROR, "KVAG files have exactly one stream\n");
139 return AVERROR(EINVAL);
140 }
141
142 par = s->streams[0]->codecpar;
143
144 if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_SSI) {
145 av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
146 avcodec_get_name(par->codec_id));
147 return AVERROR(EINVAL);
148 }
149
150 if (par->channels > 2) {
151 av_log(s, AV_LOG_ERROR, "KVAG files only support up to 2 channels\n");
152 return AVERROR(EINVAL);
153 }
154
155 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
156 av_log(s, AV_LOG_WARNING, "Stream not seekable, unable to write output file\n");
157 return AVERROR(EINVAL);
158 }
159
160 return 0;
161 }
162
kvag_write_header(AVFormatContext * s)163 static int kvag_write_header(AVFormatContext *s)
164 {
165 uint8_t buf[KVAG_HEADER_SIZE];
166 AVCodecParameters *par = s->streams[0]->codecpar;
167
168 AV_WL32(buf + 0, KVAG_TAG);
169 AV_WL32(buf + 4, 0); /* Data size, we fix this up later. */
170 AV_WL32(buf + 8, par->sample_rate);
171 AV_WL16(buf + 12, par->channels == 2);
172
173 avio_write(s->pb, buf, sizeof(buf));
174 return 0;
175 }
176
kvag_write_trailer(AVFormatContext * s)177 static int kvag_write_trailer(AVFormatContext *s)
178 {
179 int64_t file_size, data_size;
180
181 file_size = avio_tell(s->pb);
182 data_size = file_size - KVAG_HEADER_SIZE;
183 if (data_size < UINT32_MAX) {
184 avio_seek(s->pb, 4, SEEK_SET);
185 avio_wl32(s->pb, (uint32_t)data_size);
186 avio_seek(s->pb, file_size, SEEK_SET);
187 } else {
188 av_log(s, AV_LOG_WARNING,
189 "Filesize %"PRId64" invalid for KVAG, output file will be broken\n",
190 file_size);
191 }
192
193 return 0;
194 }
195
196 AVOutputFormat ff_kvag_muxer = {
197 .name = "kvag",
198 .long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
199 .extensions = "vag",
200 .audio_codec = AV_CODEC_ID_ADPCM_IMA_SSI,
201 .video_codec = AV_CODEC_ID_NONE,
202 .init = kvag_write_init,
203 .write_header = kvag_write_header,
204 .write_packet = ff_raw_write_packet,
205 .write_trailer = kvag_write_trailer
206 };
207 #endif
208