3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
26 typedef struct VAGDemuxContext {
30 static int vag_probe(AVProbeData *p)
32 if (memcmp(p->buf, "VAGp\0\0\0", 7))
35 return AVPROBE_SCORE_MAX;
38 static int vag_read_header(AVFormatContext *s)
40 VAGDemuxContext *c = s->priv_data;
43 st = avformat_new_stream(s, NULL);
45 return AVERROR(ENOMEM);
48 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
49 st->codec->codec_id = AV_CODEC_ID_ADPCM_PSX;
50 st->codec->channels = 1 + (avio_rb32(s->pb) == 0x00000004);
52 c->data_end = avio_rb32(s->pb);
53 st->duration = (c->data_end - avio_tell(s->pb)) / (16 * st->codec->channels) * 28;
54 st->codec->sample_rate = avio_rb32(s->pb);
55 if (st->codec->sample_rate <= 0)
56 return AVERROR_INVALIDDATA;
57 st->codec->block_align = 16 * st->codec->channels;
59 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
64 static int vag_read_packet(AVFormatContext *s, AVPacket *pkt)
66 VAGDemuxContext *c = s->priv_data;
67 AVCodecContext *codec = s->streams[0]->codec;
70 size = FFMIN(c->data_end - avio_tell(s->pb), codec->block_align);
74 return av_get_packet(s->pb, pkt, size);
77 AVInputFormat ff_vag_demuxer = {
79 .long_name = NULL_IF_CONFIG_SMALL("Sony VAG"),
80 .priv_data_size = sizeof(VAGDemuxContext),
81 .read_probe = vag_probe,
82 .read_header = vag_read_header,
83 .read_packet = vag_read_packet,