fsb.c 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * FSB demuxer
 * Copyright (c) 2015 Paul B Mahol
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/avassert.h"
#include "libavutil/intreadwrite.h"
#include "avformat.h"
#include "avio.h"
#include "internal.h"

static int fsb_probe(AVProbeData *p)
{
    if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5)
        return 0;
    if (AV_RL32(p->buf + 4) != 1)
        return 0;
    return AVPROBE_SCORE_MAX;
}

static int fsb_read_header(AVFormatContext *s)
{
    AVIOContext *pb = s->pb;
    unsigned format, version, c;
    int64_t offset;
42
    AVCodecParameters *par;
43 44 45 46 47 48 49 50 51 52 53 54 55
    AVStream *st = avformat_new_stream(s, NULL);

    avio_skip(pb, 3); // "FSB"
    version = avio_r8(pb) - '0';
    if (version != 4 && version != 3) {
        avpriv_request_sample(s, "version %d", version);
        return AVERROR_PATCHWELCOME;
    }

    avio_skip(pb, 4);

    if (!st)
        return AVERROR(ENOMEM);
56 57 58
    par = st->codecpar;
    par->codec_type  = AVMEDIA_TYPE_AUDIO;
    par->codec_tag   = 0;
59 60 61 62 63 64 65

    if (version == 3) {
        offset = avio_rl32(pb) + 0x18;
        avio_skip(pb, 44);
        st->duration = avio_rl32(pb);
        avio_skip(pb, 12);
        format = avio_rl32(pb);
66 67
        par->sample_rate = avio_rl32(pb);
        if (par->sample_rate <= 0)
68 69
            return AVERROR_INVALIDDATA;
        avio_skip(pb, 6);
70 71
        par->channels    = avio_rl16(pb);
        if (!par->channels)
72 73 74
            return AVERROR_INVALIDDATA;

        if (format & 0x00000100) {
75 76
            par->codec_id    = AV_CODEC_ID_PCM_S16LE;
            par->block_align = 4096 * par->channels;
77
        } else if (format & 0x00400000) {
78 79 80
            par->bits_per_coded_sample = 4;
            par->codec_id    = AV_CODEC_ID_ADPCM_IMA_WAV;
            par->block_align = 36 * par->channels;
81
        } else if (format & 0x00800000) {
82 83
            par->codec_id    = AV_CODEC_ID_ADPCM_PSX;
            par->block_align = 16 * par->channels;
84
        } else if (format & 0x02000000) {
85 86 87
            par->codec_id    = AV_CODEC_ID_ADPCM_THP;
            par->block_align = 8 * par->channels;
            if (par->channels > INT_MAX / 32)
88
                return AVERROR_INVALIDDATA;
89 90
            ff_alloc_extradata(par, 32 * par->channels);
            if (!par->extradata)
91 92
                return AVERROR(ENOMEM);
            avio_seek(pb, 0x68, SEEK_SET);
93 94
            for (c = 0; c < par->channels; c++) {
                avio_read(pb, par->extradata + 32 * c, 32);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                avio_skip(pb, 14);
            }
        } else {
            avpriv_request_sample(s, "format 0x%X", format);
            return AVERROR_PATCHWELCOME;
        }
    } else if (version == 4) {
        offset = avio_rl32(pb) + 0x30;
        avio_skip(pb, 80);
        st->duration = avio_rl32(pb);

        format = avio_rb32(pb);
        switch(format) {
        case 0x40001001:
        case 0x00001005:
        case 0x40001081:
        case 0x40200001:
112
            par->codec_id = AV_CODEC_ID_XMA2;
113 114
            break;
        case 0x40000802:
115
            par->codec_id = AV_CODEC_ID_ADPCM_THP;
116 117 118 119 120 121
            break;
        default:
            avpriv_request_sample(s, "format 0x%X", format);
            return AVERROR_PATCHWELCOME;
        }

122 123
        par->sample_rate = avio_rl32(pb);
        if (par->sample_rate <= 0)
124 125 126
            return AVERROR_INVALIDDATA;
        avio_skip(pb, 6);

127 128
        par->channels    = avio_rl16(pb);
        if (!par->channels)
129 130
            return AVERROR_INVALIDDATA;

131
        switch (par->codec_id) {
132
        case AV_CODEC_ID_XMA2:
133 134
            ff_alloc_extradata(par, 34);
            if (!par->extradata)
135
                return AVERROR(ENOMEM);
136 137
            memset(par->extradata, 0, 34);
            par->block_align = 2048;
138 139
            break;
        case AV_CODEC_ID_ADPCM_THP:
140
            if (par->channels > INT_MAX / 32)
141
                return AVERROR_INVALIDDATA;
142 143
            ff_alloc_extradata(par, 32 * par->channels);
            if (!par->extradata)
144 145
                return AVERROR(ENOMEM);
            avio_seek(pb, 0x80, SEEK_SET);
146 147
            for (c = 0; c < par->channels; c++) {
                avio_read(pb, par->extradata + 32 * c, 32);
148 149
                avio_skip(pb, 14);
            }
150
            par->block_align = 8 * par->channels;
151 152 153 154 155 156 157 158 159
            break;
        }
    } else {
        av_assert0(0);
    }

    avio_skip(pb, offset - avio_tell(pb));
    s->internal->data_offset = avio_tell(pb);

160
    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
161 162 163 164 165 166

    return 0;
}

static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
{
167
    AVCodecParameters *par = s->streams[0]->codecpar;
168 169 170 171 172 173 174
    int64_t pos;
    int ret;

    if (avio_feof(s->pb))
        return AVERROR_EOF;

    pos = avio_tell(s->pb);
175 176
    if (par->codec_id == AV_CODEC_ID_ADPCM_THP &&
               par->channels > 1) {
177 178
        int i, ch;

179
        ret = av_new_packet(pkt, par->block_align);
180 181 182
        if (ret < 0)
            return ret;
        for (i = 0; i < 4; i++) {
183
            for (ch = 0; ch < par->channels; ch++) {
184 185 186 187 188 189
                pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
                pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
            }
        }
        ret = 0;
    } else {
190
        ret = av_get_packet(s->pb, pkt, par->block_align);
191 192
    }

193
    if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        pkt->duration = (pkt->data[0] >> 2) * 512;

    pkt->pos = pos;
    pkt->stream_index = 0;

    return ret;
}

AVInputFormat ff_fsb_demuxer = {
    .name        = "fsb",
    .long_name   = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"),
    .read_probe  = fsb_probe,
    .read_header = fsb_read_header,
    .read_packet = fsb_read_packet,
    .extensions  = "fsb",
    .flags       = AVFMT_GENERIC_INDEX,
};