[FFmpeg-devel] [PATCH] CDXL demuxer and decoder

Paul B Mahol onemda at gmail.com
Fri Dec 30 00:08:44 CET 2011


---
 Changelog                |    1 +
 doc/general.texi         |    4 +
 libavcodec/Makefile      |    1 +
 libavcodec/allcodecs.c   |    1 +
 libavcodec/avcodec.h     |    1 +
 libavcodec/cdxl.c        |  270 ++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h     |    2 +-
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    1 +
 libavformat/cdxl.c       |  131 ++++++++++++++++++++++
 libavformat/version.h    |    2 +-
 11 files changed, 413 insertions(+), 2 deletions(-)
 create mode 100644 libavcodec/cdxl.c
 create mode 100644 libavformat/cdxl.c

diff --git a/Changelog b/Changelog
index 4ab7882..86efd07 100644
--- a/Changelog
+++ b/Changelog
@@ -11,6 +11,7 @@ version next:
 - thumbnail video filter
 - XML output in ffprobe
 - asplit audio filter
+- CDXL demuxer and decoder
 
 
 version 0.9:
diff --git a/doc/general.texi b/doc/general.texi
index b04d7d7..7c1138f 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -145,6 +145,8 @@ library:
     @tab Video format used by CD+G karaoke disks
 @item Core Audio Format         @tab X @tab X
     @tab Apple Core Audio Format
+ at item CDXL format               @tab   @tab X
+    @tab multimedia format for Amiga
 @item CRC testing format        @tab X @tab
 @item Creative Voice            @tab X @tab X
     @tab Created for the Sound Blaster Pro.
@@ -453,6 +455,8 @@ following image formats are supported:
     @tab fourcc: CSCD
 @item CD+G                   @tab     @tab  X
     @tab Video codec for CD+G karaoke disks
+ at item CDXL                   @tab     @tab  X
+    @tab Video codec for CDXL format
 @item Chinese AVS video      @tab  E  @tab  X
     @tab AVS1-P2, JiZhun profile, encoding through external library libxavs
 @item Delphine Software International CIN video  @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2b7a8c7..e88dc39 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -106,6 +106,7 @@ OBJS-$(CONFIG_C93_DECODER)             += c93.o
 OBJS-$(CONFIG_CAVS_DECODER)            += cavs.o cavsdec.o cavsdsp.o \
                                           mpeg12data.o mpegvideo.o
 OBJS-$(CONFIG_CDGRAPHICS_DECODER)      += cdgraphics.o
+OBJS-$(CONFIG_CDXL_DECODER)            += cdxl.o
 OBJS-$(CONFIG_CINEPAK_DECODER)         += cinepak.o
 OBJS-$(CONFIG_CLJR_DECODER)            += cljr.o
 OBJS-$(CONFIG_CLJR_ENCODER)            += cljr.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 1f47aee..617c80c 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -88,6 +88,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (C93, c93);
     REGISTER_DECODER (CAVS, cavs);
     REGISTER_DECODER (CDGRAPHICS, cdgraphics);
+    REGISTER_DECODER (CDXL, cdxl);
     REGISTER_DECODER (CINEPAK, cinepak);
     REGISTER_ENCDEC  (CLJR, cljr);
     REGISTER_DECODER (CSCD, cscd);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 690ea38..4afb8a8 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -259,6 +259,7 @@ enum CodecID {
     CODEC_ID_ESCAPE130  = MKBETAG('E','1','3','0'),
 
     CODEC_ID_G2M        = MKBETAG( 0 ,'G','2','M'),
+    CODEC_ID_CDXL,
 
     /* various PCM "codecs" */
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/cdxl.c b/libavcodec/cdxl.c
new file mode 100644
index 0000000..1755401
--- /dev/null
+++ b/libavcodec/cdxl.c
@@ -0,0 +1,270 @@
+/*
+ * CDXL video decoder
+ * Copyright (c) 2011 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/intreadwrite.h"
+#include "libavutil/imgutils.h"
+#include "avcodec.h"
+#include "bytestream.h"
+#include "get_bits.h"
+
+typedef struct {
+    AVFrame       frame;
+    int           bpp;
+    const uint8_t *palette;
+    int           palette_size;
+    const uint8_t *video;
+    uint8_t       *new_video;
+    int           video_size;
+} CDXLVideoContext;
+
+static av_cold int cdxl_decode_init(AVCodecContext *avctx)
+{
+    CDXLVideoContext *c = avctx->priv_data;
+
+    avcodec_get_frame_defaults(&c->frame);
+    avctx->coded_frame = (AVFrame*)&c->frame;
+
+    return 0;
+}
+
+static void cdxl_import_palette(CDXLVideoContext *c, uint32_t *new_palette)
+{
+    int i;
+
+    for (i = 0; i < c->palette_size / 2; i++) {
+        unsigned xxx = bytestream_get_be16(&c->palette);
+        unsigned r   = ((xxx >> 8) & 0xF) * 17;
+        unsigned g   = ((xxx >> 4) & 0xF) * 17;
+        unsigned b   =  (xxx       & 0xF) * 17;
+        new_palette[i] = (0xFF << 24) | (b + g * 256u + r * 256u * 256u);
+    }
+}
+
+static void cdxl_decode_rgb(CDXLVideoContext *c, AVCodecContext *avctx)
+{
+    int x, y, plane;
+    uint32_t *new_palette = (uint32_t *) c->frame.data[1];
+    int padded_width = (avctx->width + 15) & ~15;
+    GetBitContext gb;
+
+    cdxl_import_palette(c, new_palette);
+    memset(c->frame.data[0], 0, c->frame.linesize[0] * avctx->height);
+    init_get_bits(&gb, c->video, c->video_size * 8);
+    for (plane = 0; plane < c->bpp; plane++)
+        for (y = 0; y < avctx->height; y++)
+            for (x = 0; x < padded_width; x++)
+                c->frame.data[0][c->frame.linesize[0] * y + x] |= get_bits1(&gb) << (plane);
+}
+
+static void cdxl_decode_ham6(CDXLVideoContext *c, AVCodecContext *avctx)
+{
+    int x, y, plane;
+    uint32_t new_palette[16], r, g, b;
+    uint8_t *ptr, *out, index, op;
+    GetBitContext gb;
+
+    ptr = c->new_video;
+    out = c->frame.data[0];
+
+    cdxl_import_palette(c, new_palette);
+    init_get_bits(&gb, c->video, c->video_size * 8);
+    for (plane = 0; plane < c->bpp; plane++)
+        for (y = 0; y < avctx->height; y++)
+            for (x = 0; x < avctx->width; x++)
+                c->new_video[avctx->width * y + x] |= get_bits1(&gb) << (plane);
+
+    for (y = 0; y < avctx->height; y++) {
+        r = (new_palette[0] & 0xFF0000);
+        g = (new_palette[0] & 0xFF00);
+        b = (new_palette[0] & 0xFF);
+        for (x = 0; x < avctx->width; x++) {
+            index = *ptr++;
+            op = index >> 4;
+            index &= 15;
+            switch (op) {
+            case 0:
+                r = (new_palette[index] & 0xFF0000);
+                g = (new_palette[index] & 0xFF00);
+                b = (new_palette[index] & 0xFF);
+                break;
+            case 1:
+                b = index * 17;
+                break;
+            case 2:
+                r = (index * 17) << 16;
+                break;
+            case 3:
+                g = (index * 17) << 8;
+                break;
+            }
+            AV_WL32(out + (x * 4), (0xFF << 24) | (r | g | b));
+        }
+        out += c->frame.linesize[0];
+    }
+}
+
+static void cdxl_decode_ham8(CDXLVideoContext *c, AVCodecContext *avctx)
+{
+    int x, y, plane;
+    uint32_t new_palette[64], r, g, b;
+    uint8_t *ptr, *out, index, op;
+    GetBitContext gb;
+
+    ptr = c->new_video;
+    out = c->frame.data[0];
+
+    cdxl_import_palette(c, new_palette);
+    init_get_bits(&gb, c->video, c->video_size * 8);
+    for (plane = 0; plane < c->bpp; plane++)
+        for (y = 0; y < avctx->height; y++)
+            for (x = 0; x < avctx->width; x++)
+                c->new_video[avctx->width * y + x] |= get_bits1(&gb) << (plane);
+
+    for (y = 0; y < avctx->height; y++) {
+        r = (new_palette[0] & 0xFF0000);
+        g = (new_palette[0] & 0xFF00);
+        b = (new_palette[0] & 0xFF);
+        for (x = 0; x < avctx->width; x++) {
+            index = *ptr++;
+            op = index >> 6;
+            index &= 63;
+            switch (op) {
+            case 0:
+                r = (new_palette[index] & 0xFF0000);
+                g = (new_palette[index] & 0xFF00);
+                b = (new_palette[index] & 0xFF);
+                break;
+            case 1:
+                b = (index << 2) | (b & 3);
+                break;
+            case 2:
+                r = (index << 18) | (r & 196608);
+                break;
+            case 3:
+                g = (index << 10) | (g & 768);
+                break;
+            }
+            AV_WL32(out + (x * 4), (0xFF << 24) | (r | g | b));
+        }
+        out += c->frame.linesize[0];
+    }
+}
+
+static int cdxl_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
+                             AVPacket *pkt)
+{
+    const uint8_t *buf = pkt->data;
+    int w, h, encoding, format, buf_size = pkt->size;
+    CDXLVideoContext *c = avctx->priv_data;
+    AVFrame * const p = &c->frame;
+
+    if (buf_size < 32)
+        return AVERROR_INVALIDDATA;
+    encoding        = buf[1] & 7;
+    format          = buf[1] & 0xE0;
+    w               = AV_RB16(&buf[14]);
+    h               = AV_RB16(&buf[16]);
+    c->bpp          = AV_RB16(&buf[18]);
+    c->palette_size = AV_RB16(&buf[20]);
+    c->palette      = buf + 32;
+    c->video        = c->palette + c->palette_size;
+    c->video_size   = buf_size - c->palette_size - 32;
+
+    if (encoding == 0) {
+        if (c->video_size < ((avctx->width + 15) & ~15) * avctx->height * c->bpp / 8)
+            return AVERROR(EINVAL);
+        avctx->pix_fmt = PIX_FMT_PAL8;
+    } else if (encoding == 1) {
+        if (c->video_size < avctx->width * avctx->height * c->bpp / 8)
+            return AVERROR(EINVAL);
+        avctx->pix_fmt = PIX_FMT_RGB32;
+    } else {
+        av_log_ask_for_sample(avctx, "unsupported video format: %d\n", encoding);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (p->data[0])
+        avctx->release_buffer(avctx, p);
+
+    if (av_image_check_size(w, h, 0, avctx))
+        return -1;
+    if (w != avctx->width || h != avctx->height)
+        avcodec_set_dimensions(avctx, w, h);
+
+    p->reference = 0;
+    if (avctx->get_buffer(avctx, p) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return -1;
+    }
+    p->pict_type = AV_PICTURE_TYPE_I;
+
+    if (buf[0] != 1) {
+        av_log(avctx, AV_LOG_ERROR, "non-standard cdxl file\n");
+        return -1;
+    }
+    if (format) {
+        av_log_ask_for_sample(avctx, "unsupported pixel format: %d\n", format);
+        return AVERROR_PATCHWELCOME;
+    }
+    if (c->bpp < 1)
+        return AVERROR_INVALIDDATA;
+    if (c->bpp > 8) {
+        av_log_ask_for_sample(avctx, "unsupported pixel size: %d\n", c->bpp);
+        return AVERROR_PATCHWELCOME;
+    }
+    if (encoding == 0) {
+        cdxl_decode_rgb(c, avctx);
+    } else if (encoding == 1) {
+        c->new_video = av_mallocz(avctx->height * avctx->width);
+        if (c->new_video == NULL)
+            return AVERROR(ENOMEM);
+        if (c->bpp > 6)
+            cdxl_decode_ham8(c, avctx);
+        else
+            cdxl_decode_ham6(c, avctx);
+        av_freep(&c->new_video);
+    }
+    *data_size = sizeof(AVFrame);
+    *(AVFrame*)data = c->frame;
+    return buf_size;
+}
+
+static av_cold int cdxl_decode_end(AVCodecContext *avctx)
+{
+    CDXLVideoContext *c = avctx->priv_data;
+
+    if (c->frame.data[0])
+        avctx->release_buffer(avctx, &c->frame);
+
+    return 0;
+}
+
+AVCodec ff_cdxl_decoder = {
+    .name           = "cdxl",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_CDXL,
+    .priv_data_size = sizeof(CDXLVideoContext),
+    .init           = cdxl_decode_init,
+    .close          = cdxl_decode_end,
+    .decode         = cdxl_decode_frame,
+    .capabilities   = CODEC_CAP_DR1,
+    .long_name      = NULL_IF_CONFIG_SMALL("CDXL video"),
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 1e11626..1f7e621 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -21,7 +21,7 @@
 #define AVCODEC_VERSION_H
 
 #define LIBAVCODEC_VERSION_MAJOR 53
-#define LIBAVCODEC_VERSION_MINOR 49
+#define LIBAVCODEC_VERSION_MINOR 50
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 877a86c..1458e02 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -62,6 +62,7 @@ OBJS-$(CONFIG_CAF_MUXER)                 += cafenc.o caf.o riff.o isom.o
 OBJS-$(CONFIG_CAVSVIDEO_DEMUXER)         += cavsvideodec.o rawdec.o
 OBJS-$(CONFIG_CAVSVIDEO_MUXER)           += rawenc.o
 OBJS-$(CONFIG_CDG_DEMUXER)               += cdg.o
+OBJS-$(CONFIG_CDXL_DEMUXER)              += cdxl.o
 OBJS-$(CONFIG_CRC_MUXER)                 += crcenc.o
 OBJS-$(CONFIG_DAUD_DEMUXER)              += daud.o
 OBJS-$(CONFIG_DAUD_MUXER)                += daud.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 615ae57..45aba2b 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -80,6 +80,7 @@ void av_register_all(void)
     REGISTER_MUXDEMUX (CAF, caf);
     REGISTER_MUXDEMUX (CAVSVIDEO, cavsvideo);
     REGISTER_DEMUXER  (CDG, cdg);
+    REGISTER_DEMUXER  (CDXL, cdxl);
     REGISTER_MUXER    (CRC, crc);
     REGISTER_MUXDEMUX (DAUD, daud);
     REGISTER_DEMUXER  (DFA, dfa);
diff --git a/libavformat/cdxl.c b/libavformat/cdxl.c
new file mode 100644
index 0000000..85159e5
--- /dev/null
+++ b/libavformat/cdxl.c
@@ -0,0 +1,131 @@
+/*
+ * CDXL demuxer
+ * Copyright (c) 2011 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/intreadwrite.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/opt.h"
+#include "avformat.h"
+#include "internal.h"
+
+#define CDXL_HEADER_SIZE 32
+
+typedef struct CDXLDemuxContext {
+    AVClass *class;
+    int raw_sound_size;
+    int sample_rate;
+    char *framerate;
+} CDXLDemuxContext;
+
+static int cdxl_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+    AVIOContext *pb = s->pb;
+    CDXLDemuxContext *c = s->priv_data;
+    AVStream *vst, *ast;
+    AVRational framerate;
+    uint8_t header[CDXL_HEADER_SIZE];
+    int ret;
+
+    if (avio_read(pb, header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
+        return AVERROR(EIO);
+
+    if ((ret = av_parse_video_rate(&framerate, c->framerate)) < 0) {
+        av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", c->framerate);
+        return ret;
+    }
+
+    ast = avformat_new_stream(s, NULL);
+    if (!ast)
+        return AVERROR(ENOMEM);
+    ast->codec->bits_per_coded_sample = 8;
+    ast->codec->sample_rate = c->sample_rate;
+    ast->codec->sample_fmt  = AV_SAMPLE_FMT_U8;
+    ast->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+    ast->codec->codec_tag   = 0;
+    ast->codec->bit_rate    = c->sample_rate;
+    ast->codec->channels    = (AV_RB8(&header[1]) & 8) + 1;
+    ast->codec->codec_id    = CODEC_ID_PCM_S8;
+    avpriv_set_pts_info(ast, 24, 1, ast->codec->sample_rate);
+
+    vst = avformat_new_stream(s, NULL);
+    if (!vst)
+        return AVERROR(ENOMEM);
+    vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    ast->codec->codec_tag  = 0;
+    vst->codec->codec_id   = CODEC_ID_CDXL;
+    vst->codec->width      = AV_RB16(&header[14]);
+    vst->codec->height     = AV_RB16(&header[16]);
+    avpriv_set_pts_info(vst, 24, framerate.den, framerate.num);
+    avio_seek(pb, 0, SEEK_SET);
+    return 0;
+}
+
+static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVIOContext *pb = s->pb;
+    CDXLDemuxContext *c = s->priv_data;
+    int ret, current_size;
+    uint8_t header[CDXL_HEADER_SIZE];
+
+    if (c->raw_sound_size) {
+        ret = av_get_packet(pb, pkt, c->raw_sound_size);
+        if (ret < 0)
+            return ret;
+        pkt->stream_index = c->raw_sound_size = 0;
+    } else {
+        if (avio_read(pb, header, CDXL_HEADER_SIZE) !=
+                CDXL_HEADER_SIZE)
+            return AVERROR(EIO);
+        avio_seek(pb, -CDXL_HEADER_SIZE, SEEK_CUR);
+
+        current_size = AV_RB32(&header[2]);
+        c->raw_sound_size = AV_RB16(&header[22]);
+        ret = av_get_packet(pb, pkt, current_size - c->raw_sound_size);
+        if (ret < 0)
+            return ret;
+        pkt->stream_index = 1;
+    }
+
+    return ret;
+}
+
+#define OFFSET(x) offsetof(CDXLDemuxContext, x)
+static const AVOption cdxl_options[] = {
+    { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.dbl = 11025}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
+    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "50"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
+    { NULL },
+};
+
+static const AVClass cdxl_demuxer_class = {
+    .class_name = "CDXL demuxer",
+    .item_name  = av_default_item_name,
+    .option     = cdxl_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_cdxl_demuxer = {
+    .name           = "cdxl",
+    .long_name      = NULL_IF_CONFIG_SMALL("CDXL"),
+    .priv_data_size = sizeof(CDXLDemuxContext),
+    .read_header    = cdxl_read_header,
+    .read_packet    = cdxl_read_packet,
+    .extensions     = "cdxl,xl",
+    .priv_class     = &cdxl_demuxer_class,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index b8adb92..e1d9f1f 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 53
-#define LIBAVFORMAT_VERSION_MINOR 29
+#define LIBAVFORMAT_VERSION_MINOR 30
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
1.7.7



More information about the ffmpeg-devel mailing list