OSDN Git Service

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