[FFmpeg-devel] [PATCH 3/3] RedSpark demuxer
Paul B Mahol
onemda at gmail.com
Fri May 3 11:45:55 CEST 2013
On 4/22/13, James Almer <jamrial at gmail.com> wrote:
>
> 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 | 184
> +++++++++++++++++++++++++++++++++++++++++++++++
> libavformat/version.h | 2 +-
> 6 files changed, 189 insertions(+), 1 deletion(-)
> create mode 100644 libavformat/redspark.c
>
> diff --git a/Changelog b/Changelog
> index 912110d..ed20459 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -31,6 +31,7 @@ version <next>:
> - asetrate filter
> - interleave filter
> - RSD demuxer
> +- RedSpark demuxer
>
>
> version 1.2:
> diff --git a/doc/general.texi b/doc/general.texi
> index 8b98d51..13f56ee 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
keep alphabetical order
> @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..d37aba0
> --- /dev/null
> +++ b/libavformat/redspark.c
> @@ -0,0 +1,184 @@
> +/*
> + * 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 {
> + int samples_count;
> + 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;
You could use AV_RB64, which should be faster.
> +
> + 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;
> +
> + 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) {
you could start i from 4 and avoid subtraction
> + 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;
> + redspark->samples_count = 0;
> + 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 = 8 * codec->channels;
> + int ret;
> +
> + if (url_feof(s->pb) || redspark->samples_count ==
> s->streams[0]->duration)
> + 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 = 14;
> + redspark->samples_count += pkt->duration;
> + 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 3e186a9..23bab23 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 4
> +#define LIBAVFORMAT_VERSION_MINOR 5
> #define LIBAVFORMAT_VERSION_MICRO 100
>
> #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR,
> \
> --
> 1.8.1.msysgit.1
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
LGTM
More information about the ffmpeg-devel
mailing list