/src/ffmpeg/libavformat/iss.c
Line | Count | Source |
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 "libavutil/channel_layout.h" |
30 | | #include "avformat.h" |
31 | | #include "demux.h" |
32 | | #include "internal.h" |
33 | | #include "libavutil/avstring.h" |
34 | | |
35 | 964k | #define ISS_SIG "IMA_ADPCM_Sound" |
36 | 964k | #define ISS_SIG_LEN 15 |
37 | | #define MAX_TOKEN_SIZE 20 |
38 | | |
39 | | typedef struct IssDemuxContext { |
40 | | int packet_size; |
41 | | int sample_start_pos; |
42 | | } IssDemuxContext; |
43 | | |
44 | | static void get_token(AVIOContext *s, char *buf, int maxlen) |
45 | 17.4k | { |
46 | 17.4k | int i = 0; |
47 | 17.4k | char c; |
48 | | |
49 | 1.10M | while ((c = avio_r8(s))) { |
50 | 1.10M | if(c == ' ') |
51 | 14.2k | break; |
52 | 1.09M | if (i < maxlen-1) |
53 | 24.8k | buf[i++] = c; |
54 | 1.09M | } |
55 | | |
56 | 17.4k | if(!c) |
57 | 3.16k | avio_r8(s); |
58 | | |
59 | 17.4k | buf[i] = 0; /* Ensure null terminated, but may be truncated */ |
60 | 17.4k | } |
61 | | |
62 | | static int iss_probe(const AVProbeData *p) |
63 | 964k | { |
64 | 964k | if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN)) |
65 | 964k | return 0; |
66 | | |
67 | 871 | return AVPROBE_SCORE_MAX; |
68 | 964k | } |
69 | | |
70 | | static av_cold int iss_read_header(AVFormatContext *s) |
71 | 1.92k | { |
72 | 1.92k | IssDemuxContext *iss = s->priv_data; |
73 | 1.92k | AVIOContext *pb = s->pb; |
74 | 1.92k | AVStream *st; |
75 | 1.92k | char token[MAX_TOKEN_SIZE]; |
76 | 1.92k | int stereo, rate_divisor; |
77 | | |
78 | 1.92k | get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound" |
79 | 1.92k | get_token(pb, token, sizeof(token)); //packet size |
80 | 1.92k | if (sscanf(token, "%d", &iss->packet_size) != 1) { |
81 | 210 | av_log(s, AV_LOG_ERROR, "Failed parsing packet size\n"); |
82 | 210 | return AVERROR_INVALIDDATA; |
83 | 210 | } |
84 | 1.71k | get_token(pb, token, sizeof(token)); //File ID |
85 | 1.71k | get_token(pb, token, sizeof(token)); //out size |
86 | 1.71k | get_token(pb, token, sizeof(token)); //stereo |
87 | 1.71k | if (sscanf(token, "%d", &stereo) != 1) { |
88 | 33 | av_log(s, AV_LOG_ERROR, "Failed parsing stereo flag\n"); |
89 | 33 | return AVERROR_INVALIDDATA; |
90 | 33 | } |
91 | 1.68k | get_token(pb, token, sizeof(token)); //Unknown1 |
92 | 1.68k | get_token(pb, token, sizeof(token)); //RateDivisor |
93 | 1.68k | if (sscanf(token, "%d", &rate_divisor) != 1) { |
94 | 9 | av_log(s, AV_LOG_ERROR, "Failed parsing rate_divisor\n"); |
95 | 9 | return AVERROR_INVALIDDATA; |
96 | 9 | } |
97 | 1.67k | get_token(pb, token, sizeof(token)); //Unknown2 |
98 | 1.67k | get_token(pb, token, sizeof(token)); //Version ID |
99 | 1.67k | get_token(pb, token, sizeof(token)); //Size |
100 | | |
101 | 1.67k | if (iss->packet_size <= 0) { |
102 | 43 | av_log(s, AV_LOG_ERROR, "packet_size %d is invalid\n", iss->packet_size); |
103 | 43 | return AVERROR_INVALIDDATA; |
104 | 43 | } |
105 | | |
106 | 1.63k | iss->sample_start_pos = avio_tell(pb); |
107 | | |
108 | 1.63k | st = avformat_new_stream(s, NULL); |
109 | 1.63k | if (!st) |
110 | 0 | return AVERROR(ENOMEM); |
111 | 1.63k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
112 | 1.63k | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS; |
113 | | |
114 | 1.63k | if (stereo) { |
115 | 648 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; |
116 | 986 | } else { |
117 | 986 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; |
118 | 986 | } |
119 | | |
120 | 1.63k | st->codecpar->sample_rate = 44100; |
121 | 1.63k | if(rate_divisor > 0) |
122 | 1.11k | st->codecpar->sample_rate /= rate_divisor; |
123 | 1.63k | st->codecpar->bits_per_coded_sample = 4; |
124 | 1.63k | st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * |
125 | 1.63k | st->codecpar->sample_rate * |
126 | 1.63k | st->codecpar->bits_per_coded_sample; |
127 | 1.63k | st->codecpar->block_align = iss->packet_size; |
128 | 1.63k | avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate); |
129 | | |
130 | 1.63k | return 0; |
131 | 1.63k | } |
132 | | |
133 | | static int iss_read_packet(AVFormatContext *s, AVPacket *pkt) |
134 | 5.35M | { |
135 | 5.35M | IssDemuxContext *iss = s->priv_data; |
136 | 5.35M | int ret = av_get_packet(s->pb, pkt, iss->packet_size); |
137 | | |
138 | 5.35M | if(ret != iss->packet_size) |
139 | 2.44k | return AVERROR(EIO); |
140 | | |
141 | 5.35M | pkt->stream_index = 0; |
142 | 5.35M | pkt->pts = avio_tell(s->pb) - iss->sample_start_pos; |
143 | 5.35M | if (s->streams[0]->codecpar->ch_layout.nb_channels > 0) |
144 | 5.35M | pkt->pts /= s->streams[0]->codecpar->ch_layout.nb_channels * 2; |
145 | 5.35M | return 0; |
146 | 5.35M | } |
147 | | |
148 | | const FFInputFormat ff_iss_demuxer = { |
149 | | .p.name = "iss", |
150 | | .p.long_name = NULL_IF_CONFIG_SMALL("Funcom ISS"), |
151 | | .priv_data_size = sizeof(IssDemuxContext), |
152 | | .read_probe = iss_probe, |
153 | | .read_header = iss_read_header, |
154 | | .read_packet = iss_read_packet, |
155 | | }; |