2 * Simon & Schuster Interactive VAG demuxer
4 * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
26 #define KVAG_TAG MKTAG('K', 'V', 'A', 'G')
27 #define KVAG_HEADER_SIZE 14
28 #define KVAG_MAX_READ_SIZE 4096
30 typedef struct KVAGHeader {
37 static int kvag_probe(const AVProbeData *p)
39 if (AV_RL32(p->buf) != KVAG_TAG)
42 return AVPROBE_SCORE_EXTENSION + 1;
45 static int kvag_read_header(AVFormatContext *s)
50 AVCodecParameters *par;
51 uint8_t buf[KVAG_HEADER_SIZE];
53 if (!(st = avformat_new_stream(s, NULL)))
54 return AVERROR(ENOMEM);
56 if ((ret = avio_read(s->pb, buf, KVAG_HEADER_SIZE)) < 0)
58 else if (ret != KVAG_HEADER_SIZE)
61 hdr.magic = AV_RL32(buf + 0);
62 hdr.data_size = AV_RL32(buf + 4);
63 hdr.sample_rate = AV_RL32(buf + 8);
64 hdr.stereo = AV_RL16(buf + 12);
67 par->codec_type = AVMEDIA_TYPE_AUDIO;
68 par->codec_id = AV_CODEC_ID_ADPCM_IMA_SSI;
69 par->format = AV_SAMPLE_FMT_S16;
72 par->channel_layout = AV_CH_LAYOUT_STEREO;
75 par->channel_layout = AV_CH_LAYOUT_MONO;
79 par->sample_rate = hdr.sample_rate;
80 par->bits_per_coded_sample = 4;
81 par->bits_per_raw_sample = 16;
83 par->bit_rate = par->channels *
85 par->bits_per_coded_sample;
87 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
89 st->duration = hdr.data_size *
90 (8 / par->bits_per_coded_sample) /
96 static int kvag_read_packet(AVFormatContext *s, AVPacket *pkt)
99 AVCodecParameters *par = s->streams[0]->codecpar;
101 if ((ret = av_get_packet(s->pb, pkt, KVAG_MAX_READ_SIZE)) < 0)
104 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
105 pkt->stream_index = 0;
106 pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels;
111 AVInputFormat ff_kvag_demuxer = {
113 .long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
114 .read_probe = kvag_probe,
115 .read_header = kvag_read_header,
116 .read_packet = kvag_read_packet