]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/xan.c
aacenc: reduce the number of loop index variables
[ffmpeg] / libavcodec / xan.c
index 3e4ca2e030b331b8504fa6a346919843bfe3e09e..876a9a5558bccf6071cd5cc93d3daf4e5b26a268 100644 (file)
@@ -2,20 +2,20 @@
  * Wing Commander/Xan Video Decoder
  * Copyright (C) 2003 the ffmpeg project
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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,
+ * Libav 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
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -40,6 +40,8 @@
 // for av_memcpy_backptr
 #include "libavutil/lzo.h"
 
+#define RUNTIME_GAMMA 0
+
 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
@@ -356,9 +358,53 @@ static void xan_wc3_decode_frame(XanContext *s) {
     }
 }
 
-static void xan_wc4_decode_frame(XanContext *s) {
+#if RUNTIME_GAMMA
+static inline unsigned mul(unsigned a, unsigned b)
+{
+    return (a * b) >> 16;
+}
+
+static inline unsigned pow4(unsigned a)
+{
+    unsigned square = mul(a, a);
+    return mul(square, square);
+}
+
+static inline unsigned pow5(unsigned a)
+{
+    return mul(pow4(a), a);
 }
 
+static uint8_t gamma_corr(uint8_t in) {
+    unsigned lo, hi = 0xff40, target;
+    int i = 15;
+    in = (in << 2) | (in >> 6);
+    /*  equivalent float code:
+    if (in >= 252)
+        return 253;
+    return round(pow(in / 256.0, 0.8) * 256);
+    */
+    lo = target = in << 8;
+    do {
+        unsigned mid = (lo + hi) >> 1;
+        unsigned pow = pow5(mid);
+        if (pow > target) hi = mid;
+        else lo = mid;
+    } while (--i);
+    return (pow4((lo + hi) >> 1) + 0x80) >> 8;
+}
+#else
+/**
+ * This is a gamma correction that xan3 applies to all palette entries.
+ *
+ * There is a peculiarity, namely that the values are clamped to 253 -
+ * it seems likely that this table was calculated by a buggy fixed-point
+ * implementation, the one above under RUNTIME_GAMMA behaves like this for
+ * example.
+ * The exponent value of 0.8 can be explained by this as well, since 0.8 = 4/5
+ * and thus pow(x, 0.8) is still easy to calculate.
+ * Also, the input values are first rotated to the left by 2.
+ */
 static const uint8_t gamma_lookup[256] = {
     0x00, 0x09, 0x10, 0x16, 0x1C, 0x21, 0x27, 0x2C,
     0x31, 0x35, 0x3A, 0x3F, 0x43, 0x48, 0x4C, 0x50,
@@ -393,6 +439,7 @@ static const uint8_t gamma_lookup[256] = {
     0xCE, 0xD1, 0xD5, 0xD8, 0xDB, 0xDF, 0xE2, 0xE5,
     0xE9, 0xEC, 0xEF, 0xF2, 0xF6, 0xF9, 0xFC, 0xFD
 };
+#endif
 
 static int xan_decode_frame(AVCodecContext *avctx,
                             void *data, int *data_size,
@@ -425,9 +472,15 @@ static int xan_decode_frame(AVCodecContext *avctx,
                 s->palettes = tmpptr;
                 tmpptr += s->palettes_count * AVPALETTE_COUNT;
                 for (i = 0; i < PALETTE_COUNT; i++) {
+#if RUNTIME_GAMMA
+                    int r = gamma_corr(*buf++);
+                    int g = gamma_corr(*buf++);
+                    int b = gamma_corr(*buf++);
+#else
                     int r = gamma_lookup[*buf++];
                     int g = gamma_lookup[*buf++];
                     int b = gamma_lookup[*buf++];
+#endif
                     *tmpptr++ = (r << 16) | (g << 8) | b;
                 }
                 s->palettes_count++;
@@ -459,23 +512,12 @@ static int xan_decode_frame(AVCodecContext *avctx,
     if (!s->frame_size)
         s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
 
-    if (avctx->codec->id == CODEC_ID_XAN_WC3) {
-        memcpy(s->current_frame.data[1], s->palettes + s->cur_palette * AVPALETTE_COUNT, AVPALETTE_SIZE);
-    } else {
-        AVPaletteControl *palette_control = avctx->palctrl;
-    palette_control->palette_changed = 0;
-    memcpy(s->current_frame.data[1], palette_control->palette,
-           AVPALETTE_SIZE);
-    s->current_frame.palette_has_changed = 1;
-    }
+    memcpy(s->current_frame.data[1], s->palettes + s->cur_palette * AVPALETTE_COUNT, AVPALETTE_SIZE);
 
     s->buf = buf;
     s->size = buf_size;
 
-    if (avctx->codec->id == CODEC_ID_XAN_WC3)
-        xan_wc3_decode_frame(s);
-    else if (avctx->codec->id == CODEC_ID_XAN_WC4)
-        xan_wc4_decode_frame(s);
+    xan_wc3_decode_frame(s);
 
     /* release the last frame if it is allocated */
     if (s->last_frame.data[0])
@@ -503,11 +545,12 @@ static av_cold int xan_decode_end(AVCodecContext *avctx)
 
     av_freep(&s->buffer1);
     av_freep(&s->buffer2);
+    av_freep(&s->palettes);
 
     return 0;
 }
 
-AVCodec xan_wc3_decoder = {
+AVCodec ff_xan_wc3_decoder = {
     "xan_wc3",
     AVMEDIA_TYPE_VIDEO,
     CODEC_ID_XAN_WC3,
@@ -520,17 +563,3 @@ AVCodec xan_wc3_decoder = {
     .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
 };
 
-/*
-AVCodec xan_wc4_decoder = {
-    "xan_wc4",
-    AVMEDIA_TYPE_VIDEO,
-    CODEC_ID_XAN_WC4,
-    sizeof(XanContext),
-    xan_decode_init,
-    NULL,
-    xan_decode_end,
-    xan_decode_frame,
-    CODEC_CAP_DR1,
-    .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
-};
-*/