]> git.sesse.net Git - ffmpeg/blob - libavformat/vag.c
Merge commit '9ef748173a4e0e58d40afaf38397783cd2537eaa'
[ffmpeg] / libavformat / vag.c
1 /*
2  * VAG demuxer
3  * Copyright (c) 2015 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/channel_layout.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 typedef struct VAGDemuxContext {
27     int64_t data_end;
28 } VAGDemuxContext;
29
30 static int vag_probe(AVProbeData *p)
31 {
32     if (memcmp(p->buf, "VAGp\0\0\0", 7))
33         return 0;
34
35     return AVPROBE_SCORE_MAX;
36 }
37
38 static int vag_read_header(AVFormatContext *s)
39 {
40     VAGDemuxContext *c = s->priv_data;
41     AVStream *st;
42
43     st = avformat_new_stream(s, NULL);
44     if (!st)
45         return AVERROR(ENOMEM);
46
47     avio_skip(s->pb, 4);
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);
51     avio_skip(s->pb, 4);
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;
58     avio_skip(s->pb, 28);
59     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
60
61     return 0;
62 }
63
64 static int vag_read_packet(AVFormatContext *s, AVPacket *pkt)
65 {
66     VAGDemuxContext *c = s->priv_data;
67     AVCodecContext *codec = s->streams[0]->codec;
68     int size;
69
70     size = FFMIN(c->data_end - avio_tell(s->pb), codec->block_align);
71     if (size <= 0)
72         return AVERROR_EOF;
73
74     return av_get_packet(s->pb, pkt, size);
75 }
76
77 AVInputFormat ff_vag_demuxer = {
78     .name           = "vag",
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,
84     .extensions     = "vag",
85 };