]> git.sesse.net Git - ffmpeg/blob - libavformat/iss.c
doxygen: place empty line between brief description and detailed description
[ffmpeg] / libavformat / iss.c
1 /*
2  * ISS (.iss) file demuxer
3  * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Funcom ISS file demuxer
25  * @author Jaikrishnan Menon
26  *
27  * For more information on the .iss file format, visit:
28  * http://wiki.multimedia.cx/index.php?title=FunCom_ISS
29  */
30
31 #include "avformat.h"
32 #include "libavutil/avstring.h"
33
34 #define ISS_SIG "IMA_ADPCM_Sound"
35 #define ISS_SIG_LEN 15
36 #define MAX_TOKEN_SIZE 20
37
38 typedef struct {
39     int packet_size;
40     int sample_start_pos;
41 } IssDemuxContext;
42
43 static void get_token(AVIOContext *s, char *buf, int maxlen)
44 {
45     int i = 0;
46     char c;
47
48     while ((c = avio_r8(s))) {
49         if(c == ' ')
50             break;
51         if (i < maxlen-1)
52             buf[i++] = c;
53     }
54
55     if(!c)
56         avio_r8(s);
57
58     buf[i] = 0; /* Ensure null terminated, but may be truncated */
59 }
60
61 static int iss_probe(AVProbeData *p)
62 {
63     if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
64         return 0;
65
66     return AVPROBE_SCORE_MAX;
67 }
68
69 static av_cold int iss_read_header(AVFormatContext *s, AVFormatParameters *ap)
70 {
71     IssDemuxContext *iss = s->priv_data;
72     AVIOContext *pb = s->pb;
73     AVStream *st;
74     char token[MAX_TOKEN_SIZE];
75     int stereo, rate_divisor;
76
77     get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
78     get_token(pb, token, sizeof(token)); //packet size
79     sscanf(token, "%d", &iss->packet_size);
80     get_token(pb, token, sizeof(token)); //File ID
81     get_token(pb, token, sizeof(token)); //out size
82     get_token(pb, token, sizeof(token)); //stereo
83     sscanf(token, "%d", &stereo);
84     get_token(pb, token, sizeof(token)); //Unknown1
85     get_token(pb, token, sizeof(token)); //RateDivisor
86     sscanf(token, "%d", &rate_divisor);
87     get_token(pb, token, sizeof(token)); //Unknown2
88     get_token(pb, token, sizeof(token)); //Version ID
89     get_token(pb, token, sizeof(token)); //Size
90
91     iss->sample_start_pos = avio_tell(pb);
92
93     st = av_new_stream(s, 0);
94     if (!st)
95         return AVERROR(ENOMEM);
96     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
97     st->codec->codec_id = CODEC_ID_ADPCM_IMA_ISS;
98     st->codec->channels = stereo ? 2 : 1;
99     st->codec->sample_rate = 44100;
100     if(rate_divisor > 0)
101          st->codec->sample_rate /= rate_divisor;
102     st->codec->bits_per_coded_sample = 4;
103     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate
104                                       * st->codec->bits_per_coded_sample;
105     st->codec->block_align = iss->packet_size;
106     av_set_pts_info(st, 32, 1, st->codec->sample_rate);
107
108     return 0;
109 }
110
111 static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
112 {
113     IssDemuxContext *iss = s->priv_data;
114     int ret = av_get_packet(s->pb, pkt, iss->packet_size);
115
116     if(ret != iss->packet_size)
117         return AVERROR(EIO);
118
119     pkt->stream_index = 0;
120     pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
121     if(s->streams[0]->codec->channels > 0)
122         pkt->pts /= s->streams[0]->codec->channels*2;
123     return 0;
124 }
125
126 AVInputFormat ff_iss_demuxer = {
127     "ISS",
128     NULL_IF_CONFIG_SMALL("Funcom ISS format"),
129     sizeof(IssDemuxContext),
130     iss_probe,
131     iss_read_header,
132     iss_read_packet,
133 };
134