[SCM] libav/experimental: implemented Id Quake II CIN support

siretart at users.alioth.debian.org siretart at users.alioth.debian.org
Sun Jun 30 15:37:46 UTC 2013


The following commit has been merged in the experimental branch:
commit 4120a53a868cc99e66121c625981050deed50443
Author: Mike Melanson <mike at multimedia.cx>
Date:   Fri Oct 3 05:43:03 2003 +0000

    implemented Id Quake II CIN support
    
    Originally committed as revision 2339 to svn://svn.ffmpeg.org/ffmpeg/trunk

diff --git a/Changelog b/Changelog
index decb6e9..112d537 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@ version <next>:
 - Cinepak decoder
 - Sega FILM (CPK) file demuxer
 - Westwood multimedia support (VQA/AUD file demuxer, audio decoder)
+- Id Quake II CIN playback support
 
 version 0.4.8:
 
diff --git a/doc/ffmpeg-doc.texi b/doc/ffmpeg-doc.texi
index 5bfaa8f..c5ace0b 100644
--- a/doc/ffmpeg-doc.texi
+++ b/doc/ffmpeg-doc.texi
@@ -633,6 +633,8 @@ library:
 @tab used in many Sega Saturn console games
 @item Westwood Studios VQA/AUD  @tab    @tab X
 @tab Multimedia formats used in Westwood Studios games
+ at item Id Cinematic (.cin) @tab    @tab X
+ at tab Used in Quake II
 @end multitable
 
 @code{X} means that the encoding (resp. decoding) is supported.
@@ -692,6 +694,8 @@ following image formats are supported:
 @item Cinepak                @tab     @tab  X
 @item Microsoft RLE          @tab     @tab  X
 @item Microsoft Video-1      @tab     @tab  X
+ at item Westwood VQA           @tab     @tab  X
+ at item Id Cinematic Video     @tab     @tab  X @tab used in Quake II
 @end multitable
 
 @code{X} means that the encoding (resp. decoding) is supported.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0350153..6da467d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -18,7 +18,7 @@ OBJS= common.o utils.o mem.o allcodecs.o \
       fft.o mdct.o mace.o huffyuv.o cyuv.o opts.o raw.o h264.o golomb.o \
       vp3.o asv1.o 4xm.o cabac.o ffv1.o ra144.o ra288.o vcr1.o cljr.o \
       roqvideo.o dpcm.o interplayvideo.o xan.o rpza.o cinepak.o msrle.o \
-      msvideo1.o vqavideo.o
+      msvideo1.o vqavideo.o idcinvideo.o
 
 ifeq ($(AMR_NB),yes)
 ifeq ($(AMR_NB_FIXED),yes)
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 10dacf8..ea4ae60 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -128,6 +128,7 @@ void avcodec_register_all(void)
     register_avcodec(&msrle_decoder);
     register_avcodec(&msvideo1_decoder);
     register_avcodec(&vqa_decoder);
+    register_avcodec(&idcin_decoder);
 #ifdef CONFIG_AC3
     register_avcodec(&ac3_decoder);
 #endif
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index c2d3943..32227f4 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -79,6 +79,7 @@ enum CodecID {
     CODEC_ID_WS_VQA,
     CODEC_ID_MSRLE,
     CODEC_ID_MSVIDEO1,
+    CODEC_ID_IDCIN,
 
     /* various pcm "codecs" */
     CODEC_ID_PCM_S16LE,
@@ -1433,6 +1434,7 @@ extern AVCodec cinepak_decoder;
 extern AVCodec msrle_decoder;
 extern AVCodec msvideo1_decoder;
 extern AVCodec vqa_decoder;
+extern AVCodec idcin_decoder;
 extern AVCodec ra_144_decoder;
 extern AVCodec ra_288_decoder;
 extern AVCodec roq_dpcm_decoder;
diff --git a/libavcodec/idcinvideo.c b/libavcodec/idcinvideo.c
new file mode 100644
index 0000000..2a6ae36
--- /dev/null
+++ b/libavcodec/idcinvideo.c
@@ -0,0 +1,278 @@
+/*
+ * Id Quake II CIN Video Decoder
+ * Copyright (C) 2003 the ffmpeg project
+ *
+ * This library 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 of the License, or (at your option) any later version.
+ *
+ * This library 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/**
+ * @file idcinvideo.c
+ * Id Quake II Cin Video Decoder by Dr. Tim Ferguson
+ * For more information about the Id CIN format, visit:
+ *   http://www.csse.monash.edu.au/~timf/
+ *
+ * This video decoder outputs PAL8 colorspace data.
+ *
+ * Id CIN video is purely Huffman-coded, intraframe-only codec. It achieves
+ * a little more compression by exploiting the fact that adjacent pixels
+ * tend to be similar.
+ *
+ * Note that this decoder could use ffmpeg's optimized VLC facilities
+ * rather than naive, tree-based Huffman decoding. However, there are 256
+ * Huffman tables. Plus, the VLC bit coding order is right -> left instead
+ * or left -> right, so all of the bits would have to be reversed. Further,
+ * the original Quake II implementation likely used a similar naive
+ * decoding algorithm and it worked fine on much lower spec machines.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "avcodec.h"
+#include "dsputil.h"
+
+#define HUFFMAN_TABLE_SIZE 64 * 1024
+#define HUF_TOKENS 256
+#define PALETTE_COUNT 256
+
+typedef struct
+{
+  int count;
+  unsigned char used;
+  int children[2];
+} hnode_t;
+
+typedef struct IdcinContext {
+
+    AVCodecContext *avctx;
+    DSPContext dsp;
+    AVFrame frame;
+
+    unsigned char *buf;
+    int size;
+
+    unsigned char palette[PALETTE_COUNT * 4];
+
+    hnode_t huff_nodes[256][HUF_TOKENS*2];
+    int num_huff_nodes[256];
+
+} IdcinContext;
+
+/*
+ * Find the lowest probability node in a Huffman table, and mark it as
+ * being assigned to a higher probability.
+ * Returns the node index of the lowest unused node, or -1 if all nodes
+ * are used.
+ */
+static int huff_smallest_node(hnode_t *hnodes, int num_hnodes) {
+    int i;
+    int best, best_node;
+
+    best = 99999999;
+    best_node = -1;
+    for(i = 0; i < num_hnodes; i++) {
+        if(hnodes[i].used)
+            continue;
+        if(!hnodes[i].count)
+            continue;
+        if(hnodes[i].count < best) {
+            best = hnodes[i].count;
+            best_node = i;
+        }
+    }
+
+    if(best_node == -1)
+        return -1;
+    hnodes[best_node].used = 1;
+    return best_node;
+}
+
+/*
+ * Build the Huffman tree using the generated/loaded probabilities histogram.
+ *
+ * On completion:
+ *  huff_nodes[prev][i < HUF_TOKENS] - are the nodes at the base of the tree.
+ *  huff_nodes[prev][i >= HUF_TOKENS] - are used to construct the tree.
+ *  num_huff_nodes[prev] - contains the index to the root node of the tree.
+ *    That is: huff_nodes[prev][num_huff_nodes[prev]] is the root node.
+ */
+static void huff_build_tree(IdcinContext *s, int prev) {
+    hnode_t *node, *hnodes;
+     int num_hnodes, i;
+
+    num_hnodes = HUF_TOKENS;
+    hnodes = s->huff_nodes[prev];
+    for(i = 0; i < HUF_TOKENS * 2; i++)
+        hnodes[i].used = 0;
+
+    while (1) {
+        node = &hnodes[num_hnodes];             /* next free node */
+
+        /* pick two lowest counts */
+        node->children[0] = huff_smallest_node(hnodes, num_hnodes);
+        if(node->children[0] == -1)
+            break;      /* reached the root node */
+
+        node->children[1] = huff_smallest_node(hnodes, num_hnodes);
+        if(node->children[1] == -1)
+            break;      /* reached the root node */
+
+        /* combine nodes probability for new node */
+        node->count = hnodes[node->children[0]].count +
+        hnodes[node->children[1]].count;
+        num_hnodes++;
+    }
+
+    s->num_huff_nodes[prev] = num_hnodes - 1;
+}
+
+static int idcin_decode_init(AVCodecContext *avctx)
+{
+    IdcinContext *s = (IdcinContext *)avctx->priv_data;
+    int i, j, histogram_index = 0;
+    unsigned char *histograms;
+
+    s->avctx = avctx;
+    avctx->pix_fmt = PIX_FMT_PAL8;
+    avctx->has_b_frames = 0;
+    dsputil_init(&s->dsp, avctx);
+
+    /* make sure the Huffman tables make it */
+    if (s->avctx->extradata_size != HUFFMAN_TABLE_SIZE) {
+        printf("  Id CIN video: expected extradata size of %d\n", HUFFMAN_TABLE_SIZE);
+        return -1;
+    }
+
+    /* build the 256 Huffman decode trees */
+    histograms = (unsigned char *)s->avctx->extradata;
+    for (i = 0; i < 256; i++) {
+        for(j = 0; j < HUF_TOKENS; j++)
+            s->huff_nodes[i][j].count = histograms[histogram_index++];
+        huff_build_tree(s, i);
+    }
+
+    s->frame.data[0] = NULL;
+
+    return 0;
+}
+
+static void idcin_decode_vlcs(IdcinContext *s)
+{
+    hnode_t *hnodes;
+    long x, y;
+    int prev;
+    unsigned char v = 0;
+    int bit_pos, node_num, dat_pos;
+
+    prev = bit_pos = dat_pos = 0;
+    for (y = 0; y < (s->frame.linesize[0] * s->avctx->height);
+        y += s->frame.linesize[0]) {
+        for (x = y; x < y + s->avctx->width; x++) {
+            node_num = s->num_huff_nodes[prev];
+            hnodes = s->huff_nodes[prev];
+
+            while(node_num >= HUF_TOKENS) {
+                if(!bit_pos) {
+                    if(dat_pos > s->size) {
+                        printf("Huffman decode error.\n");
+                        return;
+                    }
+                    bit_pos = 8;
+                    v = s->buf[dat_pos++];
+                }
+
+                node_num = hnodes[node_num].children[v & 0x01];
+                v = v >> 1;
+                bit_pos--;
+            }
+
+            s->frame.data[0][x] = node_num;
+            prev = node_num;
+        }
+    }
+}
+
+static int idcin_decode_frame(AVCodecContext *avctx,
+                              void *data, int *data_size,
+                              uint8_t *buf, int buf_size)
+{
+    IdcinContext *s = (IdcinContext *)avctx->priv_data;
+    AVPaletteControl *palette_control = 
+        (AVPaletteControl *)avctx->extradata;
+    int i;
+    unsigned int *palette32;
+    int palette_index = 0;
+    unsigned char r, g, b;
+
+    s->buf = buf;
+    s->size = buf_size;
+
+    if (palette_control->palette_changed) {
+        palette32 = (unsigned int *)s->palette;
+        for (i = 0; i < PALETTE_COUNT; i++) {
+            r = palette_control->palette[palette_index++] * 1;
+            g = palette_control->palette[palette_index++] * 1;
+            b = palette_control->palette[palette_index++] * 1;
+            palette32[i] = (r << 16) | (g << 8) | (b);
+        }
+        palette_control->palette_changed = 0;
+    }
+
+    if (s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
+    if (avctx->get_buffer(avctx, &s->frame)) {
+        printf ("  Id CIN Video: get_buffer() failed\n");
+        return -1;
+    }
+
+    idcin_decode_vlcs(s);
+
+    /* make the palette available on the way out */
+    memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
+
+    *data_size = sizeof(AVFrame);
+    *(AVFrame*)data = s->frame;
+
+    /* report that the buffer was completely consumed */
+    return buf_size;
+}
+
+static int idcin_decode_end(AVCodecContext *avctx)
+{
+    IdcinContext *s = (IdcinContext *)avctx->priv_data;
+
+    if (s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
+    return 0;
+}
+
+AVCodec idcin_decoder = {
+    "idcinvideo",
+    CODEC_TYPE_VIDEO,
+    CODEC_ID_IDCIN,
+    sizeof(IdcinContext),
+    idcin_decode_init,
+    NULL,
+    idcin_decode_end,
+    idcin_decode_frame,
+    CODEC_CAP_DR1,
+};
+
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 7057a7a..de385a5 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -15,7 +15,7 @@ PPOBJS=
 OBJS+=mpeg.o mpegts.o mpegtsenc.o ffm.o crc.o img.o raw.o rm.o \
       avienc.o avidec.o wav.o swf.o au.o gif.o mov.o mpjpeg.o dv.o \
       yuv4mpeg.o 4xm.o flvenc.o flvdec.o movenc.o psxstr.o idroq.o ipmovie.o \
-      nut.o wc3movie.o mp3.o westwood.o segafilm.o
+      nut.o wc3movie.o mp3.o westwood.o segafilm.o idcin.o
 
 ifeq ($(CONFIG_RISKY),yes)
 OBJS+= asf.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index ba42483..2367c47 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -58,6 +58,7 @@ void av_register_all(void)
     wc3_init();
     westwood_init();
     film_init();
+    idcin_init();
 
 #if defined(AMR_NB) || defined(AMR_NB_FIXED) || defined(AMR_WB)
     amr_init();
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 3ca8979..7de9c8e 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -412,6 +412,9 @@ int westwood_init(void);
 /* segafilm.c */
 int film_init(void);
 
+/* idcin.c */
+int idcin_init(void);
+
 #include "rtp.h"
 
 #include "rtsp.h"
diff --git a/libavformat/idcin.c b/libavformat/idcin.c
new file mode 100644
index 0000000..6125c64
--- /dev/null
+++ b/libavformat/idcin.c
@@ -0,0 +1,313 @@
+/*
+ * Id Quake II CIN File Demuxer
+ * Copyright (c) 2003 The ffmpeg Project
+ *
+ * This library 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 of the License, or (at your option) any later version.
+ *
+ * This library 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/**
+ * @file idcin.c
+ * Id Quake II CIN file demuxer by Mike Melanson (melanson at pcisys.net)
+ * For more information about the Id CIN format, visit:
+ *   http://www.csse.monash.edu.au/~timf/
+ *
+ * CIN is a somewhat quirky and ill-defined format. Here are some notes
+ * for anyone trying to understand the technical details of this format:
+ *
+ * The format has no definite file signature. This is problematic for a
+ * general-purpose media player that wants to automatically detect file
+ * types. However, a CIN file does start with 5 32-bit numbers that
+ * specify audio and video parameters. This demuxer gets around the lack
+ * of file signature by performing sanity checks on those parameters.
+ * Probabalistically, this is a reasonable solution since the number of
+ * valid combinations of the 5 parameters is a very small subset of the
+ * total 160-bit number space.
+ *
+ * Refer to the function idcin_probe() for the precise A/V parameters
+ * that this demuxer allows.
+ *
+ * Next, each audio and video frame has a duration of 1/14 sec. If the
+ * audio sample rate is a multiple of the common frequency 22050 Hz it will
+ * divide evenly by 14. However, if the sample rate is 11025 Hz:
+ *   11025 (samples/sec) / 14 (frames/sec) = 787.5 (samples/frame)
+ * The way the CIN stores audio in this case is by storing 787 sample
+ * frames in the first audio frame and 788 sample frames in the second
+ * audio frame. Therefore, the total number of bytes in an audio frame
+ * is given as:
+ *   audio frame #0: 787 * (bytes/sample) * (# channels) bytes in frame
+ *   audio frame #1: 788 * (bytes/sample) * (# channels) bytes in frame
+ *   audio frame #2: 787 * (bytes/sample) * (# channels) bytes in frame
+ *   audio frame #3: 788 * (bytes/sample) * (# channels) bytes in frame
+ *
+ * Finally, not all Id CIN creation tools agree on the resolution of the
+ * color palette, apparently. Some creation tools specify red, green, and
+ * blue palette components in terms of 6-bit VGA color DAC values which
+ * range from 0..63. Other tools specify the RGB components as full 8-bit
+ * values that range from 0..255. Since there are no markers in the file to
+ * differentiate between the two variants, this demuxer uses the following
+ * heuristic:
+ *   - load the 768 palette bytes from disk
+ *   - assume that they will need to be shifted left by 2 bits to
+ *     transform them from 6-bit values to 8-bit values
+ *   - scan through all 768 palette bytes
+ *     - if any bytes exceed 63, do not shift the bytes at all before
+ *       transmitting them to the video decoder
+ */
+
+#include "avformat.h"
+
+#define LE_16(x)  ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
+#define LE_32(x)  ((((uint8_t*)(x))[3] << 24) | \
+                   (((uint8_t*)(x))[2] << 16) | \
+                   (((uint8_t*)(x))[1] << 8) | \
+                    ((uint8_t*)(x))[0])
+
+#define HUFFMAN_TABLE_SIZE (64 * 1024)
+#define FRAME_PTS_INC (90000 / 14)
+
+typedef struct IdcinDemuxContext {
+    int video_stream_index;
+    int audio_stream_index;
+    int audio_chunk_size1;
+    int audio_chunk_size2;
+
+    /* demux state variables */
+    int current_audio_chunk;
+    int next_chunk_is_video;
+    int audio_present;
+
+    int64_t pts;
+
+    /* keep reference to extradata but never free it */
+    void *extradata;
+} IdcinDemuxContext;
+
+static int idcin_probe(AVProbeData *p)
+{
+    unsigned int number;
+
+    /*
+     * This is what you could call a "probabilistic" file check: Id CIN
+     * files don't have a definite file signature. In lieu of such a marker,
+     * perform sanity checks on the 5 32-bit header fields:
+     *  width, height: greater than 0, less than or equal to 1024
+     * audio sample rate: greater than or equal to 8000, less than or
+     *  equal to 48000, or 0 for no audio
+     * audio sample width (bytes/sample): 0 for no audio, or 1 or 2
+     * audio channels: 0 for no audio, or 1 or 2
+     */
+
+    /* cannot proceed without 20 bytes */
+    if (p->buf_size < 20)
+        return 0;
+
+    /* check the video width */
+    number = LE_32(&p->buf[0]);
+    if ((number == 0) || (number > 1024))
+       return 0;
+
+    /* check the video height */
+    number = LE_32(&p->buf[4]);
+    if ((number == 0) || (number > 1024))
+       return 0;
+
+    /* check the audio sample rate */
+    number = LE_32(&p->buf[8]);
+    if ((number != 0) && ((number < 8000) | (number > 48000)))
+        return 0;
+
+    /* check the audio bytes/sample */
+    number = LE_32(&p->buf[12]);
+    if (number > 2)
+        return 0;
+
+    /* check the audio channels */
+    number = LE_32(&p->buf[16]);
+    if (number > 2)
+        return 0;
+
+    /* return half certainly since this check is a bit sketchy */
+    return AVPROBE_SCORE_MAX / 2;
+}
+
+static int idcin_read_header(AVFormatContext *s,
+                             AVFormatParameters *ap)
+{
+    ByteIOContext *pb = &s->pb;
+    IdcinDemuxContext *idcin = (IdcinDemuxContext *)s->priv_data;
+    AVStream *st;
+    unsigned int width, height;
+    unsigned int sample_rate, bytes_per_sample, channels;
+
+    /* get the 5 header parameters */
+    width = get_le32(pb);
+    height = get_le32(pb);
+    sample_rate = get_le32(pb);
+    bytes_per_sample = get_le32(pb);
+    channels = get_le32(pb);
+
+    st = av_new_stream(s, 0);
+    if (!st)
+        return AVERROR_NOMEM;
+    idcin->video_stream_index = st->index;
+    st->codec.codec_type = CODEC_TYPE_VIDEO;
+    st->codec.codec_id = CODEC_ID_IDCIN;
+    st->codec.codec_tag = 0;  /* no fourcc */
+    st->codec.width = width;
+    st->codec.height = height;
+
+    /* load up the Huffman tables into extradata */
+    st->codec.extradata_size = HUFFMAN_TABLE_SIZE;
+    st->codec.extradata = av_malloc(HUFFMAN_TABLE_SIZE);
+    if (get_buffer(pb, st->codec.extradata, HUFFMAN_TABLE_SIZE) !=
+        HUFFMAN_TABLE_SIZE)
+        return -EIO;
+    /* save a reference in order to transport the palette */
+    idcin->extradata = st->codec.extradata;
+
+    /* if sample rate is 0, assume no audio */
+    if (sample_rate) {
+        idcin->audio_present = 1;
+        st = av_new_stream(s, 0);
+        if (!st)
+            return AVERROR_NOMEM;
+        idcin->audio_stream_index = st->index;
+        st->codec.codec_type = CODEC_TYPE_AUDIO;
+        st->codec.codec_tag = 1;
+        st->codec.channels = channels;
+        st->codec.sample_rate = sample_rate;
+        st->codec.bits_per_sample = bytes_per_sample * 8;
+        st->codec.bit_rate = sample_rate * bytes_per_sample * 8 * channels;
+        st->codec.block_align = bytes_per_sample * channels;
+        if (bytes_per_sample == 1)
+            st->codec.codec_id = CODEC_ID_PCM_U8;
+        else
+            st->codec.codec_id = CODEC_ID_PCM_S16LE;
+
+        if (sample_rate % 14 != 0) {
+            idcin->audio_chunk_size1 = (sample_rate / 14) *
+            bytes_per_sample * channels;
+            idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
+                bytes_per_sample * channels;
+        } else {
+            idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
+                (sample_rate / 14) * bytes_per_sample * channels;
+        }
+        idcin->current_audio_chunk = 0;
+    } else
+        idcin->audio_present = 1;
+
+    idcin->next_chunk_is_video = 1;
+    idcin->pts = 0;
+
+    /* set the pts reference (1 pts = 1/90000) */
+    s->pts_num = 1;
+    s->pts_den = 90000;
+
+    return 0;
+}
+
+static int idcin_read_packet(AVFormatContext *s,
+                             AVPacket *pkt)
+{
+    int ret;
+    unsigned int command;
+    unsigned int chunk_size;
+    IdcinDemuxContext *idcin = (IdcinDemuxContext *)s->priv_data;
+    ByteIOContext *pb = &s->pb;
+    AVPaletteControl *palette_control = (AVPaletteControl *)idcin->extradata;
+    int i;
+    int palette_scale;
+
+    if (url_feof(&s->pb))
+        return -EIO;
+
+    if (idcin->next_chunk_is_video) {
+        command = get_le32(pb);
+        if (command == 2) {
+            return -EIO;
+        } else if (command == 1) {
+            /* trigger a palette change */
+            palette_control->palette_changed = 1;
+            if (get_buffer(pb, palette_control->palette, 768) != 768)
+                return -EIO;
+            /* scale the palette as necessary */
+            palette_scale = 2;
+            for (i = 0; i < 768; i++)
+                if (palette_control->palette[i] > 63) {
+                    palette_scale = 0;
+                    break;
+                }
+
+            if (palette_scale)
+                for (i = 0; i < 768; i++)
+                    palette_control->palette[i] <<= palette_scale;
+        }
+
+        chunk_size = get_le32(pb);
+        /* skip the number of decoded bytes (always equal to width * height) */
+        url_fseek(pb, 4, SEEK_CUR);
+        chunk_size -= 4;
+        if (av_new_packet(pkt, chunk_size))
+            ret = -EIO;
+        pkt->stream_index = idcin->video_stream_index;
+        pkt->pts = idcin->pts;
+        ret = get_buffer(pb, pkt->data, chunk_size);
+        if (ret != chunk_size)
+            ret = -EIO;
+    } else {
+        /* send out the audio chunk */
+        if (idcin->current_audio_chunk)
+            chunk_size = idcin->audio_chunk_size2;
+        else
+            chunk_size = idcin->audio_chunk_size1;
+        if (av_new_packet(pkt, chunk_size))
+            return -EIO;
+        pkt->stream_index = idcin->audio_stream_index;
+        pkt->pts = idcin->pts;
+        ret = get_buffer(&s->pb, pkt->data, chunk_size);
+        if (ret != chunk_size)
+            ret = -EIO;
+
+        idcin->current_audio_chunk ^= 1;
+        idcin->pts += FRAME_PTS_INC;
+    }
+
+    if (idcin->audio_present)
+        idcin->next_chunk_is_video ^= 1;
+
+    return ret;
+}
+
+static int idcin_read_close(AVFormatContext *s)
+{
+    return 0;
+}
+
+static AVInputFormat idcin_iformat = {
+    "idcin",
+    "Id CIN format",
+    sizeof(IdcinDemuxContext),
+    idcin_probe,
+    idcin_read_header,
+    idcin_read_packet,
+    idcin_read_close,
+};
+
+int idcin_init(void)
+{
+    av_register_input_format(&idcin_iformat);
+    return 0;
+}

-- 
Libav/FFmpeg packaging



More information about the pkg-multimedia-commits mailing list