[FFmpeg-soc] [soc]: r287 - in jpeg2000: aec.h aecenc.c checkout.sh ffmpeg.patch j2k.h j2kenc.c

k.nowosad subversion at mplayerhq.hu
Sat Jun 30 16:14:54 CEST 2007


Author: k.nowosad
Date: Sat Jun 30 16:14:53 2007
New Revision: 287

Log:
moved the arithmetic coder into a separate file


Added:
   jpeg2000/aec.h
   jpeg2000/aecenc.c
Modified:
   jpeg2000/checkout.sh
   jpeg2000/ffmpeg.patch
   jpeg2000/j2k.h
   jpeg2000/j2kenc.c

Added: jpeg2000/aec.h
==============================================================================
--- (empty file)
+++ jpeg2000/aec.h	Sat Jun 30 16:14:53 2007
@@ -0,0 +1,123 @@
+/*
+ * Arithmetic entropy coder
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * 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
+ *
+ */
+
+/**
+ * Arithmetic entropy coder
+ * @file aec.h
+ * @author Kamil Nowosad
+ */
+
+/* arithmetic entropy coder context */
+//TODO: optimize [nice solution in openjpeg]
+
+#ifndef AEC_H
+#define AEC_H
+
+#include "avcodec.h"
+
+#define AEC_CX_UNI 17
+#define AEC_CX_RL  18
+
+typedef struct {
+        unsigned int qe;
+        unsigned int nmps;
+        unsigned int nlps;
+        unsigned int sw;
+} AecCxState;
+
+const static AecCxState cx_states[47] = {
+    {0x5601,  1,  1, 1},
+    {0x3401,  2,  6, 0},
+    {0x1801,  3,  9, 0},
+    {0x0AC1,  4, 12, 0},
+    {0x0521,  5, 29, 0},
+    {0x0221, 38, 33, 0},
+    {0x5601,  7,  6, 1},
+    {0x5401,  8, 14, 0},
+    {0x4801,  9, 14, 0},
+    {0x3801, 10, 14, 0},
+    {0x3001, 11, 17, 0},
+    {0x2401, 12, 18, 0},
+    {0x1C01, 13, 20, 0},
+    {0x1601, 29, 21, 0},
+    {0x5601, 15, 14, 1},
+    {0x5401, 16, 14, 0},
+    {0x5101, 17, 15, 0},
+    {0x4801, 18, 16, 0},
+    {0x3801, 19, 17, 0},
+    {0x3401, 20, 18, 0},
+    {0x3001, 21, 19, 0},
+    {0x2801, 22, 19, 0},
+    {0x2401, 23, 20, 0},
+    {0x2201, 24, 21, 0},
+    {0x1C01, 25, 22, 0},
+    {0x1801, 26, 23, 0},
+    {0x1601, 27, 24, 0},
+    {0x1401, 28, 25, 0},
+    {0x1201, 29, 26, 0},
+    {0x1101, 30, 27, 0},
+    {0x0AC1, 31, 28, 0},
+    {0x09C1, 32, 29, 0},
+    {0x08A1, 33, 30, 0},
+    {0x0521, 34, 31, 0},
+    {0x0441, 35, 32, 0},
+    {0x02A1, 36, 33, 0},
+    {0x0221, 37, 34, 0},
+    {0x0141, 38, 35, 0},
+    {0x0111, 39, 36, 0},
+    {0x0085, 40, 37, 0},
+    {0x0049, 41, 38, 0},
+    {0x0025, 42, 39, 0},
+    {0x0015, 43, 40, 0},
+    {0x0009, 44, 41, 0},
+    {0x0005, 45, 42, 0},
+    {0x0001, 45, 43, 0},
+    {0x5601, 46, 46, 0}
+};
+
+typedef struct {
+    unsigned int state;
+    unsigned int mps;
+} AecContext;
+
+typedef struct {
+    uint8_t *bp, *bpstart;
+    unsigned int a;
+    unsigned int c;
+    unsigned int ct;
+    AecContext contexts[19];
+    AecContext *curctx;
+} AecState;
+
+void aec_initenc(AecState *aec, uint8_t *bp);
+
+/**
+ * code bit d with context cx
+ * */
+void aec_encode(AecState *aec, int cx, int d);
+
+/**
+ * flush the encoder [returns number of bytes encoded]
+ * */
+int aec_flush(AecState *aec);
+
+#endif

Added: jpeg2000/aecenc.c
==============================================================================
--- (empty file)
+++ jpeg2000/aecenc.c	Sat Jun 30 16:14:53 2007
@@ -0,0 +1,159 @@
+/*
+ * Arithmetic entropy coder
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * 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
+ *
+ */
+
+/**
+ * Arithmetic entropy coder
+ * @file aecenc.c
+ * @author Kamil Nowosad
+ */
+
+#include "aec.h"
+
+static void byteout_l(AecState *aec)
+{
+    aec->bp++;
+    *aec->bp = aec->c >> 19;
+    aec->c &= 0x7ffff;
+    aec->ct = 8;
+}
+
+static void byteout_r(AecState *aec)
+{
+    aec->bp++;
+    *aec->bp = aec->c >> 20;
+    aec->c &= 0xfffff;
+    aec->ct = 7;
+}
+
+static void byteout(AecState *aec)
+{
+    if (*aec->bp == 0xff){
+        byteout_r(aec);
+    }
+    else
+    {
+        if ((aec->c & 0x8000000) == 0){
+            byteout_l(aec);
+        }
+        else{
+            (*aec->bp)++;
+            if (*aec->bp == 0xff){
+                aec->c &= 0x7ffffff;
+                byteout_r(aec);
+            }
+            else{
+                byteout_l(aec);
+            }
+        }
+    }
+}
+
+static void renorme(AecState *aec)
+{
+    do{
+        aec->a = aec->a << 1;
+        aec->c = aec->c << 1;
+        aec->ct--;
+        if (!aec->ct)
+            byteout(aec);
+    }
+    while ((aec->a & 0x8000) == 0);
+}
+
+static void codelps(AecState *aec)
+{
+    int qe = cx_states[aec->curctx->state].qe;
+    aec->a -= qe;
+    if (aec->a < qe)
+        aec->c += qe;
+    else
+        aec->a = qe;
+    if (cx_states[aec->curctx->state].sw)
+        aec->curctx->mps = 1 - aec->curctx->mps;
+    aec->curctx->state = cx_states[aec->curctx->state].nlps;
+    renorme(aec);
+}
+
+static void codemps(AecState *aec)
+{
+    int qe = cx_states[aec->curctx->state].qe;
+    aec->a -= qe;
+    if ((aec->a & 0x8000) == 0){
+        if (aec->a < qe)
+            aec->a = qe;
+        else
+            aec->c += qe;
+        aec->curctx->state = cx_states[aec->curctx->state].nmps;
+        renorme(aec);
+    }
+    else
+        aec->c += qe;
+}
+
+static void setbits(AecState *aec)
+{
+    int tmp = aec->c + aec->a;
+    aec->c |= 0xffff;
+    if (aec->c >= tmp)
+        aec->c -= 0x8000;
+}
+
+void aec_initenc(AecState *aec, uint8_t *bp)
+{
+    bzero(aec->contexts, 19*sizeof(AecContext));
+    aec->contexts[AEC_CX_UNI].state = 46;
+    aec->contexts[AEC_CX_RL].state = 3;
+    aec->contexts[0].state = 4;
+    aec->curctx = aec->contexts;
+
+    aec->a = 0x8000;
+    aec->c = 0;
+    aec->bp = bp-1;
+    aec->bpstart = bp;
+    if (*aec->bp == 0xff)
+        aec->ct = 13;
+    else
+        aec->ct = 12;
+}
+
+void aec_encode(AecState *aec, int cx, int d)
+{
+    aec->curctx = aec->contexts + cx;
+    if (aec->curctx->mps == d){
+        codemps(aec);
+    }
+    else{
+        codelps(aec);
+    }
+}
+
+int aec_flush(AecState *aec)
+{
+    setbits(aec);
+    aec->c = aec->c << aec->ct;
+    byteout(aec);
+    aec->c = aec->c << aec->ct;
+    byteout(aec);
+    if (*aec->bp != 0xff)
+        aec->bp++;
+    return aec->bp - aec->bpstart;
+}

Modified: jpeg2000/checkout.sh
==============================================================================
--- jpeg2000/checkout.sh	(original)
+++ jpeg2000/checkout.sh	Sat Jun 30 16:14:53 2007
@@ -6,4 +6,6 @@ patch -p0 <../../ffmpeg.patch
 echo "copying the jpeg2000 files to ffmpeg/libavcodec"
 ln -s ../../j2kenc.c j2kenc.c
 ln -s ../../j2k.h j2k.h
+ln -s ../../aecenc.c aecenc.c
+ln -s ../../aec.h aec.h
 echo "Done, now just do a regular configure and make to build."

Modified: jpeg2000/ffmpeg.patch
==============================================================================
--- jpeg2000/ffmpeg.patch	(original)
+++ jpeg2000/ffmpeg.patch	Sat Jun 30 16:14:53 2007
@@ -1,18 +1,18 @@
 Index: Makefile
 ===================================================================
---- Makefile	(wersja 9417)
+--- Makefile	(wersja 9451)
 +++ Makefile	(kopia robocza)
 @@ -93,6 +93,7 @@
  OBJS-$(CONFIG_INDEO3_DECODER)          += indeo3.o
  OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
  OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o
-+OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o
++OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o aecenc.o
  OBJS-$(CONFIG_JPEGLS_DECODER)          += jpeglsdec.o jpegls.o mjpegdec.o mjpeg.o golomb.o
  OBJS-$(CONFIG_JPEGLS_ENCODER)          += jpeglsenc.o jpegls.o golomb.o
  OBJS-$(CONFIG_KMVC_DECODER)            += kmvc.o
 Index: allcodecs.c
 ===================================================================
---- allcodecs.c	(wersja 9417)
+--- allcodecs.c	(wersja 9451)
 +++ allcodecs.c	(kopia robocza)
 @@ -92,6 +92,7 @@
      REGISTER_DECODER(INDEO3, indeo3);
@@ -24,7 +24,7 @@ Index: allcodecs.c
      REGISTER_ENCODER(LIBXVID, libxvid);
 Index: allcodecs.h
 ===================================================================
---- allcodecs.h	(wersja 9417)
+--- allcodecs.h	(wersja 9451)
 +++ allcodecs.h	(kopia robocza)
 @@ -40,6 +40,7 @@
  extern AVCodec h264_encoder;

Modified: jpeg2000/j2k.h
==============================================================================
--- jpeg2000/j2k.h	(original)
+++ jpeg2000/j2k.h	Sat Jun 30 16:14:53 2007
@@ -29,6 +29,8 @@
 #ifndef _J2K_H_
 #define _J2K_H_
 
+#include "aec.h"
+
 enum J2kMarkers{
     J2K_SOC = 0xff4f,
     J2K_SIZ = 0xff51,
@@ -53,79 +55,6 @@ enum J2kMarkers{
 };
 
 
-/* arithmetic entropy coder context */
-//TODO: optimize [nice solution in openjpeg]
-typedef struct {
-        unsigned int qe;
-        unsigned int nmps;
-        unsigned int nlps;
-        unsigned int sw;
-} J2kAecState;
-
-const static J2kAecState aec_cx_states[47] = {
-    {0x5601,  1,  1, 1},
-    {0x3401,  2,  6, 0},
-    {0x1801,  3,  9, 0},
-    {0x0AC1,  4, 12, 0},
-    {0x0521,  5, 29, 0},
-    {0x0221, 38, 33, 0},
-    {0x5601,  7,  6, 1},
-    {0x5401,  8, 14, 0},
-    {0x4801,  9, 14, 0},
-    {0x3801, 10, 14, 0},
-    {0x3001, 11, 17, 0},
-    {0x2401, 12, 18, 0},
-    {0x1C01, 13, 20, 0},
-    {0x1601, 29, 21, 0},
-    {0x5601, 15, 14, 1},
-    {0x5401, 16, 14, 0},
-    {0x5101, 17, 15, 0},
-    {0x4801, 18, 16, 0},
-    {0x3801, 19, 17, 0},
-    {0x3401, 20, 18, 0},
-    {0x3001, 21, 19, 0},
-    {0x2801, 22, 19, 0},
-    {0x2401, 23, 20, 0},
-    {0x2201, 24, 21, 0},
-    {0x1C01, 25, 22, 0},
-    {0x1801, 26, 23, 0},
-    {0x1601, 27, 24, 0},
-    {0x1401, 28, 25, 0},
-    {0x1201, 29, 26, 0},
-    {0x1101, 30, 27, 0},
-    {0x0AC1, 31, 28, 0},
-    {0x09C1, 32, 29, 0},
-    {0x08A1, 33, 30, 0},
-    {0x0521, 34, 31, 0},
-    {0x0441, 35, 32, 0},
-    {0x02A1, 36, 33, 0},
-    {0x0221, 37, 34, 0},
-    {0x0141, 38, 35, 0},
-    {0x0111, 39, 36, 0},
-    {0x0085, 40, 37, 0},
-    {0x0049, 41, 38, 0},
-    {0x0025, 42, 39, 0},
-    {0x0015, 43, 40, 0},
-    {0x0009, 44, 41, 0},
-    {0x0005, 45, 42, 0},
-    {0x0001, 45, 43, 0},
-    {0x5601, 46, 46, 0}
-};
-
-typedef struct {
-    unsigned int state;
-    unsigned int mps;
-} J2kAecContext;
-
-typedef struct {
-    uint8_t *bp, *bpstart;
-    unsigned int a;
-    unsigned int c;
-    unsigned int ct;
-    J2kAecContext contexts[19];
-    J2kAecContext *curctx;
-} J2kAec;
-
 #define J2K_MAX_CBLKW 64
 #define J2K_MAX_CBLKH 64
 
@@ -151,13 +80,10 @@ typedef struct {
 #define J2K_T1_SIG    0x2000
 #define J2K_T1_REF    0x4000
 
-#define J2K_T1_CTX_RL  17
-#define J2K_T1_CTX_UNI 18
-
 typedef struct {
     int data[J2K_MAX_CBLKW][J2K_MAX_CBLKH];
     int flags[J2K_MAX_CBLKW+2][J2K_MAX_CBLKH+2];
-    J2kAec aec;
+    AecState aec;
 } J2kT1Context;
 
 #endif

Modified: jpeg2000/j2kenc.c
==============================================================================
--- jpeg2000/j2kenc.c	(original)
+++ jpeg2000/j2kenc.c	Sat Jun 30 16:14:53 2007
@@ -639,139 +639,6 @@ static void dwt_encode53(J2kEncoderConte
     av_free(ppu);
 }
 
-/* arithmetic entropy coder routines: */
-static void aec_initenc(J2kAec *aec, uint8_t *bp)
-{
-    bzero(aec->contexts, 19*sizeof(J2kAecContext));
-    aec->contexts[J2K_T1_CTX_UNI].state = 46;
-    aec->contexts[J2K_T1_CTX_RL].state = 3;
-    aec->contexts[0].state = 4;
-    aec->curctx = aec->contexts;
-
-    aec->a = 0x8000;
-    aec->c = 0;
-    aec->bp = bp-1;
-    aec->bpstart = bp;
-    if (*aec->bp == 0xff)
-        aec->ct = 13;
-    else
-        aec->ct = 12;
-}
-
-static void aec_byteout_l(J2kAec *aec)
-{
-    aec->bp++;
-    *aec->bp = aec->c >> 19;
-    aec->c &= 0x7ffff;
-    aec->ct = 8;
-}
-
-static void aec_byteout_r(J2kAec *aec)
-{
-    aec->bp++;
-    *aec->bp = aec->c >> 20;
-    aec->c &= 0xfffff;
-    aec->ct = 7;
-}
-
-static void aec_byteout(J2kAec *aec)
-{
-    if (*aec->bp == 0xff){
-        aec_byteout_r(aec);
-    }
-    else
-    {
-        if ((aec->c & 0x8000000) == 0){
-            aec_byteout_l(aec);
-        }
-        else{
-            (*aec->bp)++;
-            if (*aec->bp == 0xff){
-                aec->c &= 0x7ffffff;
-                aec_byteout_r(aec);
-            }
-            else{
-                aec_byteout_l(aec);
-            }
-        }
-    }
-}
-
-static void aec_renorme(J2kAec *aec)
-{
-    do{
-        aec->a = aec->a << 1;
-        aec->c = aec->c << 1;
-        aec->ct--;
-        if (!aec->ct)
-            aec_byteout(aec);
-    }
-    while ((aec->a & 0x8000) == 0);
-}
-
-static void aec_codelps(J2kAec *aec)
-{
-    int qe = aec_cx_states[aec->curctx->state].qe;
-    aec->a -= qe;
-    if (aec->a < qe)
-        aec->c += qe;
-    else
-        aec->a = qe;
-    if (aec_cx_states[aec->curctx->state].sw)
-        aec->curctx->mps = 1 - aec->curctx->mps;
-    aec->curctx->state = aec_cx_states[aec->curctx->state].nlps;
-    aec_renorme(aec);
-}
-
-static void aec_codemps(J2kAec *aec)
-{
-    int qe = aec_cx_states[aec->curctx->state].qe;
-    aec->a -= qe;
-    if ((aec->a & 0x8000) == 0){
-        if (aec->a < qe)
-            aec->a = qe;
-        else
-            aec->c += qe;
-        aec->curctx->state = aec_cx_states[aec->curctx->state].nmps;
-        aec_renorme(aec);
-    }
-    else
-        aec->c += qe;
-}
-
-/* code bit d with context cx */
-static void aec_encode(J2kAec *aec, int cx, int d)
-{
-    aec->curctx = aec->contexts + cx;
-    if (aec->curctx->mps == d){
-        aec_codemps(aec);
-    }
-    else{
-        aec_codelps(aec);
-    }
-}
-
-static void aec_setbits(J2kAec *aec)
-{
-    int tmp = aec->c + aec->a;
-    aec->c |= 0xffff;
-    if (aec->c >= tmp)
-        aec->c -= 0x8000;
-}
-
-/* flush the encoder [returns number of bytes encoded] */
-static int aec_flush(J2kAec *aec)
-{
-    aec_setbits(aec);
-    aec->c = aec->c << aec->ct;
-    aec_byteout(aec);
-    aec->c = aec->c << aec->ct;
-    aec_byteout(aec);
-    if (*aec->bp != 0xff)
-        aec->bp++;
-    return aec->bp - aec->bpstart;
-}
-
 /* tier-1 routines */
 static int getnbctxno(int flag, int bandno)
 {
@@ -927,11 +794,11 @@ static void encode_clnpass(J2kT1Context 
                 for (rlen = 0; rlen < 4; rlen++)
                     if (abs(t1->data[i+rlen][j]) & mask)
                         break;
-                aec_encode(&t1->aec, J2K_T1_CTX_RL, rlen != 4);
+                aec_encode(&t1->aec, AEC_CX_RL, rlen != 4);
                 if (rlen == 4)
                     continue;
-                aec_encode(&t1->aec, J2K_T1_CTX_UNI, rlen >> 1);
-                aec_encode(&t1->aec, J2K_T1_CTX_UNI, rlen & 1);
+                aec_encode(&t1->aec, AEC_CX_UNI, rlen >> 1);
+                aec_encode(&t1->aec, AEC_CX_UNI, rlen & 1);
                 for (k = i + rlen; k < i + 4; k++){
                     if (!(t1->flags[k+1][j+1] & (J2K_T1_SIG | J2K_T1_VIS))){
                         int ctxno = getnbctxno(t1->flags[k+1][j+1], bandno);



More information about the FFmpeg-soc mailing list