OSDN Git Service

fix bitstream decoding.
[qtheora/main.git] / Lib / QTheoraEx / FrameDecoder_Impl.h
index a07c16d..0446a06 100644 (file)
@@ -65,6 +65,8 @@ static BOOL FrameDecoder_DecodeCodedBlockFlag(
                t->BlocksCoded[i] = 0;
        }
 
+       memset(t->Count, 0, sizeof(UINT8) * t->Index->Blocks);
+
        /* PARTIAL or FULL */
 
        FETCH_BITS_I(x, 1)
@@ -182,6 +184,13 @@ static BOOL FrameDecoder_DecodeMacroBlockCodingModes(
        UINT8* m   = t->MBMode;
        UINT8* end = m + t->Index->MC;
 
+       const UINT16* mbi = t->Index->MBIndex;
+       const UINT16* cbi = t->Index->CBIndex;
+
+       UINT8* m0 = t->BMode;
+       UINT8* m1 = m0 + t->Index->BC[0];
+       UINT8* m2 = m1 + t->Index->BC[1];
+
        const UINT8* c = t->Count;
 
        FETCH_BITS_I(scheme, 3)
@@ -197,7 +206,7 @@ static BOOL FrameDecoder_DecodeMacroBlockCodingModes(
        }
 
        if (scheme != 7) {
-               for (; m < end; m++, c += 4) {
+               for (; m < end; m++, c += 4, mbi += 4, cbi++) {
                        UINT8 mm = 0;
 
                        if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff || c[3] != 0xff) {
@@ -209,10 +218,17 @@ static BOOL FrameDecoder_DecodeMacroBlockCodingModes(
                        }
 
                        *m = mm;
+
+                       m0[mbi[0]] = mm;
+                       m0[mbi[1]] = mm;
+                       m0[mbi[2]] = mm;
+                       m0[mbi[3]] = mm;
+                       m1[cbi[0]] = mm;
+                       m2[cbi[0]] = mm;
                }
 
        } else {
-               for (; m < end; m++, c += 4) {
+               for (; m < end; m++, c += 4, mbi += 4, cbi++) {
                        UINT8 mm = 0;
 
                        if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff || c[3] != 0xff) {
@@ -222,6 +238,13 @@ static BOOL FrameDecoder_DecodeMacroBlockCodingModes(
                        }
 
                        *m = mm;
+
+                       m0[mbi[0]] = mm;
+                       m0[mbi[1]] = mm;
+                       m0[mbi[2]] = mm;
+                       m0[mbi[3]] = mm;
+                       m1[cbi[0]] = mm;
+                       m2[cbi[0]] = mm;
                }
        }
 
@@ -241,9 +264,9 @@ static BOOL FrameDecoder_DecodeMotionVectors(
        const UINT8* m   = t->MBMode;
        const UINT8* end = m + t->Index->MC;
 
-       const UINT8* c = t->Count;
+       const UINT16* mbi = t->Index->MBIndex;
 
-       MotionVector_t* mv = t->MV;
+       const UINT8* c = t->Count;
 
        MotionVector_t last[2] = { { 0, 0 }, { 0, 0 } };
 
@@ -254,7 +277,9 @@ static BOOL FrameDecoder_DecodeMotionVectors(
 
        Decode = (x == 0) ? DecodeMV0 : DecodeMV1;
 
-       for (; m < end; m++, c += 4, mv += 4) {
+       for (; m < end; m++, c += 4, mbi += 4) {
+               MotionVector_t* mv = t->MV + mbi[0];
+
                switch (*m) {
                case 2: /* INTER_MV */
                        LOAD_BITS
@@ -286,27 +311,26 @@ static BOOL FrameDecoder_DecodeMotionVectors(
 
                case 7: /* INTER_MV_FOUR */
                {
-                       const MotionVector_t* mv0 = mv;
-
                        INT32 i;
                        for (i = 0; i < 4; i++) {
-                               /* Check: Raster Order */
+                               MotionVector_t* v = t->MV + mbi[i];
+
                                if (c[i] != 0xff) {
                                        LOAD_BITS
                                        x = GET_BITS_I(16);
-                                       s = Decode(mv + i, x);
+                                       s = Decode(v, x);
                                        RETIRE_BITS(s)
 
-                                       mv0 = mv + i;
+                                       mv = v;
 
                                } else {
-                                       mv[i].X = 0;
-                                       mv[i].Y = 0;
+                                       v->X = 0;
+                                       v->Y = 0;
                                }
                        }
 
                        last[1] = last[0];
-                       last[0] = *mv0;
+                       last[0] = *mv;
                        break;
                }
 
@@ -399,8 +423,6 @@ static BOOL FrameDecoder_DecodeBlocks(
                                }
                        }
 
-                       printf("T: %d\n", token);
-
                        if (token >= 0 && token < 7) {
                                INT32 blen = EOB_BITS_LEN[token];
                                ctx->EOB_Run = EOB_RUN_BASE[token];
@@ -448,7 +470,7 @@ static BOOL FrameDecoder_DecodeBlocks(
                        *((ctx->Run  )++) = run;
                        *((ctx->Coeff)++) = coeff;
 
-                       if (index + run + 1 >= 64) {
+                       if (index + run + 1 > 64) {
                                return FALSE;
                        }
 
@@ -461,13 +483,13 @@ static BOOL FrameDecoder_DecodeBlocks(
                } else {
                        INT32 eob = ctx->EOB_Run;
 
-                       *((ctx->Run  )++) = -1;
-                       *((ctx->Coeff)++) = eob;
-
                        if (eob > count) {
                                eob = count;
                        }
 
+                       *((ctx->Run  )++) = -1;
+                       *((ctx->Coeff)++) = eob;
+
                        for (i = index + 1; i < 64; i++) {
                                ctx->BlocksCoded[i] -= eob;
                        }
@@ -531,8 +553,6 @@ static BOOL FrameDecoder_DecodeDCTCoefficients(
 
                                ctx.BlocksCoded = Coded[k];
 
-                               printf("DCT: %d : %d\n", index, k);
-
                                if (!FrameDecoder_DecodeBlocks(
                                        t,
                                        d,
@@ -568,6 +588,8 @@ static BOOL FrameDecoder_Decode(
        if (t->Header.Type == 0) { /* Intra */
                memset(t->Count, 0, sizeof(UINT8) * t->Index->Blocks);
 
+               memset(t->BMode, 1, t->Index->Blocks);
+
                t->BlocksCoded[0] = t->Index->BC[0];
                t->BlocksCoded[1] = t->Index->BC[1];
                t->BlocksCoded[2] = t->Index->BC[2];
@@ -592,6 +614,62 @@ static BOOL FrameDecoder_Decode(
 
        /* */
 
+       DecodeDCCoefficients(t);
+
+#if 0
+       {
+               INT32 i;
+               INT32 x, y;
+
+               const INT16* dc = t->DC;
+
+               for (i = 0; i < 3; i++) {
+                       INT32 bx = t->Index->BX[i];
+                       INT32 by = t->Index->BY[i];
+
+                       printf("DC: %d\n", i);
+                       for (y = 0; y < by; y++) {
+                               for (x = 0; x < bx; x++, dc++) {
+                                       if (x > 0) {
+                                               printf(" ");
+                                       }
+                                       printf("%d", *dc);
+                               }
+                               printf("\n");
+                       }
+               }
+       }
+#endif
+
+       /* */
+
+       UndoDCPrediction(t);
+
+       {
+               INT32 i;
+               INT32 x, y;
+
+               const INT16* dc = t->DC;
+
+               for (i = 0; i < 3; i++) {
+                       INT32 bx = t->Index->BX[i];
+                       INT32 by = t->Index->BY[i];
+
+                       printf("DC: %d\n", i);
+                       for (y = 0; y < by; y++) {
+                               for (x = 0; x < bx; x++, dc++) {
+                                       if (x > 0) {
+                                               printf(" ");
+                                       }
+                                       printf("%d", *dc);
+                               }
+                               printf("\n");
+                       }
+               }
+       }
+
+       /* */
+
        return TRUE;
 }