6 #include "FrameDecoder.h"
14 #include "FrameDecoderTable.h"
18 BOOL QT_FrameDecoder_Setup(
20 const BlockIndex_t* index,
21 const SetupHeader_t* setup,
29 for (i = 0; i < 3; i++) {
30 Plane_t* r = t->Plane + i * 3;
32 INT32 cb = index->MX * 16 * index->MY * 16;
34 UINT8* p = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * cb);
40 r->Pitch = index->MX * 16;
41 r->CX = index->MX * 16;
42 r->CY = index->MY * 16;
44 for (j = 1; j < 3; j++) {
45 p = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * cb / 4);
51 r[j].Pitch = index->MX * 8;
52 r[j].CX = index->MX * 8;
53 r[j].CY = index->MY * 8;
57 for (i = 0; i < 3; i++) {
58 t->Frame[i] = t->Plane + i * 3;
63 t->SBCoded = (INT8*)QT_MemoryPool_Allocate(pool, sizeof(INT8) * index->SBlocks);
64 if (t->SBCoded == NULL) {
68 t->BMode = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->Blocks);
69 if (t->BMode == NULL) {
73 t->MV = (MotionVector_t*)QT_MemoryPool_Allocate(pool, sizeof(MotionVector_t) * index->BC[0]);
78 t->Block = (INT16*)QT_MemoryPool_Allocate(pool, sizeof(INT16) * 64 * index->Blocks);
79 if (t->Block == NULL) {
83 t->Count = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->Blocks);
84 if (t->Count == NULL) {
96 static BOOL DecodeCodedBlockFlag(
108 /* PARTIAL or FULL */
110 RunLength_Start(&bs, d);
113 for (i = 0; i < 3; i++) {
114 INT8* e = p + t->Index->SC[i];
116 if ((*p = RunLength_DecodeLong(&bs, d)) == 0) {
131 INT16* block = t->Block;
133 RunLength_Start(&bs, d);
136 for (i = 0; i < 3; i++) {
137 const UINT16* b = t->Index->BIndex[i];
138 const UINT8* c = t->Index->BCount[i];
140 INT8* e = p + t->Index->SC[i];
141 for (; p < e; p++, c++) {
143 INT16 flag = NOT_CODED;
144 if (RunLength_DecodeLong(&bs, d) != 0) {
149 for (j = 0; j < *c; j++) {
150 block[b[j] * 64] = flag;
157 block += t->Index->BC[i] * 64;
168 INT16* block = t->Block;
170 RunLength_Start(&bs, d);
173 for (i = 0; i < 3; i++) {
174 const UINT16* b = t->Index->BIndex[i];
175 const UINT8* c = t->Index->BCount[i];
177 INT8* e = p + t->Index->SC[i];
178 for (; p < e; p++, c++) {
180 for (j = 0; j < *c; j++) {
181 INT16 flag = NOT_CODED;
182 if (RunLength_DecodeShort(&bs, d) != 0) {
186 block[b[j] * 64] = flag;
193 block += t->Index->BC[i] * 64;
206 static INT32 DecodeModeX(BitDecoder_t* d)
208 INT32 code = H_MODE[QT_BitDecoder_PeekBits(d, 7)];
209 QT_BitDecoder_SkipBits(d, code & 0x0f);
213 static INT32 DecodeMode7(BitDecoder_t* d)
215 INT32 m = QT_BitDecoder_GetBits(d, 3);
221 static BOOL DecodeMacroBlockCodingModes(
228 const UINT16* mbi = t->Index->MBIndex;
229 const UINT16* cbi = t->Index->CBIndex;
230 const UINT16* end = mbi + t->Index->BC[0];
232 const INT16* block = t->Block;
234 UINT8* m0 = t->BMode;
235 UINT8* m1 = m0 + t->Index->BC[0];
236 UINT8* m2 = m1 + t->Index->BC[1];
238 INT32 (*DecodeMode)(BitDecoder_t*);
240 scheme = QT_BitDecoder_GetBits(d, 3);
241 memcpy(mode, M_MODE[scheme], 8);
245 for (i = 0; i < 8; i++) {
246 mode[QT_BitDecoder_GetBits(d, 3)] = i;
250 DecodeMode = (scheme != 7) ? DecodeModeX : DecodeMode7;
252 for (; mbi < end; mbi += 4, cbi++) {
255 if ((block[mbi[0] * 64] != NOT_CODED) ||
256 (block[mbi[1] * 64] != NOT_CODED) ||
257 (block[mbi[2] * 64] != NOT_CODED) ||
258 (block[mbi[3] * 64] != NOT_CODED)) {
259 mm = mode[DecodeMode(d)];
275 static void DecodeMV0(MotionVector_t* mv, BitDecoder_t* d)
279 b = QT_BitDecoder_PeekBits(d, 8);
281 QT_BitDecoder_SkipBits(d, MV_LEN[b]);
283 b = QT_BitDecoder_PeekBits(d, 8);
285 QT_BitDecoder_SkipBits(d, MV_LEN[b]);
288 static void DecodeMV1(MotionVector_t* mv, BitDecoder_t* d)
294 b = QT_BitDecoder_GetBits(d, 6);
299 b = QT_BitDecoder_GetBits(d, 6);
307 static BOOL DecodeMotionVectors(
311 const UINT16* mbi = t->Index->MBIndex;
312 const UINT16* end = mbi + t->Index->BC[0];
314 const UINT8* m = t->BMode;
316 const INT16* block = t->Block;
318 void (*Decode)(MotionVector_t*, BitDecoder_t*) = (QT_BitDecoder_GetBits(d, 1) == 0) ? DecodeMV0 : DecodeMV1;
320 MotionVector_t last[2] = { { 0, 0 }, { 0, 0 } };
322 for (; mbi < end; mbi += 4) {
323 MotionVector_t* mv = t->MV + mbi[0];
326 case 2: /* INTER_MV */
333 case 3: /* INTER_MV_LAST */
337 case 4: /* INTER_MV_LAST2 */
344 case 6: /* INTER_GOLDEN_MV */
348 case 7: /* INTER_MV_FOUR */
351 for (i = 0; i < 4; i++) {
352 MotionVector_t* v = t->MV + mbi[i];
354 if (block[mbi[i] * 64] != NOT_CODED) {
376 static BOOL DecodeBlock(
378 const HuffmanDecoder_t* h,
382 const UINT16* bindex,
386 INT32 eob = *eob_run;
391 const UINT16* bi = bindex;
392 const UINT16* end = bi + blocks;
394 for (; bi < end; bi++) {
395 INT16* b = block + *bi * 64;
396 UINT8* c = count + *bi;
398 if (*b == NOT_CODED || *c > index) {
403 INT32 token = QT_Huffman_LookupToken(h, d);
406 case 0: case 1: case 2:
414 eob = EOB_RUN[token - 3] + QT_BitDecoder_GetBits(d, EOB_BITS[token - 3]);
421 run = QT_BitDecoder_GetBits(d, ZR_BITS_7[token - 7]);
426 coeff = COEFF_BASE_9[token - 9];
432 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)] * (token - 10);
436 case 17: case 18: case 19:
437 case 20: case 21: case 22:
438 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)];
439 coeff *= COEFF_BASE_17[token - 17] + QT_BitDecoder_GetBits(d, COEFF_BITS_17[token - 17]);
443 case 23: case 24: case 25:
445 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)];
450 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)];
451 run = ZR_BASE_28[token - 28] + QT_BitDecoder_GetBits(d, token - 26);
455 coeff = COEFF_30_31[QT_BitDecoder_GetBits(d, 2)];
460 coeff = COEFF_30_31[QT_BitDecoder_GetBits(d, 2)];
461 run = 2 + QT_BitDecoder_GetBits(d, 1);
505 static BOOL DecodeDCTCoefficients(
511 const HuffmanDecoder_t* huff[3];
518 memset(t->Count, 0, t->Index->Blocks);
520 for (i = 0; i < 5; i++) {
522 hy = QT_BitDecoder_GetBits(d, 4);
523 hc = QT_BitDecoder_GetBits(d, 4);
526 huff[0] = t->Setup->Huffman + i * 0x10 + hy;
527 huff[1] = t->Setup->Huffman + i * 0x10 + hc;
530 for (j = 0; j < COEFFS[i]; j++, index++) {
531 INT16* block = t->Block;
532 UINT8* count = t->Count;
534 for (k = 0; k < 3; k++) {
535 INT32 blocks = t->Index->BC[k];
548 block += blocks * 64;
559 static void UndoDCPrediction(
565 INT16* block = t->Block;
567 const UINT8* mode = t->BMode;
569 for (i = 0; i < 3; i++) {
570 INT32 bx = t->Index->BX[i];
571 INT32 by = t->Index->BY[i];
573 INT16 last[3] = { 0 };
579 for (y = 0; y < by; y++) {
580 for (x = 0; x < bx; x++, idx++) {
581 INT32 dc = block[idx * 64];
582 if (dc != NOT_CODED) {
586 INT32 type = DCP_T[mode[idx]];
590 v[0] = block[i0 * 64];
591 if (v[0] != NOT_CODED && DCP_T[mode[i0]] == type) {
598 INT32 i1 = idx - bx - 1;
599 v[1] = block[i1 * 64];
600 if (v[1] != NOT_CODED && DCP_T[mode[i1]] == type) {
607 v[2] = block[i2 * 64];
608 if (v[2] != NOT_CODED && DCP_T[mode[i2]] == type) {
614 INT32 i3 = idx - bx + 1;
615 v[3] = block[i3 * 64];
616 if (v[3] != NOT_CODED && DCP_T[mode[i3]] == type) {
624 ( v[0] * DCP_W[t0][0]
625 + v[1] * DCP_W[t0][1]
626 + v[2] * DCP_W[t0][2]
627 + v[3] * DCP_W[t0][3] ) / 128;
629 if ((t0 & 0x7) == 7) {
630 INT32 d = pred - v[2]; /* D */
631 if (d < -128 || d > 128) {
633 } else if (d = pred - v[0], d < -128 || d > 128) { /* L */
635 } else if (d = pred - v[1], d < -128 || d > 128) { /* DL */
646 block[idx * 64] = dc;
653 block += t->Index->BC[i] * 64;
654 mode += t->Index->BC[i];
660 static void Reconstruct_IntraBlock(
671 if (block[0] == NOT_CODED) {
673 Block_CopyPlane8x8(p, x, y, r);
678 Dequantize_DoDequantize(
685 IDCT_8x8(coeff, coeff);
687 Block_CopyIntra8x8(p, x, y, coeff);
692 static void Reconstruct_InterBlock(
703 if (block[0] == NOT_CODED) {
705 Block_CopyPlane8x8(p, x, y, r);
710 Dequantize_DoDequantize(
717 IDCT_8x8(coeff, coeff);
719 Block_ReviseInter8x8(p, x, y, coeff);
724 static void Reconstruct(
729 INT32 mx = t->Index->MX;
730 INT32 my = t->Index->MY;
732 Plane_t* g = t->Frame[0];
733 Plane_t* p = t->Frame[1];
734 Plane_t* r = t->Frame[2];
736 const UINT8* b0 = t->BMode;
738 const INT16* y0 = t->Block;
739 const INT16* y1 = t->Block + mx * 2 * 64;
740 const INT16* c0 = y0 + t->Index->BC[0] * 64;
741 const INT16* c1 = c0 + t->Index->BC[1] * 64;
743 const MotionVector_t* mv0 = t->MV;
745 for (y = 0; y < my; y++, b0 += mx * 4, mv0 += mx * 4, y0 += mx * 2 * 64, y1 += mx * 2 * 64) {
746 for (x = 0; x < mx; x++, y0 += 2 * 64, y1 += 2 * 64, c0 += 64, c1 += 64) {
748 case 0: /* INTER_NOMV */
749 Block_CopyPlane16x16(p + 0, x * 16, y * 16, r + 0);
750 Block_CopyPlane8x8 (p + 1, x * 8, y * 8, r + 1);
751 Block_CopyPlane8x8 (p + 2, x * 8, y * 8, r + 2);
753 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 0, y0 + 0, 0, NULL);
754 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 0, y0 + 64, 0, NULL);
755 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 8, y1 + 0, 0, NULL);
756 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 8, y1 + 64, 0, NULL);
757 Reconstruct_InterBlock(t, p + 1, x * 8 + 0, y * 8 + 0, c0 + 0, 1, NULL);
758 Reconstruct_InterBlock(t, p + 2, x * 8 + 0, y * 8 + 0, c1 + 0, 2, NULL);
762 Reconstruct_IntraBlock(t, p + 0, x * 16 + 0, y * 16 + 0, y0 + 0, 0, r + 0);
763 Reconstruct_IntraBlock(t, p + 0, x * 16 + 8, y * 16 + 0, y0 + 64, 0, r + 0);
764 Reconstruct_IntraBlock(t, p + 0, x * 16 + 0, y * 16 + 8, y1 + 0, 0, r + 0);
765 Reconstruct_IntraBlock(t, p + 0, x * 16 + 8, y * 16 + 8, y1 + 64, 0, r + 0);
766 Reconstruct_IntraBlock(t, p + 1, x * 8 + 0, y * 8 + 0, c0 + 0, 1, r + 1);
767 Reconstruct_IntraBlock(t, p + 2, x * 8 + 0, y * 8 + 0, c1 + 0, 2, r + 2);
770 case 2: /* INTER_MV */
771 case 3: /* INTER_MV_LAST */
772 case 4: /* INTER_MV_LAST2 */
774 const MotionVector_t* mv = mv0 + x * 2;
776 MotionComp_Block16x16(p + 0, x * 16, y * 16, r + 0, mv);
777 MotionComp_Block8x8C (p + 1, x * 8, y * 8, r + 1, mv);
778 MotionComp_Block8x8C (p + 2, x * 8, y * 8, r + 2, mv);
780 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 0, y0 + 0, 0, r + 0);
781 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 0, y0 + 64, 0, r + 0);
782 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 8, y1 + 0, 0, r + 0);
783 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 8, y1 + 64, 0, r + 0);
784 Reconstruct_InterBlock(t, p + 1, x * 8 + 0, y * 8 + 0, c0 + 0, 1, r + 1);
785 Reconstruct_InterBlock(t, p + 2, x * 8 + 0, y * 8 + 0, c1 + 0, 2, r + 2);
789 case 5: /* INTER_GOLDEN_NOMV */
790 Block_CopyPlane16x16(p + 0, x * 16, y * 16, g + 0);
791 Block_CopyPlane8x8 (p + 1, x * 8, y * 8, g + 1);
792 Block_CopyPlane8x8 (p + 2, x * 8, y * 8, g + 2);
794 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 0, y0 + 0, 0, r + 0);
795 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 0, y0 + 64, 0, r + 0);
796 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 8, y1 + 0, 0, r + 0);
797 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 8, y1 + 64, 0, r + 0);
798 Reconstruct_InterBlock(t, p + 1, x * 8 + 0, y * 8 + 0, c0 + 0, 1, r + 1);
799 Reconstruct_InterBlock(t, p + 2, x * 8 + 0, y * 8 + 0, c1 + 0, 2, r + 2);
802 case 6: /* INTER_GOLDEN_MV */
804 const MotionVector_t* mv = mv0 + x * 2;
806 MotionComp_Block16x16(p + 0, x * 16, y * 16, g + 0, mv);
807 MotionComp_Block8x8C (p + 1, x * 8, y * 8, g + 1, mv);
808 MotionComp_Block8x8C (p + 2, x * 8, y * 8, g + 2, mv);
810 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 0, y0 + 0, 0, r + 0);
811 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 0, y0 + 64, 0, r + 0);
812 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 8, y1 + 0, 0, r + 0);
813 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 8, y1 + 64, 0, r + 0);
814 Reconstruct_InterBlock(t, p + 1, x * 8 + 0, y * 8 + 0, c0 + 0, 1, r + 1);
815 Reconstruct_InterBlock(t, p + 2, x * 8 + 0, y * 8 + 0, c1 + 0, 2, r + 2);
819 case 7: /* INTER_MV_FOUR */
821 const MotionVector_t* v0 = mv0 + x * 2 + 0;
822 const MotionVector_t* v1 = mv0 + x * 2 + 1;
823 const MotionVector_t* v2 = mv0 + x * 2 + 0 + mx * 2;
824 const MotionVector_t* v3 = mv0 + x * 2 + 1 + mx * 2;
826 INT32 cmx = v0->X + v1->X + v2->X + v3->X;
827 INT32 cmy = v0->Y + v1->Y + v2->Y + v3->Y;
829 MotionVector_t mv = {
830 (cmx + CMV[cmx < 0]) >> 2,
831 (cmy + CMV[cmy < 0]) >> 2
834 MotionComp_Block8x8Y(p + 0, x * 16 + 0, y * 16 + 0, r + 0, v0 );
835 MotionComp_Block8x8Y(p + 0, x * 16 + 8, y * 16 + 0, r + 0, v1 );
836 MotionComp_Block8x8Y(p + 0, x * 16 + 0, y * 16 + 8, r + 0, v2 );
837 MotionComp_Block8x8Y(p + 0, x * 16 + 8, y * 16 + 8, r + 0, v3 );
838 MotionComp_Block8x8C(p + 1, x * 8, y * 8, r + 1, &mv);
839 MotionComp_Block8x8C(p + 2, x * 8, y * 8, r + 2, &mv);
841 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 0, y0 + 0, 0, r + 0);
842 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 0, y0 + 64, 0, r + 0);
843 Reconstruct_InterBlock(t, p + 0, x * 16 + 0, y * 16 + 8, y1 + 0, 0, r + 0);
844 Reconstruct_InterBlock(t, p + 0, x * 16 + 8, y * 16 + 8, y1 + 64, 0, r + 0);
845 Reconstruct_InterBlock(t, p + 1, x * 8 + 0, y * 8 + 0, c0 + 0, 1, r + 1);
846 Reconstruct_InterBlock(t, p + 2, x * 8 + 0, y * 8 + 0, c1 + 0, 2, r + 2);
857 static void LoopFilter(
863 const INT16* b = t->Block;
865 Plane_t* plane = t->Frame[1];
867 for (i = 0; i < 3; i++, plane++) {
868 INT32 bx = t->Index->BX[i];
869 INT32 by = t->Index->BY[i];
871 UINT8* r0 = plane->Plane;
873 for (y = 0; y < by; y++, r0 += plane->Pitch * 8) {
876 for (x = 0; x < bx; x++, r += 8, b += 64) {
877 if (*b != NOT_CODED) {
879 Filter_LoopFilterH(&(t->Filter), r, plane->Pitch);
883 Filter_LoopFilterV(&(t->Filter), r, plane->Pitch);
886 if (x < bx - 1 && b[64 * 1] == NOT_CODED) {
887 Filter_LoopFilterH(&(t->Filter), r + 8, plane->Pitch);
890 if (y < by - 1 && b[64 * bx] == NOT_CODED) {
891 Filter_LoopFilterV(&(t->Filter), r + 8 * plane->Pitch, plane->Pitch);
901 BOOL QT_FrameDecoder_DecodeFrame(
905 if (!FrameHeader_Decode(
911 if (t->QIndex != t->Header.QIS[0]) {
912 t->QIndex = t->Header.QIS[0];
913 Dequantize_MakeMatrix(
915 &(t->Setup->Dequantize),
924 if (t->Header.Type == 0) { /* Intra */
926 INT16* e = p + 64 * t->Index->Blocks;
927 for (; p < e; p += 64) {
931 memset(t->BMode, 1, t->Index->Blocks);
933 t->Frame[0] = t->Plane + 0; /* G */
934 t->Frame[1] = t->Plane + 0; /* C */
935 t->Frame[2] = t->Plane + 3; /* R */
938 if (t->Frame[1] == t->Plane + 0) {
939 t->Frame[1] = t->Plane + 3;
940 t->Frame[2] = t->Plane + 0;
941 } else if (t->Frame[1] == t->Plane + 3) {
942 t->Frame[1] = t->Plane + 6;
943 t->Frame[2] = t->Plane + 3;
944 } else if (t->Frame[1] == t->Plane + 6) {
945 t->Frame[1] = t->Plane + 3;
946 t->Frame[2] = t->Plane + 6;
949 if (!DecodeCodedBlockFlag(
955 if (!DecodeMacroBlockCodingModes(
961 if (!DecodeMotionVectors(
968 if (!DecodeDCTCoefficients(
978 if (t->Filter.Limit > 0) {