[FFmpeg-devel] [PATCH 3/3] RedSpark demuxer

James Almer jamrial at gmail.com
Thu Apr 18 03:28:03 CEST 2013


Signed-off-by: James Almer <jamrial at gmail.com>
---
 Changelog                |   1 +
 doc/general.texi         |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/redspark.c   | 189 +++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   2 +-
 6 files changed, 194 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/redspark.c

diff --git a/Changelog b/Changelog
index ecf7143..4bc554f 100644
--- a/Changelog
+++ b/Changelog
@@ -24,6 +24,7 @@ version <next>:
 - smptehdbars source
 - inverse telecine filters (fieldmatch and decimate)
 - RSD demuxer
+- RedSpark demuxer
 
 
 version 1.2:
diff --git a/doc/general.texi b/doc/general.texi
index af4ec74..47aef86 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -346,6 +346,7 @@ library:
 @item RDT                       @tab   @tab X
 @item REDCODE R3D               @tab   @tab X
     @tab File format used by RED Digital cameras, contains JPEG 2000 frames and PCM audio.
+ at item RedSpark                  @tab   @tab X
 @item RealMedia                 @tab X @tab X
 @item Redirector                @tab   @tab X
 @item Renderware TeXture Dictionary @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index dbc0e41..eda9f2d 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -304,6 +304,7 @@ OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
 OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += rawvideodec.o
 OBJS-$(CONFIG_RAWVIDEO_MUXER)            += rawenc.o
 OBJS-$(CONFIG_REALTEXT_DEMUXER)          += realtextdec.o subtitles.o
+OBJS-$(CONFIG_REDSPARK_DEMUXER)          += redspark.o
 OBJS-$(CONFIG_RL2_DEMUXER)               += rl2.o
 OBJS-$(CONFIG_RM_DEMUXER)                += rmdec.o rm.o rmsipr.o
 OBJS-$(CONFIG_RM_MUXER)                  += rmenc.o rm.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index e18351b..8d134f2 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -230,6 +230,7 @@ void av_register_all(void)
     REGISTER_DEMUXER (R3D,              r3d);
     REGISTER_MUXDEMUX(RAWVIDEO,         rawvideo);
     REGISTER_DEMUXER (REALTEXT,         realtext);
+    REGISTER_DEMUXER (REDSPARK,         redspark);
     REGISTER_DEMUXER (RL2,              rl2);
     REGISTER_MUXDEMUX(RM,               rm);
     REGISTER_MUXDEMUX(ROQ,              roq);
diff --git a/libavformat/redspark.c b/libavformat/redspark.c
new file mode 100644
index 0000000..c12ca7b
--- /dev/null
+++ b/libavformat/redspark.c
@@ -0,0 +1,189 @@
+/*
+ * RedSpark demuxer
+ * Copyright (c) 2013 James Almer
+ *
+ * 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 "libavcodec/bytestream.h"
+#include "libavutil/intreadwrite.h"
+#include "avformat.h"
+#include "avio.h"
+#include "internal.h"
+
+#define HEADER_SIZE 4096
+
+typedef struct RedSparkContext {
+    int64_t     filesize;
+    uint8_t     *table;
+} RedSparkContext;
+
+static int redspark_probe(AVProbeData *p)
+{
+    uint32_t key, data;
+    uint8_t header[8];
+
+    /* Decrypt first 8 bytes of the header */
+    data = AV_RB32(p->buf);
+    data = data ^ (key = data ^ 0x52656453);
+    AV_WB32(header, data);
+    key = (key << 11) | (key >> 21);
+
+    data = AV_RB32(p->buf + 4) ^ (((key << 3) | (key >> 29)) + key);
+    AV_WB32(header + 4, data);
+
+    if (!memcmp(header, "RedSpark", 8))
+        return AVPROBE_SCORE_MAX;
+
+    return 0;
+}
+
+static int redspark_close(AVFormatContext *s)
+{
+    RedSparkContext *redspark = s->priv_data;
+
+    av_freep(&redspark->table);
+
+    return 0;
+}
+
+static int redspark_read_header(AVFormatContext *s)
+{
+    AVIOContext *pb = s->pb;
+    RedSparkContext *redspark = s->priv_data;
+    AVCodecContext *codec;
+    GetByteContext gbc;
+    int i, coef_off, ret = 0;
+    uint32_t key, data;
+    uint8_t *header, *pbc;
+    AVStream *st;
+
+    redspark->filesize = avio_size(pb);
+    if (redspark->filesize < HEADER_SIZE) {
+        av_log(s, AV_LOG_ERROR, "Header too small\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    codec = st->codec;
+
+    header = av_malloc(HEADER_SIZE);
+    if (!header)
+        return AVERROR(ENOMEM);
+    pbc = header;
+
+    /* Decrypt header */
+    data = avio_rb32(pb);
+    data = data ^ (key = data ^ 0x52656453);
+    bytestream_put_be32(&pbc, data);
+    key = (key << 11) | (key >> 21);
+
+    for (i = 0; i < HEADER_SIZE - 4; i += 4) {
+        data = avio_rb32(pb) ^ (key = ((key << 3) | (key >> 29)) + key);
+        bytestream_put_be32(&pbc, data);
+    }
+
+    codec->codec_id    = AV_CODEC_ID_ADPCM_THP;
+    codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+
+    bytestream2_init(&gbc, header, HEADER_SIZE);
+    bytestream2_seek(&gbc, 0x3c, SEEK_SET);
+    codec->sample_rate = bytestream2_get_be32u(&gbc);
+    if (codec->sample_rate <= 0 || codec->sample_rate > 96000) {
+        av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", codec->sample_rate);
+        ret = AVERROR_INVALIDDATA;
+        goto end;
+    }
+
+    st->duration = bytestream2_get_be32u(&gbc) * 14;
+
+    bytestream2_skipu(&gbc, 10);
+    codec->channels = bytestream2_get_byteu(&gbc);
+    if (!codec->channels) {
+        ret = AVERROR_INVALIDDATA;
+        goto end;
+    }
+
+    coef_off = 0x54 + codec->channels * 8;
+    if (bytestream2_get_byteu(&gbc)) // Loop flag
+        coef_off += 16;
+
+    redspark->table = av_malloc(32 * codec->channels);
+    if (!redspark->table) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    /* Get the ADPCM table */
+    bytestream2_seek(&gbc, coef_off, SEEK_SET);
+    for (i = 0; i < codec->channels; i++) {
+        if (bytestream2_get_bufferu(&gbc, redspark->table + i * 32, 32) != 32) {
+            ret = AVERROR_INVALIDDATA;
+            redspark_close(s);
+            goto end;
+        }
+        bytestream2_skipu(&gbc, 14);
+    }
+
+    avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
+
+end:
+    av_freep(&header);
+
+    return ret;
+}
+
+static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVCodecContext *codec = s->streams[0]->codec;
+    RedSparkContext *redspark = s->priv_data;
+    uint32_t size = FFMIN(redspark->filesize - avio_tell(s->pb), 8 * codec->channels);
+    int ret;
+
+    if (url_feof(s->pb) || size <= 0)
+        return AVERROR_EOF;
+
+    if ((ret = av_new_packet(pkt, size)) < 0)
+        return ret;
+    if (!av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, 32 * codec->channels))
+        return AVERROR(ENOMEM);
+
+    memcpy(pkt->side_data[0].data, redspark->table, 32 * codec->channels);
+    ret = avio_read(s->pb, pkt->data, size);
+    if (ret != size) {
+        av_free_packet(pkt);
+        return AVERROR(EIO);
+    }
+
+    pkt->duration = size / (8 * codec->channels) * 14;
+    pkt->stream_index = 0;
+
+    return ret;
+}
+
+AVInputFormat ff_redspark_demuxer = {
+    .name           =   "redspark",
+    .long_name      =   NULL_IF_CONFIG_SMALL("RedSpark"),
+    .priv_data_size =   sizeof(RedSparkContext),
+    .read_probe     =   redspark_probe,
+    .read_header    =   redspark_read_header,
+    .read_packet    =   redspark_read_packet,
+    .read_close     =   redspark_close,
+    .extensions     =   "rsd",
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 8e323ce..0798073 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 55
-#define LIBAVFORMAT_VERSION_MINOR  3
+#define LIBAVFORMAT_VERSION_MINOR  4
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
1.8.1.msysgit.1




More information about the ffmpeg-devel mailing list