I've picked Diego's version and changed some things I didn't like and updated
to the current HEAD. Samples from our repository look Good Enough^TM.
>From 1690804a42ef37fb6c6cb883430af1287d84e76e Mon Sep 17 00:00:00 2001
From: Eli Friedman <eli.fried...@gmail.com>
Date: Fri, 16 Dec 2011 21:30:27 +0100
Subject: [PATCH] Escape 130 (RPL) decoder

Some fixes provided by Paul B Mahol <one...@gmail.com>
and Michael Niedermayer <michae...@gmx.at> and me.

Signed-off-by: Diego Biurrun <di...@biurrun.de>
---
 Changelog              |    1 +
 doc/general.texi       |    1 +
 libavcodec/Makefile    |    1 +
 libavcodec/allcodecs.c |    1 +
 libavcodec/avcodec.h   |    1 +
 libavcodec/escape130.c |  321 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h   |    2 +-
 libavformat/rpl.c      |    2 -
 8 files changed, 327 insertions(+), 3 deletions(-)
 create mode 100644 libavcodec/escape130.c

diff --git a/Changelog b/Changelog
index cbe61f5..5889425 100644
--- a/Changelog
+++ b/Changelog
@@ -5,6 +5,7 @@ releases are sorted from youngest to oldest.
 version <next>:
 
 - GSM audio parser
+- Escape 130 video decoder
 
 
 version 0.8_beta2:
diff --git a/doc/general.texi b/doc/general.texi
index 65d65bb..8f9d6de 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -464,6 +464,7 @@ following image formats are supported:
 @item Electronic Arts TGQ video  @tab     @tab  X
 @item Electronic Arts TQI video  @tab     @tab  X
 @item Escape 124             @tab     @tab  X
+@item Escape 130             @tab     @tab  X
 @item FFmpeg video codec #1  @tab  X  @tab  X
     @tab experimental lossless codec (fourcc: FFV1)
 @item Flash Screen Video v1  @tab  X  @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 1e8d09b..39d373f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -142,6 +142,7 @@ OBJS-$(CONFIG_EIGHTBPS_DECODER)        += 8bps.o
 OBJS-$(CONFIG_EIGHTSVX_EXP_DECODER)    += 8svx.o
 OBJS-$(CONFIG_EIGHTSVX_FIB_DECODER)    += 8svx.o
 OBJS-$(CONFIG_ESCAPE124_DECODER)       += escape124.o
+OBJS-$(CONFIG_ESCAPE130_DECODER)       += escape130.o
 OBJS-$(CONFIG_FFV1_DECODER)            += ffv1.o rangecoder.o
 OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1.o rangecoder.o
 OBJS-$(CONFIG_FFVHUFF_DECODER)         += huffyuv.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 5289692..9832cb4 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -106,6 +106,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (EIGHTSVX_EXP, eightsvx_exp);
     REGISTER_DECODER (EIGHTSVX_FIB, eightsvx_fib);
     REGISTER_DECODER (ESCAPE124, escape124);
+    REGISTER_DECODER (ESCAPE130, escape130);
     REGISTER_ENCDEC  (FFV1, ffv1);
     REGISTER_ENCDEC  (FFVHUFF, ffvhuff);
     REGISTER_ENCDEC  (FLASHSV, flashsv);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index c195ad5..575137d 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -254,6 +254,7 @@ enum CodecID {
     CODEC_ID_VBLE,
     CODEC_ID_DXTORY,
     CODEC_ID_V410,
+    CODEC_ID_ESCAPE130,
 
     /* various PCM "codecs" */
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/escape130.c b/libavcodec/escape130.c
new file mode 100644
index 0000000..f48f361
--- /dev/null
+++ b/libavcodec/escape130.c
@@ -0,0 +1,321 @@
+/*
+ * Escape 130 Video Decoder
+ * Copyright (C) 2008 Eli Friedman (eli.friedman <at> gmail.com)
+ *
+ * This file is part of Libav.
+ *
+ * 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.
+ *
+ * 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+#define BITSTREAM_READER_LE
+#include "get_bits.h"
+
+typedef struct Escape130Context {
+    AVFrame  frame;
+    uint8_t *old_y_avg;
+} Escape130Context;
+
+static const uint8_t offset_table[] = { 2, 4, 10, 20 };
+static const int8_t sign_table[64][4] = {
+    {  0,  0,  0,  0 },
+    { -1,  1,  0,  0 },
+    {  1, -1,  0,  0 },
+    { -1,  0,  1,  0 },
+    { -1,  1,  1,  0 },
+    {  0, -1,  1,  0 },
+    {  1, -1,  1,  0 },
+    { -1, -1,  1,  0 },
+    {  1,  0, -1,  0 },
+    {  0,  1, -1,  0 },
+    {  1,  1, -1,  0 },
+    { -1,  1, -1,  0 },
+    {  1, -1, -1,  0 },
+    { -1,  0,  0,  1 },
+    { -1,  1,  0,  1 },
+    {  0, -1,  0,  1 },
+
+    {  0,  0,  0,  0 },
+    {  1, -1,  0,  1 },
+    { -1, -1,  0,  1 },
+    { -1,  0,  1,  1 },
+    { -1,  1,  1,  1 },
+    {  0, -1,  1,  1 },
+    {  1, -1,  1,  1 },
+    { -1, -1,  1,  1 },
+    {  0,  0, -1,  1 },
+    {  1,  0, -1,  1 },
+    { -1,  0, -1,  1 },
+    {  0,  1, -1,  1 },
+    {  1,  1, -1,  1 },
+    { -1,  1, -1,  1 },
+    {  0, -1, -1,  1 },
+    {  1, -1, -1,  1 },
+
+    {  0,  0,  0,  0 },
+    { -1, -1, -1,  1 },
+    {  1,  0,  0, -1 },
+    {  0,  1,  0, -1 },
+    {  1,  1,  0, -1 },
+    { -1,  1,  0, -1 },
+    {  1, -1,  0, -1 },
+    {  0,  0,  1, -1 },
+    {  1,  0,  1, -1 },
+    { -1,  0,  1, -1 },
+    {  0,  1,  1, -1 },
+    {  1,  1,  1, -1 },
+    { -1,  1,  1, -1 },
+    {  0, -1,  1, -1 },
+    {  1, -1,  1, -1 },
+    { -1, -1,  1, -1 },
+
+    {  0,  0,  0,  0 },
+    {  1,  0, -1, -1 },
+    {  0,  1, -1, -1 },
+    {  1,  1, -1, -1 },
+    { -1,  1, -1, -1 },
+    {  1, -1, -1, -1 }
+};
+
+static const int8_t luma_adjust[] = { -4, -3, -2, -1, 1, 2, 3, 4 };
+
+static const int8_t chroma_adjust[2][8] = {
+    { 1, 1, 0, -1, -1, -1,  0,  1 },
+    { 0, 1, 1,  1,  0, -1, -1, -1 }
+};
+
+/**
+ * Initialize the decoder.
+ * @param avctx decoder context
+ * @return 0 success, negative on error
+ */
+static av_cold int escape130_decode_init(AVCodecContext *avctx)
+{
+    Escape130Context *s = avctx->priv_data;
+    avctx->pix_fmt = PIX_FMT_YUV420P;
+
+    if ((avctx->width & 1) || (avctx->height & 1)) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Dimensions should be multiple of two.\n");
+        return AVERROR(EINVAL);
+    }
+
+    s->old_y_avg = av_malloc(avctx->width * avctx->height / 4);
+    if (!s->old_y_avg) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Could not allocate buffer.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+static av_cold int escape130_decode_close(AVCodecContext *avctx)
+{
+    Escape130Context *s = avctx->priv_data;
+
+    if (s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
+    av_freep(&s->old_y_avg);
+
+    return 0;
+}
+
+static unsigned decode_skip_count(GetBitContext* gb)
+{
+    unsigned value;
+
+    value = get_bits1(gb);
+    if (value)
+        return 0;
+
+    value = get_bits(gb, 3);
+    if (value)
+        return value;
+
+    value = get_bits(gb, 8);
+    if (value)
+        return value + 7;
+
+    value = get_bits(gb, 15);
+    if (value)
+        return value + 262;
+
+    return -1;
+}
+
+/**
+ * Decode a single frame.
+ * @param avctx decoder context
+ * @param data decoded frame
+ * @param data_size size of the decoded frame
+ * @param buf input buffer
+ * @param buf_size input buffer size
+ * @return 0 success, -1 on error
+ */
+static int escape130_decode_frame(AVCodecContext *avctx, void *data,
+                                  int *data_size, AVPacket *avpkt)
+{
+    const uint8_t *buf  = avpkt->data;
+    int buf_size        = avpkt->size;
+    Escape130Context *s = avctx->priv_data;
+    GetBitContext gb;
+    AVFrame new_frame = { { 0 } };
+
+    uint8_t *old_y, *old_cb, *old_cr,
+            *new_y, *new_cb, *new_cr;
+    unsigned old_y_stride, old_cb_stride, old_cr_stride,
+             new_y_stride, new_cb_stride, new_cr_stride;
+    unsigned total_blocks = avctx->width * avctx->height / 4,
+             block_index, block_x = 0;
+    unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;
+    int skip = -1, y_avg = 0, i;
+    uint8_t *ya = s->old_y_avg;
+
+    // first 16 bytes are header; no useful information in here
+    if (buf_size <= 16) {
+        av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    new_frame.reference = 3;
+    if (avctx->get_buffer(avctx, &new_frame)) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return -1;
+    }
+
+    init_get_bits(&gb, buf + 16, (buf_size - 16) * 8);
+
+    new_y  = new_frame.data[0];
+    new_cb = new_frame.data[1];
+    new_cr = new_frame.data[2];
+    new_y_stride  = new_frame.linesize[0];
+    new_cb_stride = new_frame.linesize[1];
+    new_cr_stride = new_frame.linesize[2];
+    old_y  = s->frame.data[0];
+    old_cb = s->frame.data[1];
+    old_cr = s->frame.data[2];
+    old_y_stride  = s->frame.linesize[0];
+    old_cb_stride = s->frame.linesize[1];
+    old_cr_stride = s->frame.linesize[2];
+
+    for (block_index = 0; block_index < total_blocks; block_index++) {
+        // Note that this call will make us skip the rest of the blocks
+        // if the frame prematurely ends
+        if (skip == -1)
+            skip = decode_skip_count(&gb);
+
+        if (skip) {
+            if (old_y) {
+                y[0] = old_y[0] >> 2;
+                y[1] = old_y[1] >> 2;
+                y[2] = old_y[old_y_stride]     >> 2;
+                y[3] = old_y[old_y_stride + 1] >> 2;
+                y_avg = ya[0];
+                cb = old_cb[0] >> 3;
+                cr = old_cr[0] >> 3;
+            } else {
+                y_avg = y[0] = y[1] = y[2] = y[3] = 0;
+                cb = cr = 0x10;
+            }
+        } else {
+            if (get_bits1(&gb)) {
+                unsigned sign_selector       = get_bits(&gb, 6);
+                unsigned difference_selector = get_bits(&gb, 2);
+                y_avg = 2 * get_bits(&gb, 5);
+                for (i = 0; i < 4; i++) {
+                    y[i] = av_clip(y_avg + offset_table[difference_selector] *
+                                   sign_table[sign_selector][i], 0, 63);
+                }
+            } else if (get_bits1(&gb)) {
+                if (get_bits1(&gb)) {
+                    y_avg = get_bits(&gb, 6);
+                } else {
+                    unsigned adjust_index = get_bits(&gb, 3);
+                    y_avg = (y_avg + luma_adjust[adjust_index]) & 63;
+                }
+                for (i = 0; i < 4; i++)
+                    y[i] = y_avg;
+            }
+
+            if (get_bits1(&gb)) {
+                if (get_bits1(&gb)) {
+                    cb = get_bits(&gb, 5);
+                    cr = get_bits(&gb, 5);
+                } else {
+                    unsigned adjust_index = get_bits(&gb, 3);
+                    cb = (cb + chroma_adjust[0][adjust_index]) & 31;
+                    cr = (cr + chroma_adjust[1][adjust_index]) & 31;
+                }
+            }
+        }
+        *ya++ = y_avg;
+
+        new_y[0]                = (y[0] << 2) | (y[0] >> 4);
+        new_y[1]                = (y[1] << 2) | (y[1] >> 4);
+        new_y[new_y_stride]     = (y[2] << 2) | (y[2] >> 4);
+        new_y[new_y_stride + 1] = (y[3] << 2) | (y[3] >> 4);
+        *new_cb = (cb << 3) | (((cb - 0x10) >> 2) & 0x7);
+        *new_cr = (cr << 3) | (((cr - 0x10) >> 2) & 0x7);
+
+        if (old_y) {
+            old_y += 2;
+            old_cb++;
+            old_cr++;
+        }
+        new_y += 2;
+        new_cb++;
+        new_cr++;
+        block_x++;
+        if (block_x * 2 == avctx->width) {
+            block_x = 0;
+            if (old_y) {
+                old_y  += old_y_stride * 2  - avctx->width;
+                old_cb += old_cb_stride     - avctx->width / 2;
+                old_cr += old_cr_stride     - avctx->width / 2;
+            }
+            new_y  += new_y_stride * 2  - avctx->width;
+            new_cb += new_cb_stride     - avctx->width / 2;
+            new_cr += new_cr_stride     - avctx->width / 2;
+        }
+
+        skip--;
+    }
+
+    av_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n",
+            buf_size, get_bits_count(&gb) >> 3);
+
+    if (s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
+    *(AVFrame*)data = s->frame = new_frame;
+    *data_size = sizeof(AVFrame);
+
+    return buf_size;
+}
+
+
+AVCodec ff_escape130_decoder = {
+    .name           = "escape130",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = CODEC_ID_ESCAPE130,
+    .priv_data_size = sizeof(Escape130Context),
+    .init           = escape130_decode_init,
+    .close          = escape130_decode_close,
+    .decode         = escape130_decode_frame,
+    .capabilities   = CODEC_CAP_DR1,
+    .long_name      = NULL_IF_CONFIG_SMALL("Escape 130"),
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 87838c0..d088f0e 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 33
+#define LIBAVCODEC_VERSION_MINOR 34
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavformat/rpl.c b/libavformat/rpl.c
index 0fa00f3..bf5fe0a 100644
--- a/libavformat/rpl.c
+++ b/libavformat/rpl.c
@@ -164,11 +164,9 @@ static int rpl_read_header(AVFormatContext *s, AVFormatParameters *ap)
             // The header is wrong here, at least sometimes
             vst->codec->bits_per_coded_sample = 16;
             break;
-#if 0
         case 130:
             vst->codec->codec_id = CODEC_ID_ESCAPE130;
             break;
-#endif
         default:
             av_log(s, AV_LOG_WARNING,
                    "RPL video format %i not supported yet!\n",
-- 
1.7.0.4

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to