eacdata.c 2.78 KB
Newer Older
1 2 3 4
/*
 * Electronic Arts .cdata file Demuxer
 * Copyright (c) 2007 Peter Ross
 *
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
 * Electronic Arts cdata Format Demuxer
Peter Ross's avatar
Peter Ross committed
25
 * by Peter Ross (pross@xvid.org)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 * Technical details here:
 *  http://wiki.multimedia.cx/index.php?title=EA_Command_And_Conquer_3_Audio_Codec
 */

#include "avformat.h"

typedef struct {
  unsigned int channels;
  unsigned int audio_pts;
} CdataDemuxContext;

static int cdata_probe(AVProbeData *p)
{
    const uint8_t *b = p->buf;

    if (b[0] == 0x04 && (b[1] == 0x00 || b[1] == 0x04 || b[1] == 0x0C))
        return AVPROBE_SCORE_MAX/8;
    return 0;
}

static int cdata_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
    CdataDemuxContext *cdata = s->priv_data;
50
    AVIOContext *pb = s->pb;
51 52 53
    unsigned int sample_rate, header;
    AVStream *st;

54
    header = avio_rb16(pb);
55 56 57 58 59 60 61 62 63
    switch (header) {
        case 0x0400: cdata->channels = 1; break;
        case 0x0404: cdata->channels = 2; break;
        case 0x040C: cdata->channels = 4; break;
        default:
            av_log(s, AV_LOG_INFO, "unknown header 0x%04x\n", header);
            return -1;
    };

64
    sample_rate = avio_rb16(pb);
65
    avio_skip(pb, 12);
66 67 68 69

    st = av_new_stream(s, 0);
    if (!st)
        return AVERROR(ENOMEM);
70
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    st->codec->codec_tag = 0; /* no fourcc */
    st->codec->codec_id = CODEC_ID_ADPCM_EA_XAS;
    st->codec->channels = cdata->channels;
    st->codec->sample_rate = sample_rate;
    av_set_pts_info(st, 64, 1, sample_rate);

    cdata->audio_pts = 0;
    return 0;
}

static int cdata_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    CdataDemuxContext *cdata = s->priv_data;
    int packet_size = 76*cdata->channels;

86 87 88
    int ret = av_get_packet(s->pb, pkt, packet_size);
    if (ret < 0)
        return ret;
89
    pkt->pts = cdata->audio_pts++;
90
    return 0;
91 92
}

93
AVInputFormat ff_ea_cdata_demuxer = {
94
    "ea_cdata",
95
    NULL_IF_CONFIG_SMALL("Electronic Arts cdata"),
96 97 98 99 100 101
    sizeof(CdataDemuxContext),
    cdata_probe,
    cdata_read_header,
    cdata_read_packet,
    .extensions = "cdata",
};