changeset 2701:25028505671c libavformat

add support for demuxing Electronic Arts .cdata files based on a patch from Peter Ross
author aurel
date Sat, 03 Nov 2007 22:42:20 +0000
parents dfcac0102c34
children 6a7b82888880
files Makefile allformats.c eacdata.c
diffstat 3 files changed, 102 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Nov 03 18:29:16 2007 +0000
+++ b/Makefile	Sat Nov 03 22:42:20 2007 +0000
@@ -41,6 +41,7 @@
 OBJS-$(CONFIG_DV_MUXER)                  += dvenc.o
 OBJS-$(CONFIG_DV1394_DEMUXER)            += dv1394.o dv.o
 OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o riff.o
+OBJS-$(CONFIG_EA_CDATA_DEMUXER)          += eacdata.o
 OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
 OBJS-$(CONFIG_FFM_DEMUXER)               += ffm.o
 OBJS-$(CONFIG_FFM_MUXER)                 += ffm.o
--- a/allformats.c	Sat Nov 03 18:29:16 2007 +0000
+++ b/allformats.c	Sat Nov 03 22:42:20 2007 +0000
@@ -74,6 +74,7 @@
     REGISTER_DEMUXER  (DV1394, dv1394);
     REGISTER_DEMUXER  (DXA, dxa);
     REGISTER_DEMUXER  (EA, ea);
+    REGISTER_DEMUXER  (EA_CDATA, ea_cdata);
     REGISTER_MUXDEMUX (FFM, ffm);
     REGISTER_MUXDEMUX (FLAC, flac);
     REGISTER_DEMUXER  (FLIC, flic);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eacdata.c	Sat Nov 03 22:42:20 2007 +0000
@@ -0,0 +1,100 @@
+/*
+ * Electronic Arts .cdata file Demuxer
+ * Copyright (c) 2007 Peter Ross
+ *
+ * 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
+ */
+
+/**
+ * @file eacdata.c
+ * Electronic Arts cdata Format Demuxer
+ * by Peter Ross (suxen_drol at hotmail dot com)
+ *
+ * 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;
+    ByteIOContext *pb = &s->pb;
+    unsigned int sample_rate, header;
+    AVStream *st;
+
+    header = get_be16(pb);
+    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;
+    };
+
+    sample_rate = get_be16(pb);
+    url_fskip(pb, 12);
+
+    st = av_new_stream(s, 0);
+    if (!st)
+        return AVERROR(ENOMEM);
+    st->codec->codec_type = CODEC_TYPE_AUDIO;
+    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;
+
+    if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
+        return AVERROR(EIO);
+    pkt->pts = cdata->audio_pts++;
+    return 1;
+}
+
+AVInputFormat ea_cdata_demuxer = {
+    "ea_cdata",
+    "Electronic Arts cdata",
+    sizeof(CdataDemuxContext),
+    cdata_probe,
+    cdata_read_header,
+    cdata_read_packet,
+    .extensions = "cdata",
+};