1 /* FrameDecoder_Impl.h */
6 static BOOL FrameHeader_Decode(
20 FETCH_BITS_I(t->Type, 1)
22 FETCH_BITS_I(t->QIS[n], 6) n++;
26 FETCH_BITS_I(t->QIS[n], 6) n++;
30 FETCH_BITS_I(t->QIS[n], 6) n++;
36 if (t->Type == 0) { /* INTRA */
50 static BOOL FrameDecoder_DecodeCodedBlockFlag(
64 for (i = 0; i < 3; i++) {
65 t->BlocksCoded[i] = 0;
68 memset(t->Count, 0, sizeof(UINT8) * t->Index->Blocks);
73 RunLength_Start(&r, x);
76 for (i = 0; i < 3; i++) {
77 INT8* e = p + t->Index->SC[i];
97 UINT8* count = t->Count;
100 RunLength_Start(&r, x);
103 for (i = 0; i < 3; i++) {
104 const UINT8* c = t->Index->BCount[i];
106 INT8* e = p + t->Index->SC[i];
107 for (; p < e; p++, c++) {
114 t->BlocksCoded[i] += *c;
117 for (j = 0; j < *c; j++) {
135 UINT8* count = t->Count;
138 RunLength_Start(&r, x);
141 for (i = 0; i < 3; i++) {
142 const UINT8* c = t->Index->BCount[i];
144 INT8* e = p + t->Index->SC[i];
145 for (; p < e; p++, c++) {
147 for (j = 0; j < *c; j++) {
151 t->BlocksCoded[i] += 1;
175 static BOOL FrameDecoder_DecodeMacroBlockCodingModes(
184 UINT8* m = t->MBMode;
185 UINT8* end = m + t->Index->MC;
187 const UINT16* mbi = t->Index->MBIndex;
188 const UINT16* cbi = t->Index->CBIndex;
190 UINT8* m0 = t->BMode;
191 UINT8* m1 = m0 + t->Index->BC[0];
192 UINT8* m2 = m1 + t->Index->BC[1];
194 const UINT8* c = t->Count;
196 FETCH_BITS_I(scheme, 3)
197 memcpy(mode, M_MODE[scheme], 8);
201 for (i = 0; i < 8; i++) {
209 for (; m < end; m++, c += 4, mbi += 4, cbi++) {
212 if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff || c[3] != 0xff) {
215 code = H_MODE[GET_BITS_I(7)];
216 RETIRE_BITS(code & 0xf);
217 mm = mode[code >> 4];
231 for (; m < end; m++, c += 4, mbi += 4, cbi++) {
234 if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff || c[3] != 0xff) {
258 static BOOL FrameDecoder_DecodeMotionVectors(
264 const UINT8* m = t->MBMode;
265 const UINT8* end = m + t->Index->MC;
267 const UINT16* cbi = t->Index->CBIndex;
269 const UINT8* c = t->Count;
271 MotionVector_t* mv = t->MV;
273 MotionVector_t last[2] = { { 0, 0 }, { 0, 0 } };
275 INT32 (*Decode)(MotionVector_t*, INT32);
280 Decode = (x == 0) ? DecodeMV0 : DecodeMV1;
282 for (; m < end; m++, c += 4, mv += 4, cbi++) {
284 case 2: /* INTER_MV */
294 case 3: /* INTER_MV_LAST */
298 case 4: /* INTER_MV_LAST2 */
305 case 6: /* INTER_GOLDEN_MV */
312 case 7: /* INTER_MV_FOUR */
314 MotionVector_t* mv0 = mv;
316 MotionVector_t* v = mv;
322 for (i = 0; i < 4; i++) {
336 for (; v < mv + 4; v++) {
341 t->MVC[*cbi].X = (cmx + CMV[cmx < 0]) >> 2;
342 t->MVC[*cbi].Y = (cmy + CMV[cmy < 0]) >> 2;
356 if (*m != 7) { /* INTER_MV_FOUR */
368 static BOOL FrameDecoder_DecodeBlocks(
371 const HuffmanDecoder_t* h,
372 DCTCoefficientsContext_t* ctx,
377 INT32 count = ctx->BlocksCoded[index];
380 INT32 i, run = 0, coeff = 0;
382 if (ctx->EOB_Run == 0) {
391 if (h->LookupBits > 0) {
394 code = GET_BITS(h->LookupBits);
395 len = h->LookupLength[code];
399 token = h->LookupToken[code];
402 RETIRE_BITS(h->LookupBits)
403 rbits = bits = h->LookupBits;
407 for (; token < 0 && bits < BITS_COUNT; bits++) {
408 INT32 es = h->Count[bits], idx;
413 idx = h->Index[bits];
415 INT32 len = bits + 1 - rbits;
417 const UINT32* s = h->Code + idx;
418 const UINT32* e = s + es;
421 code = (code << len) | GET_BITS(len);
427 const UINT32* x = s + (e - s) / 2;
429 token = h->Token[x - h->Code];
442 if (token >= 0 && token < 7) {
443 INT32 blen = EOB_BITS_LEN[token];
444 ctx->EOB_Run = EOB_RUN_BASE[token];
454 } else if (token >= 7 && token < 32) {
455 INT32 clen = COEFF_BITS_LEN[token - 7];
456 INT32 rlen = RUN_BITS_LEN [token - 7];
458 coeff = COEFF_BASE[token - 7];
461 FETCH_BITS_I(sign, 1)
462 sign = COEFF_SIGN[sign];
467 coeff = sign * (coeff + x);
473 run = RUN_BASE[token - 7];
485 if (ctx->EOB_Run == 0) {
486 *((ctx->Run )++) = run;
487 *((ctx->Coeff)++) = coeff;
489 if (index + run + 1 > 64) {
493 for (i = index + 1; i < index + run + 1; i++) {
494 ctx->BlocksCoded[i] -= 1;
500 INT32 eob = ctx->EOB_Run;
506 *((ctx->Run )++) = -1;
507 *((ctx->Coeff)++) = eob;
509 for (i = index + 1; i < 64; i++) {
510 ctx->BlocksCoded[i] -= eob;
528 static BOOL FrameDecoder_DecodeDCTCoefficients(
534 const HuffmanDecoder_t* huff[3];
540 DCTCoefficientsContext_t ctx = { 0 };
545 ctx.Coeff = t->DCTCoeff;
547 for (i = 0; i < 64; i++) {
548 Coded[0][i] = t->BlocksCoded[0];
549 Coded[1][i] = t->BlocksCoded[1];
550 Coded[2][i] = t->BlocksCoded[2];
553 for (i = 0; i < 5; i++) {
561 huff[0] = t->Setup->Huffman + i * 0x10 + hy;
562 huff[1] = t->Setup->Huffman + i * 0x10 + hc;
565 for (j = 0; j < COEFFS[i]; j++, index++) {
566 for (k = 0; k < 3; k++) {
567 t->BRun [k][index] = ctx.Run;
568 t->BCoeff[k][index] = ctx.Coeff;
570 ctx.BlocksCoded = Coded[k];
572 if (!FrameDecoder_DecodeBlocks(
589 static BOOL FrameDecoder_Decode(
596 QT_BitReader_Initialize(&d, p, size);
598 if (!FrameHeader_Decode(&(t->Header), &d)) {
602 if (t->QIndex != t->Header.QIS[0]) {
603 t->QIndex = t->Header.QIS[0];
604 Dequantize_MakeMatrix(
606 &(t->Setup->Dequantize),
617 if (t->Header.Type == 0) { /* Intra */
618 t->Frame[0] = t->Plane + 0; /* G */
619 t->Frame[1] = t->Plane + 0; /* C */
620 t->Frame[2] = t->Plane + 3; /* R */
622 memset(t->Count, 0, sizeof(UINT8) * t->Index->Blocks);
624 memset(t->MBMode, 1, sizeof(UINT8) * t->Index->MC);
625 memset(t->BMode, 1, sizeof(UINT8) * t->Index->Blocks);
627 t->BlocksCoded[0] = t->Index->BC[0];
628 t->BlocksCoded[1] = t->Index->BC[1];
629 t->BlocksCoded[2] = t->Index->BC[2];
632 if (t->Frame[1] == t->Plane + 0) {
633 t->Frame[1] = t->Plane + 3;
634 t->Frame[2] = t->Plane + 0;
635 } else if (t->Frame[1] == t->Plane + 3) {
636 t->Frame[1] = t->Plane + 6;
637 t->Frame[2] = t->Plane + 3;
638 } else if (t->Frame[1] == t->Plane + 6) {
639 t->Frame[1] = t->Plane + 3;
640 t->Frame[2] = t->Plane + 6;
643 if (!FrameDecoder_DecodeCodedBlockFlag(t, &d)) {
647 if (!FrameDecoder_DecodeMacroBlockCodingModes(t, &d)) {
651 if (!FrameDecoder_DecodeMotionVectors(t, &d)) {
656 if (!FrameDecoder_DecodeDCTCoefficients(t, &d)) {
662 DecodeDCCoefficients(t);
669 const INT16* dc = t->DC;
671 for (i = 0; i < 3; i++) {
672 INT32 bx = t->Index->BX[i];
673 INT32 by = t->Index->BY[i];
675 printf("DC: %d\n", i);
676 for (y = 0; y < by; y++) {
677 for (x = 0; x < bx; x++, dc++) {
698 const INT16* dc = t->DC;
700 for (i = 0; i < 3; i++) {
701 INT32 bx = t->Index->BX[i];
702 INT32 by = t->Index->BY[i];
704 printf("DC: %d\n", i);
705 for (y = 0; y < by; y++) {
706 for (x = 0; x < bx; x++, dc++) {
720 QT_ReconstructFrame(t);
729 #undef FrameHeader_Decode
731 #undef FrameDecoder_DecodeCodedBlockFlag
732 #undef FrameDecoder_DecodeMacroBlockCodingModes
733 #undef FrameDecoder_DecodeMotionVectors
735 #undef FrameDecoder_DecodeBlocks
736 #undef FrameDecoder_DecodeDCTCoefficients
738 #undef FrameDecoder_Decode