changeset 1585:6b224ca24033 libavcodec

revised palette API, courtesy of Roberto Togni (rtogni at freemail.it)
author melanson
date Thu, 30 Oct 2003 05:40:58 +0000
parents 3615999a7284
children 3d1d0490e5a6
files avcodec.h idcinvideo.c interplayvideo.c utils.c vqavideo.c xan.c
diffstat 6 files changed, 76 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/avcodec.h	Wed Oct 29 17:30:13 2003 +0000
+++ b/avcodec.h	Thu Oct 30 05:40:58 2003 +0000
@@ -453,7 +453,13 @@
      * - decoding: set by lavc\
      */\
     AVPanScan *pan_scan;\
-    
+    \
+    /**\
+     * tell user application that palette has changed from previous frame.\
+     * - encoding: ??? (no palette-enabled encoder yet)\
+     * - decoding: set by lavc (default 0)\
+     */\
+    int palette_has_changed;\
 
 #define FF_QSCALE_TYPE_MPEG1	0
 #define FF_QSCALE_TYPE_MPEG2	1
@@ -1345,6 +1351,13 @@
      * - decoding: unused
      */
     int lmax;
+
+    /**
+     * Palette control structure
+     * - encoding: ??? (no palette-enabled encoder yet)
+     * - decoding: set by user.
+     */
+    struct AVPaletteControl *palctrl;
     
 } AVCodecContext;
 
@@ -1426,17 +1439,18 @@
  * This structure defines a method for communicating palette changes
  * between and demuxer and a decoder.
  */
+#define AVPALETTE_SIZE 256
 typedef struct AVPaletteControl {
 
     /* demuxer sets this to 1 to indicate the palette has changed;
      * decoder resets to 0 */
     int palette_changed;
 
-    /* 256 3-byte RGB palette entries; the components should be
-     * formatted in the buffer as "RGBRGB..." and should be scaled to
-     * 8 bits if they originally represented 6-bit VGA palette
-     * components */
-    unsigned char palette[256 * 3];
+    /* 4-byte ARGB palette entries, stored in native byte order; note that
+     * the individual palette components should be on a 8-bit scale; if
+     * the palette data comes from a IBM VGA native format, the component
+     * data is probably 6 bits in size and needs to be scaled */
+    unsigned int palette[AVPALETTE_SIZE];
 
 } AVPaletteControl;
 
--- a/idcinvideo.c	Wed Oct 29 17:30:13 2003 +0000
+++ b/idcinvideo.c	Thu Oct 30 05:40:58 2003 +0000
@@ -72,8 +72,6 @@
     unsigned char *buf;
     int size;
 
-    unsigned char palette[PALETTE_COUNT * 4];
-
     hnode_t huff_nodes[256][HUF_TOKENS*2];
     int num_huff_nodes[256];
 
@@ -218,27 +216,11 @@
                               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;
+    AVPaletteControl *palette_control = avctx->palctrl;
 
     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);
 
@@ -250,7 +232,12 @@
     idcin_decode_vlcs(s);
 
     /* make the palette available on the way out */
-    memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
+    memcpy(s->frame.data[1], palette_control->palette, PALETTE_COUNT * 4);
+    /* If palette changed inform application*/
+    if (palette_control->palette_changed) {
+        palette_control->palette_changed = 0;
+        s->frame.palette_has_changed = 1;
+    }
 
     *data_size = sizeof(AVFrame);
     *(AVFrame*)data = s->frame;
--- a/interplayvideo.c	Wed Oct 29 17:30:13 2003 +0000
+++ b/interplayvideo.c	Thu Oct 30 05:40:58 2003 +0000
@@ -65,8 +65,6 @@
     unsigned char *buf;
     int size;
 
-    unsigned char palette[PALETTE_COUNT * 4];
-
     unsigned char *stream_ptr;
     unsigned char *stream_end;
     unsigned char *pixel_ptr;
@@ -83,21 +81,6 @@
     return -1; \
   }
 
-static void ipvideo_new_palette(IpvideoContext *s, unsigned char *palette) {
-
-    int i;
-    unsigned char r, g, b;
-    unsigned int *palette32;
-
-    palette32 = (unsigned int *)s->palette;
-    for (i = 0; i < PALETTE_COUNT; i++) {
-        r = *palette++;
-        g = *palette++;
-        b = *palette++;
-        palette32[i] = (r << 16) | (g << 8) | (b);
-    }
-}
-
 #define COPY_FROM_CURRENT() \
     motion_offset = current_offset; \
     motion_offset += y * s->stride; \
@@ -828,7 +811,7 @@
         code_counts[x] = 0;
 
     /* this is PAL8, so make the palette available */
-    memcpy(s->current_frame.data[1], s->palette, PALETTE_COUNT * 4);
+    memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
 
     s->stride = s->current_frame.linesize[0];
     s->stream_ptr = s->buf + 14;  /* data starts 14 bytes in */
@@ -874,9 +857,8 @@
 
     s->avctx = avctx;
 
-    if (s->avctx->extradata_size != sizeof(AVPaletteControl)) {
-        printf (" Interplay video: expected extradata_size of %d\n",
-		(int)sizeof(AVPaletteControl));
+    if (s->avctx->palctrl == NULL) {
+        printf (" Interplay video: palette expected.\n");
         return -1;
     }
 
@@ -916,13 +898,7 @@
                                 uint8_t *buf, int buf_size)
 {
     IpvideoContext *s = avctx->priv_data;
-    AVPaletteControl *palette_control = (AVPaletteControl *)avctx->extradata;
-
-    if (palette_control->palette_changed) {
-        /* load the new palette and reset the palette control */
-        ipvideo_new_palette(s, palette_control->palette);
-        palette_control->palette_changed = 0;
-    }
+    AVPaletteControl *palette_control = avctx->palctrl;
 
     s->decoding_map = buf;
     s->buf = buf + s->decoding_map_size;
@@ -936,6 +912,11 @@
 
     ipvideo_decode_opcodes(s);
 
+    if (palette_control->palette_changed) {
+        palette_control->palette_changed = 0;
+        s->current_frame.palette_has_changed = 1;
+    }
+
     *data_size = sizeof(AVFrame);
     *(AVFrame*)data = s->current_frame;
 
--- a/utils.c	Wed Oct 29 17:30:13 2003 +0000
+++ b/utils.c	Thu Oct 30 05:40:58 2003 +0000
@@ -318,6 +318,7 @@
     
     s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
     s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
+    s->palctrl = NULL;
 }
 
 /**
--- a/vqavideo.c	Wed Oct 29 17:30:13 2003 +0000
+++ b/vqavideo.c	Thu Oct 30 05:40:58 2003 +0000
@@ -120,7 +120,7 @@
     unsigned char *buf;
     int size;
 
-    unsigned char palette[PALETTE_COUNT * 4];
+    unsigned int palette[PALETTE_COUNT];
 
     int width;   /* width of a frame */
     int height;   /* height of a frame */
@@ -311,7 +311,6 @@
     unsigned int index = 0;
     int i;
     unsigned char r, g, b;
-    unsigned int *palette32;
     int index_shift;
 
     int cbf0_chunk = -1;
@@ -407,13 +406,12 @@
             return;
         }
         cpl0_chunk += CHUNK_PREAMBLE_SIZE;
-        palette32 = (unsigned int *)s->palette;
         for (i = 0; i < chunk_size / 3; i++) {
             /* scale by 4 to transform 6-bit palette -> 8-bit */
             r = s->buf[cpl0_chunk++] * 4;
             g = s->buf[cpl0_chunk++] * 4;
             b = s->buf[cpl0_chunk++] * 4;
-            palette32[i] = (r << 16) | (g << 8) | (b);
+            s->palette[i] = (r << 16) | (g << 8) | (b);
         }
     }
 
@@ -583,6 +581,7 @@
 
     /* make the palette available on the way out */
     memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
+    s->frame.palette_has_changed = 1;
 
     *data_size = sizeof(AVFrame);
     *(AVFrame*)data = s->frame;
--- a/xan.c	Wed Oct 29 17:30:13 2003 +0000
+++ b/xan.c	Thu Oct 30 05:40:58 2003 +0000
@@ -115,9 +115,8 @@
     s->avctx = avctx;
 
     if ((avctx->codec->id == CODEC_ID_XAN_WC3) && 
-        (s->avctx->extradata_size != sizeof(AVPaletteControl))) {
-        printf (" WC3 Xan video: expected extradata_size of %d\n",
-            sizeof(AVPaletteControl));
+        (s->avctx->palctrl == NULL)) {
+        printf (" WC3 Xan video: palette expected.\n");
         return -1;
     }
 
@@ -253,12 +252,13 @@
 }
 
 static void inline xan_wc3_build_palette(XanContext *s, 
-    unsigned char *palette_data)
+    unsigned int *palette_data)
 {
     int i;
     unsigned char r, g, b;
     unsigned short *palette16;
     unsigned int *palette32;
+    unsigned int pal_elem;
 
     /* transform the palette passed through the palette control structure
      * into the necessary internal format depending on colorspace */
@@ -268,9 +268,10 @@
     case PIX_FMT_RGB555:
         palette16 = (unsigned short *)s->palette;
         for (i = 0; i < PALETTE_COUNT; i++) {
-            r = *palette_data++;
-            g = *palette_data++;
-            b = *palette_data++;
+            pal_elem = palette_data[i];
+            r = (pal_elem >> 16) & 0xff;
+            g = (pal_elem >> 8) & 0xff;
+            b = pal_elem & 0xff;
             palette16[i] = 
                 ((r >> 3) << 10) |
                 ((g >> 3) <<  5) |
@@ -281,9 +282,10 @@
     case PIX_FMT_RGB565:
         palette16 = (unsigned short *)s->palette;
         for (i = 0; i < PALETTE_COUNT; i++) {
-            r = *palette_data++;
-            g = *palette_data++;
-            b = *palette_data++;
+            pal_elem = palette_data[i];
+            r = (pal_elem >> 16) & 0xff;
+            g = (pal_elem >> 8) & 0xff;
+            b = pal_elem & 0xff;
             palette16[i] = 
                 ((r >> 3) << 11) |
                 ((g >> 2) <<  5) |
@@ -293,17 +295,22 @@
 
     case PIX_FMT_RGB24:
         for (i = 0; i < PALETTE_COUNT; i++) {
-            s->palette[i * 4 + 0] = *palette_data++;
-            s->palette[i * 4 + 1] = *palette_data++;
-            s->palette[i * 4 + 2] = *palette_data++;
+            pal_elem = palette_data[i];
+            r = (pal_elem >> 16) & 0xff;
+            g = (pal_elem >> 8) & 0xff;
+            b = pal_elem & 0xff;
+            s->palette[i * 4 + 0] = r;
+            s->palette[i * 4 + 1] = g;
+            s->palette[i * 4 + 2] = b;
         }
         break;
 
     case PIX_FMT_BGR24:
         for (i = 0; i < PALETTE_COUNT; i++) {
-            r = *palette_data++;
-            g = *palette_data++;
-            b = *palette_data++;
+            pal_elem = palette_data[i];
+            r = (pal_elem >> 16) & 0xff;
+            g = (pal_elem >> 8) & 0xff;
+            b = pal_elem & 0xff;
             s->palette[i * 4 + 0] = b;
             s->palette[i * 4 + 1] = g;
             s->palette[i * 4 + 2] = r;
@@ -313,19 +320,15 @@
     case PIX_FMT_PAL8:
     case PIX_FMT_RGBA32:
         palette32 = (unsigned int *)s->palette;
-        for (i = 0; i < PALETTE_COUNT; i++) {
-            r = *palette_data++;
-            g = *palette_data++;
-            b = *palette_data++;
-            palette32[i] = (r << 16) | (g << 8) | (b);
-        }
+        memcpy (palette32, palette_data, PALETTE_COUNT * sizeof(unsigned int));
         break;
 
     case PIX_FMT_YUV444P:
         for (i = 0; i < PALETTE_COUNT; i++) {
-            r = *palette_data++;
-            g = *palette_data++;
-            b = *palette_data++;
+            pal_elem = palette_data[i];
+            r = (pal_elem >> 16) & 0xff;
+            g = (pal_elem >> 8) & 0xff;
+            b = pal_elem & 0xff;
             s->palette[i * 4 + 0] = COMPUTE_Y(r, g, b);
             s->palette[i * 4 + 1] = COMPUTE_U(r, g, b);
             s->palette[i * 4 + 2] = COMPUTE_V(r, g, b);
@@ -730,8 +733,11 @@
     }
 
     /* for PAL8, make the palette available on the way out */
-    if (s->avctx->pix_fmt == PIX_FMT_PAL8)
+    if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
         memcpy(s->current_frame.data[1], s->palette, PALETTE_COUNT * 4);
+        s->current_frame.palette_has_changed = 1;
+        s->avctx->palctrl->palette_changed = 0;
+    }
 }
 
 static void xan_wc4_decode_frame(XanContext *s) {
@@ -742,13 +748,15 @@
                             uint8_t *buf, int buf_size)
 {
     XanContext *s = avctx->priv_data;
-    AVPaletteControl *palette_control = (AVPaletteControl *)avctx->extradata;
+    AVPaletteControl *palette_control = avctx->palctrl;
     int keyframe = 0;
 
     if (palette_control->palette_changed) {
         /* load the new palette and reset the palette control */
         xan_wc3_build_palette(s, palette_control->palette);
-        palette_control->palette_changed = 0;
+        /* If pal8 we clear flag when we copy palette */
+        if (s->avctx->pix_fmt != PIX_FMT_PAL8)
+            palette_control->palette_changed = 0;
         keyframe = 1;
     }