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* mbi = t->Index->MBIndex;
269 const UINT8* c = t->Count;
271 MotionVector_t last[2] = { { 0, 0 }, { 0, 0 } };
273 INT32 (*Decode)(MotionVector_t*, INT32);
278 Decode = (x == 0) ? DecodeMV0 : DecodeMV1;
280 for (; m < end; m++, c += 4, mbi += 4) {
281 MotionVector_t* mv = t->MV + mbi[0];
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 */
315 for (i = 0; i < 4; i++) {
316 MotionVector_t* v = t->MV + mbi[i];
352 static BOOL FrameDecoder_DecodeBlocks(
355 const HuffmanDecoder_t* h,
356 DCTCoefficientsContext_t* ctx,
361 INT32 count = ctx->BlocksCoded[index];
364 INT32 i, run = 0, coeff = 0;
366 if (ctx->EOB_Run == 0) {
375 if (h->LookupBits > 0) {
378 code = GET_BITS(h->LookupBits);
379 len = h->LookupLength[code];
383 token = h->LookupToken[code];
386 RETIRE_BITS(h->LookupBits)
387 rbits = bits = h->LookupBits;
391 for (; token < 0 && bits < BITS_COUNT; bits++) {
392 INT32 es = h->Count[bits], idx;
397 idx = h->Index[bits];
399 INT32 len = bits + 1 - rbits;
401 const UINT32* s = h->Code + idx;
402 const UINT32* e = s + es;
405 code = (code << len) | GET_BITS(len);
411 const UINT32* x = s + (e - s) / 2;
413 token = h->Token[x - h->Code];
426 if (token >= 0 && token < 7) {
427 INT32 blen = EOB_BITS_LEN[token];
428 ctx->EOB_Run = EOB_RUN_BASE[token];
438 } else if (token >= 7 && token < 32) {
439 INT32 clen = COEFF_BITS_LEN[token - 7];
440 INT32 rlen = RUN_BITS_LEN [token - 7];
442 coeff = COEFF_BASE[token - 7];
445 FETCH_BITS_I(sign, 1)
446 sign = COEFF_SIGN[sign];
451 coeff = sign * (coeff + x);
457 run = RUN_BASE[token - 7];
469 if (ctx->EOB_Run == 0) {
470 *((ctx->Run )++) = run;
471 *((ctx->Coeff)++) = coeff;
473 if (index + run + 1 > 64) {
477 for (i = index + 1; i < index + run + 1; i++) {
478 ctx->BlocksCoded[i] -= 1;
484 INT32 eob = ctx->EOB_Run;
490 *((ctx->Run )++) = -1;
491 *((ctx->Coeff)++) = eob;
493 for (i = index + 1; i < 64; i++) {
494 ctx->BlocksCoded[i] -= eob;
512 static BOOL FrameDecoder_DecodeDCTCoefficients(
518 const HuffmanDecoder_t* huff[3];
524 DCTCoefficientsContext_t ctx = { 0 };
529 ctx.Coeff = t->DCTCoeff;
531 for (i = 0; i < 64; i++) {
532 Coded[0][i] = t->BlocksCoded[0];
533 Coded[1][i] = t->BlocksCoded[1];
534 Coded[2][i] = t->BlocksCoded[2];
537 for (i = 0; i < 5; i++) {
545 huff[0] = t->Setup->Huffman + i * 0x10 + hy;
546 huff[1] = t->Setup->Huffman + i * 0x10 + hc;
549 for (j = 0; j < COEFFS[i]; j++, index++) {
550 for (k = 0; k < 3; k++) {
551 t->BRun [k][index] = ctx.Run;
552 t->BCoeff[k][index] = ctx.Coeff;
554 ctx.BlocksCoded = Coded[k];
556 if (!FrameDecoder_DecodeBlocks(
573 static BOOL FrameDecoder_Decode(
580 QT_BitReader_Initialize(&d, p, size);
582 if (!FrameHeader_Decode(&(t->Header), &d)) {
588 if (t->Header.Type == 0) { /* Intra */
589 memset(t->Count, 0, sizeof(UINT8) * t->Index->Blocks);
591 memset(t->BMode, 1, t->Index->Blocks);
593 t->BlocksCoded[0] = t->Index->BC[0];
594 t->BlocksCoded[1] = t->Index->BC[1];
595 t->BlocksCoded[2] = t->Index->BC[2];
598 if (!FrameDecoder_DecodeCodedBlockFlag(t, &d)) {
602 if (!FrameDecoder_DecodeMacroBlockCodingModes(t, &d)) {
606 if (!FrameDecoder_DecodeMotionVectors(t, &d)) {
611 if (!FrameDecoder_DecodeDCTCoefficients(t, &d)) {
617 DecodeDCCoefficients(t);
624 const INT16* dc = t->DC;
626 for (i = 0; i < 3; i++) {
627 INT32 bx = t->Index->BX[i];
628 INT32 by = t->Index->BY[i];
630 printf("DC: %d\n", i);
631 for (y = 0; y < by; y++) {
632 for (x = 0; x < bx; x++, dc++) {
652 const INT16* dc = t->DC;
654 for (i = 0; i < 3; i++) {
655 INT32 bx = t->Index->BX[i];
656 INT32 by = t->Index->BY[i];
658 printf("DC: %d\n", i);
659 for (y = 0; y < by; y++) {
660 for (x = 0; x < bx; x++, dc++) {
678 #undef FrameHeader_Decode
680 #undef FrameDecoder_DecodeCodedBlockFlag
681 #undef FrameDecoder_DecodeMacroBlockCodingModes
682 #undef FrameDecoder_DecodeMotionVectors
684 #undef FrameDecoder_DecodeBlocks
685 #undef FrameDecoder_DecodeDCTCoefficients
687 #undef FrameDecoder_Decode