iss.c 4.14 KB
Newer Older
1 2 3 4
/*
 * ISS (.iss) file demuxer
 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25
 * Funcom ISS file demuxer
 * @author Jaikrishnan Menon
26
 * @see http://wiki.multimedia.cx/index.php?title=FunCom_ISS
27 28
 */

Justin Ruggles's avatar
Justin Ruggles committed
29
#include "libavutil/channel_layout.h"
30
#include "avformat.h"
31
#include "internal.h"
32 33 34 35 36 37
#include "libavutil/avstring.h"

#define ISS_SIG "IMA_ADPCM_Sound"
#define ISS_SIG_LEN 15
#define MAX_TOKEN_SIZE 20

38
typedef struct IssDemuxContext {
39 40 41 42
    int packet_size;
    int sample_start_pos;
} IssDemuxContext;

43
static void get_token(AVIOContext *s, char *buf, int maxlen)
44 45 46 47
{
    int i = 0;
    char c;

48
    while ((c = avio_r8(s))) {
49 50 51 52 53 54
        if(c == ' ')
            break;
        if (i < maxlen-1)
            buf[i++] = c;
    }

55
    if(!c)
56
        avio_r8(s);
57

58 59 60 61 62 63 64 65 66 67 68
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
}

static int iss_probe(AVProbeData *p)
{
    if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
        return 0;

    return AVPROBE_SCORE_MAX;
}

69
static av_cold int iss_read_header(AVFormatContext *s)
70 71
{
    IssDemuxContext *iss = s->priv_data;
72
    AVIOContext *pb = s->pb;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    AVStream *st;
    char token[MAX_TOKEN_SIZE];
    int stereo, rate_divisor;

    get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
    get_token(pb, token, sizeof(token)); //packet size
    sscanf(token, "%d", &iss->packet_size);
    get_token(pb, token, sizeof(token)); //File ID
    get_token(pb, token, sizeof(token)); //out size
    get_token(pb, token, sizeof(token)); //stereo
    sscanf(token, "%d", &stereo);
    get_token(pb, token, sizeof(token)); //Unknown1
    get_token(pb, token, sizeof(token)); //RateDivisor
    sscanf(token, "%d", &rate_divisor);
    get_token(pb, token, sizeof(token)); //Unknown2
    get_token(pb, token, sizeof(token)); //Version ID
    get_token(pb, token, sizeof(token)); //Size

91
    iss->sample_start_pos = avio_tell(pb);
92

93
    st = avformat_new_stream(s, NULL);
94 95
    if (!st)
        return AVERROR(ENOMEM);
96 97
    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
    st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS;
Justin Ruggles's avatar
Justin Ruggles committed
98
    if (stereo) {
99 100
        st->codecpar->channels       = 2;
        st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
Justin Ruggles's avatar
Justin Ruggles committed
101
    } else {
102 103
        st->codecpar->channels       = 1;
        st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
Justin Ruggles's avatar
Justin Ruggles committed
104
    }
105
    st->codecpar->sample_rate = 44100;
106
    if(rate_divisor > 0)
107 108 109 110 111 112
         st->codecpar->sample_rate /= rate_divisor;
    st->codecpar->bits_per_coded_sample = 4;
    st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate
                                      * st->codecpar->bits_per_coded_sample;
    st->codecpar->block_align = iss->packet_size;
    avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
113 114 115 116 117 118 119 120 121

    return 0;
}

static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    IssDemuxContext *iss = s->priv_data;
    int ret = av_get_packet(s->pb, pkt, iss->packet_size);

122 123
    if(ret != iss->packet_size)
        return AVERROR(EIO);
124 125

    pkt->stream_index = 0;
126
    pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
127 128
    if(s->streams[0]->codecpar->channels > 0)
        pkt->pts /= s->streams[0]->codecpar->channels*2;
129 130 131
    return 0;
}

132
AVInputFormat ff_iss_demuxer = {
133
    .name           = "iss",
134
    .long_name      = NULL_IF_CONFIG_SMALL("Funcom ISS"),
135 136 137 138
    .priv_data_size = sizeof(IssDemuxContext),
    .read_probe     = iss_probe,
    .read_header    = iss_read_header,
    .read_packet    = iss_read_packet,
139
};