Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
967k
#define ISS_SIG "IMA_ADPCM_Sound"
36
967k
#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
382
{
46
382
    int i = 0;
47
382
    char c;
48
49
7.71k
    while ((c = avio_r8(s))) {
50
7.47k
        if(c == ' ')
51
142
            break;
52
7.33k
        if (i < maxlen-1)
53
3.23k
            buf[i++] = c;
54
7.33k
    }
55
56
382
    if(!c)
57
240
        avio_r8(s);
58
59
382
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
60
382
}
61
62
static int iss_probe(const AVProbeData *p)
63
967k
{
64
967k
    if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
65
967k
        return 0;
66
67
620
    return AVPROBE_SCORE_MAX;
68
967k
}
69
70
static av_cold int iss_read_header(AVFormatContext *s)
71
62
{
72
62
    IssDemuxContext *iss = s->priv_data;
73
62
    AVIOContext *pb = s->pb;
74
62
    AVStream *st;
75
62
    char token[MAX_TOKEN_SIZE];
76
62
    int stereo, rate_divisor;
77
78
62
    get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
79
62
    get_token(pb, token, sizeof(token)); //packet size
80
62
    if (sscanf(token, "%d", &iss->packet_size) != 1) {
81
22
        av_log(s, AV_LOG_ERROR, "Failed parsing packet size\n");
82
22
        return AVERROR_INVALIDDATA;
83
22
    }
84
40
    get_token(pb, token, sizeof(token)); //File ID
85
40
    get_token(pb, token, sizeof(token)); //out size
86
40
    get_token(pb, token, sizeof(token)); //stereo
87
40
    if (sscanf(token, "%d", &stereo) != 1) {
88
10
        av_log(s, AV_LOG_ERROR, "Failed parsing stereo flag\n");
89
10
        return AVERROR_INVALIDDATA;
90
10
    }
91
30
    get_token(pb, token, sizeof(token)); //Unknown1
92
30
    get_token(pb, token, sizeof(token)); //RateDivisor
93
30
    if (sscanf(token, "%d", &rate_divisor) != 1) {
94
4
        av_log(s, AV_LOG_ERROR, "Failed parsing rate_divisor\n");
95
4
        return AVERROR_INVALIDDATA;
96
4
    }
97
26
    get_token(pb, token, sizeof(token)); //Unknown2
98
26
    get_token(pb, token, sizeof(token)); //Version ID
99
26
    get_token(pb, token, sizeof(token)); //Size
100
101
26
    if (iss->packet_size <= 0) {
102
1
        av_log(s, AV_LOG_ERROR, "packet_size %d is invalid\n", iss->packet_size);
103
1
        return AVERROR_INVALIDDATA;
104
1
    }
105
106
25
    iss->sample_start_pos = avio_tell(pb);
107
108
25
    st = avformat_new_stream(s, NULL);
109
25
    if (!st)
110
0
        return AVERROR(ENOMEM);
111
25
    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
112
25
    st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS;
113
114
25
    if (stereo) {
115
23
        st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
116
23
    } else {
117
2
        st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
118
2
    }
119
120
25
    st->codecpar->sample_rate = 44100;
121
25
    if(rate_divisor > 0)
122
13
         st->codecpar->sample_rate /= rate_divisor;
123
25
    st->codecpar->bits_per_coded_sample = 4;
124
25
    st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels *
125
25
                             st->codecpar->sample_rate *
126
25
                             st->codecpar->bits_per_coded_sample;
127
25
    st->codecpar->block_align = iss->packet_size;
128
25
    avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
129
130
25
    return 0;
131
25
}
132
133
static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
134
1.70k
{
135
1.70k
    IssDemuxContext *iss = s->priv_data;
136
1.70k
    int ret = av_get_packet(s->pb, pkt, iss->packet_size);
137
138
1.70k
    if(ret != iss->packet_size)
139
32
        return AVERROR_INVALIDDATA;
140
141
1.66k
    pkt->stream_index = 0;
142
1.66k
    pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
143
1.66k
    if (s->streams[0]->codecpar->ch_layout.nb_channels > 0)
144
1.66k
        pkt->pts /= s->streams[0]->codecpar->ch_layout.nb_channels * 2;
145
1.66k
    return 0;
146
1.70k
}
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
};