OSDN Git Service

fix loop filter processing.
[qtheora/main.git] / Lib / QTheora / FrameDecoder.c
1 /* FrameDecoder.c */
2 /* 2009/05/15     */
3
4 #include "StdAfx.h"
5
6 #include "FrameDecoder.h"
7
8 #include "RunLength.h"
9
10 #include "IDCT.h"
11
12 /* */
13
14 #include "FrameDecoderTable.h"
15
16 /* */
17
18 BOOL QT_FrameDecoder_Setup(
19         FrameDecoder_t*      t,
20         const BlockIndex_t*  index,
21         const SetupHeader_t* setup,
22         MemoryPool_t*        pool)
23 {
24         INT32 i, j;
25
26         t->Index = index;
27         t->Setup = setup;
28
29         for (i = 0; i < 3; i++) {
30                 Plane_t* r = t->Plane + i * 3;
31
32                 INT32 cb = index->MX * 16 * index->MY * 16;
33
34                 UINT8* p = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * cb);
35                 if (p == NULL) {
36                         return FALSE;
37                 }
38
39                 r->Plane = p;
40                 r->Pitch = index->MX * 16;
41                 r->CX    = index->MX * 16;
42                 r->CY    = index->MY * 16;
43
44                 for (j = 1; j < 3; j++) {
45                         p = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * cb / 4);
46                         if (p == NULL) {
47                                 return FALSE;
48                         }
49
50                         r[j].Plane = p;
51                         r[j].Pitch = index->MX * 8;
52                         r[j].CX    = index->MX * 8;
53                         r[j].CY    = index->MY * 8;
54                 }
55         }
56
57         for (i = 0; i < 3; i++) {
58                 t->Frame[i] = t->Plane + i * 3;
59         }
60
61         t->QIndex = -1;
62
63         t->SBCoded = (INT8*)QT_MemoryPool_Allocate(pool, sizeof(INT8) * index->SBlocks);
64         if (t->SBCoded == NULL) {
65                 return FALSE;
66         }
67
68         t->BMode = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->Blocks);
69         if (t->BMode == NULL) {
70                 return FALSE;
71         }
72
73         t->MV = (MotionVector_t*)QT_MemoryPool_Allocate(pool, sizeof(MotionVector_t) * index->BC[0]);
74         if (t->MV == NULL) {
75                 return FALSE;
76         }
77
78         t->Block = (INT16*)QT_MemoryPool_Allocate(pool, sizeof(INT16) * 64 * index->Blocks);
79         if (t->Block == NULL) {
80                 return FALSE;
81         }
82
83         t->Count = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->Blocks);
84         if (t->Count == NULL) {
85                 return FALSE;
86         }
87
88         return TRUE;
89 }
90
91 /* */
92
93
94 /* */
95
96 static BOOL DecodeCodedBlockFlag(
97         FrameDecoder_t* t,
98         BitDecoder_t*   d)
99 {
100         INT8* p;
101         INT32 i, j;
102
103         RunLength_t bs;
104
105         INT32 full = 0;
106         INT32 part = 0;
107
108         /* PARTIAL or FULL */
109
110         RunLength_Start(&bs, d);
111
112         p = t->SBCoded;
113         for (i = 0; i < 3; i++) {
114                 INT8* e = p + t->Index->SC[i];
115                 for (; p < e; p++) {
116                         if ((*p = RunLength_DecodeLong(&bs, d)) == 0) {
117                                 full++;
118                         } else {
119                                 part++;
120                         }
121                 }
122         }
123
124         if (bs.Run > 0) {
125                 return FALSE;
126         }
127
128         /* FULL */
129
130         if (full > 0) {
131                 INT16* block = t->Block;
132
133                 RunLength_Start(&bs, d);
134
135                 p = t->SBCoded;
136                 for (i = 0; i < 3; i++) {
137                         const UINT16* b = t->Index->BIndex[i];
138                         const UINT8*  c = t->Index->BCount[i];
139
140                         INT8* e = p + t->Index->SC[i];
141                         for (; p < e; p++, c++) {
142                                 if (*p == 0) {
143                                         INT16 flag = NOT_CODED;
144                                         if (RunLength_DecodeLong(&bs, d) != 0) {
145                                                 flag = 0;
146                                                 *p   = -1;
147                                         }
148
149                                         for (j = 0; j < *c; j++) {
150                                                 block[b[j] * 64] = flag;
151                                         }
152                                 }
153
154                                 b += *c;
155                         }
156
157                         block += t->Index->BC[i] * 64;
158                 }
159
160                 if (bs.Run > 0) {
161                         return FALSE;
162                 }
163         }
164
165         /* PARTIAL */
166
167         if (part > 0) {
168                 INT16* block = t->Block;
169
170                 RunLength_Start(&bs, d);
171
172                 p = t->SBCoded;
173                 for (i = 0; i < 3; i++) {
174                         const UINT16* b = t->Index->BIndex[i];
175                         const UINT8*  c = t->Index->BCount[i];
176
177                         INT8* e = p + t->Index->SC[i];
178                         for (; p < e; p++, c++) {
179                                 if (*p == 1) {
180                                         for (j = 0; j < *c; j++) {
181                                                 INT16 flag = NOT_CODED;
182                                                 if (RunLength_DecodeShort(&bs, d) != 0) {
183                                                         flag = 0;
184                                                 }
185
186                                                 block[b[j] * 64] = flag;
187                                         }
188                                 }
189
190                                 b += *c;
191                         }
192
193                         block += t->Index->BC[i] * 64;
194                 }
195
196                 if (bs.Run > 0) {
197                         return FALSE;
198                 }
199         }
200
201         return TRUE;
202 }
203
204 /* */
205
206 static INT32 DecodeModeX(BitDecoder_t* d)
207 {
208         INT32 code = H_MODE[QT_BitDecoder_PeekBits(d, 7)];
209         QT_BitDecoder_SkipBits(d, code & 0x0f);
210         return code >> 4;
211 }
212
213 static INT32 DecodeMode7(BitDecoder_t* d)
214 {
215         INT32 m = QT_BitDecoder_GetBits(d, 3);
216         return m;
217 }
218
219 /* */
220
221 static BOOL DecodeMacroBlockCodingModes(
222         FrameDecoder_t* t,
223         BitDecoder_t*   d)
224 {
225         INT32 scheme;
226         UINT8 mode[8];
227
228         const UINT16* mbi = t->Index->MBIndex;
229         const UINT16* cbi = t->Index->CBIndex;
230         const UINT16* end = mbi + t->Index->BC[0];
231
232         const INT16* block = t->Block;
233
234         UINT8* m0 = t->BMode;
235         UINT8* m1 = m0 + t->Index->BC[0];
236         UINT8* m2 = m1 + t->Index->BC[1];
237
238         INT32 (*DecodeMode)(BitDecoder_t*);
239
240         scheme = QT_BitDecoder_GetBits(d, 3);
241         memcpy(mode, M_MODE[scheme], 8);
242
243         if (scheme == 0) {
244                 INT32 i;
245                 for (i = 0; i < 8; i++) {
246                         mode[QT_BitDecoder_GetBits(d, 3)] = i;
247                 }
248         }
249
250         DecodeMode = (scheme != 7) ? DecodeModeX : DecodeMode7;
251
252         for (; mbi < end; mbi += 4, cbi++) {
253                 UINT8 mm = 0;
254
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)];
260                 }
261
262                 m0[mbi[0]] = mm;
263                 m0[mbi[1]] = mm;
264                 m0[mbi[2]] = mm;
265                 m0[mbi[3]] = mm;
266                 m1[cbi[0]] = mm;
267                 m2[cbi[0]] = mm;
268         }
269
270         return TRUE;
271 }
272
273 /* */
274
275 static void DecodeMV0(MotionVector_t* mv, BitDecoder_t* d)
276 {
277         INT32 b;
278
279         b = QT_BitDecoder_PeekBits(d, 8);
280         mv->X = MV_VAL[b];
281         QT_BitDecoder_SkipBits(d, MV_LEN[b]);
282
283         b = QT_BitDecoder_PeekBits(d, 8);
284         mv->Y = MV_VAL[b];
285         QT_BitDecoder_SkipBits(d, MV_LEN[b]);
286 }
287
288 static void DecodeMV1(MotionVector_t* mv, BitDecoder_t* d)
289 {
290         INT32 b;
291
292         INT8 v[2];
293
294         b = QT_BitDecoder_GetBits(d, 6);
295         v[0] = b >> 1;
296         v[1] = -v[0];
297         mv->X = v[b & 1];
298
299         b = QT_BitDecoder_GetBits(d, 6);
300         v[0] = b >> 1;
301         v[1] = -v[0];
302         mv->Y = v[b & 1];
303 }
304
305 /* */
306
307 static BOOL DecodeMotionVectors(
308         FrameDecoder_t* t,
309         BitDecoder_t*   d)
310 {
311         const UINT16* mbi = t->Index->MBIndex;
312         const UINT16* end = mbi + t->Index->BC[0];
313
314         const UINT8* m = t->BMode;
315
316         const INT16* block = t->Block;
317
318         void (*Decode)(MotionVector_t*, BitDecoder_t*) = (QT_BitDecoder_GetBits(d, 1) == 0) ? DecodeMV0 : DecodeMV1;
319
320         MotionVector_t last[2] = { { 0, 0 }, { 0, 0 } };
321
322         for (; mbi < end; mbi += 4) {
323                 MotionVector_t* mv = t->MV + mbi[0];
324
325                 switch (m[mbi[0]]) {
326                 case 2: /* INTER_MV */
327                         Decode(mv, d);
328
329                         last[1] = last[0];
330                         last[0] = *mv;
331                         break;
332
333                 case 3: /* INTER_MV_LAST */
334                         *mv = last[0];
335                         break;
336
337                 case 4: /* INTER_MV_LAST2 */
338                         *mv = last[1];
339
340                         last[1] = last[0];
341                         last[0] = *mv;
342                         break;
343
344                 case 6: /* INTER_GOLDEN_MV */
345                         Decode(mv, d);
346                         break;
347
348                 case 7: /* INTER_MV_FOUR */
349                 {
350                         INT32 i;
351                         for (i = 0; i < 4; i++) {
352                                 MotionVector_t* v = t->MV + mbi[i];
353
354                                 if (block[mbi[i] * 64] != NOT_CODED) {
355                                         Decode(v, d);
356                                         mv = v;
357                                 } else {
358                                         v->X = 0;
359                                         v->Y = 0;
360                                 }
361                         }
362
363                         last[1] = last[0];
364                         last[0] = *mv;
365                         break;
366                 }
367
368                 } /* switch */
369         }
370
371         return TRUE;
372 }
373
374 /* */
375
376 static BOOL DecodeBlock(
377         BitDecoder_t*           d,
378         const HuffmanDecoder_t* h,
379         INT32                   index,
380         INT16*                  block,
381         UINT8*                  count,
382         const UINT16*           bindex,
383         INT32                   blocks,
384         INT32*                  eob_run)
385 {
386         INT32 eob = *eob_run;
387
388         INT32 coeff = 0;
389         INT32 run   = 0;
390
391         const UINT16* bi  = bindex;
392         const UINT16* end = bi + blocks;
393
394         for (; bi < end; bi++) {
395                 INT16* b = block + *bi * 64;
396                 UINT8* c = count + *bi;
397
398                 if (*b == NOT_CODED || *c > index) {
399                         continue;
400                 }
401
402                 if (eob == 0) {
403                         INT32 token = QT_Huffman_LookupToken(h, d);
404
405                         switch (token) {
406                         case 0: case 1: case 2:
407                                 eob   = token + 1;
408                                 coeff = 0;
409                                 run   = 0;
410                                 break;
411
412                         case 3: case 4:
413                         case 5: case 6:
414                                 eob   = EOB_RUN[token - 3] + QT_BitDecoder_GetBits(d, EOB_BITS[token - 3]);
415                                 coeff = 0;
416                                 run   = 0;
417                                 break;
418
419                         case 7: case 8:
420                                 coeff = 0;
421                                 run   = QT_BitDecoder_GetBits(d, ZR_BITS_7[token - 7]);
422                                 break;
423
424                         case  9: case 10:
425                         case 11: case 12:
426                                 coeff = COEFF_BASE_9[token - 9];
427                                 run   = 0;
428                                 break;
429
430                         case 13: case 14:
431                         case 15: case 16:
432                                 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)] * (token - 10);
433                                 run   = 0;
434                                 break;
435
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]);
440                                 run    = 0;
441                                 break;
442
443                         case 23: case 24: case 25:
444                         case 26: case 27:
445                                 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)];
446                                 run   = token - 22;
447                                 break;
448
449                         case 28: case 29:
450                                 coeff = COEFF_SIGN[QT_BitDecoder_GetBits(d, 1)];
451                                 run   = ZR_BASE_28[token - 28] + QT_BitDecoder_GetBits(d, token - 26);
452                                 break;
453
454                         case 30:
455                                 coeff = COEFF_30_31[QT_BitDecoder_GetBits(d, 2)];
456                                 run   = 1;
457                                 break;
458
459                         case 31:
460                                 coeff = COEFF_30_31[QT_BitDecoder_GetBits(d, 2)];
461                                 run   = 2 + QT_BitDecoder_GetBits(d, 1);
462                                 break;
463
464                         default:
465                                 return FALSE;
466                         }
467                 }
468
469                 if (eob == 0) {
470                         INT16* p = b + *c;
471                         INT16* e = p + run;
472
473                         if (p >= b + 64) {
474                                 return FALSE;
475                         }
476
477                         while (p < e) {
478                                 *(p++) = 0;
479                         }
480
481                         *p  = coeff;
482                         *c += run + 1;
483
484                 } else {
485                         INT16* p = b + *c;
486                         INT16* e = b + 64;
487
488                         while (p < e) {
489                                 *(p++) = 0;
490                         }
491
492                         *c |= 0x80;
493
494                         eob--;
495                 }
496         }
497
498         *eob_run = eob;
499
500         return TRUE;
501 }
502
503 /* */
504
505 static BOOL DecodeDCTCoefficients(
506         FrameDecoder_t* t,
507         BitDecoder_t*   d)
508 {
509         INT32 i, j, k;
510
511         const HuffmanDecoder_t* huff[3];
512
513         INT32 index   = 0;
514         INT32 eob_run = 0;
515
516         INT32 hy, hc;
517
518         memset(t->Count, 0, t->Index->Blocks);
519
520         for (i = 0; i < 5; i++) {
521                 if (i < 2) {
522                         hy = QT_BitDecoder_GetBits(d, 4);
523                         hc = QT_BitDecoder_GetBits(d, 4);
524                 }
525
526                 huff[0] = t->Setup->Huffman + i * 0x10 + hy;
527                 huff[1] = t->Setup->Huffman + i * 0x10 + hc;
528                 huff[2] = huff[1];
529
530                 for (j = 0; j < COEFFS[i]; j++, index++) {
531                         INT16* block = t->Block;
532                         UINT8* count = t->Count;
533
534                         for (k = 0; k < 3; k++) {
535                                 INT32 blocks = t->Index->BC[k];
536                                 if (!DecodeBlock(
537                                         d,
538                                         huff[k],
539                                         index,
540                                         block,
541                                         count,
542                                         t->Index->BIndex[k],
543                                         blocks,
544                                         &eob_run)) {
545                                         return FALSE;
546                                 }
547
548                                 block += blocks * 64;
549                                 count += blocks;
550                         }
551                 }
552         }
553
554         return TRUE;
555 }
556
557 /* */
558
559 static void UndoDCPrediction(
560         FrameDecoder_t* t)
561 {
562         INT32 i;
563         INT32 x, y;
564
565         INT16* block = t->Block;
566
567         const UINT8* mode = t->BMode;
568
569         for (i = 0; i < 3; i++) {
570                 INT32 bx = t->Index->BX[i];
571                 INT32 by = t->Index->BY[i];
572
573                 INT16 last[3] = { 0 };
574
575                 INT32 v[4] = { 0 };
576
577                 INT32 idx = 0;
578
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) {
583                                         INT32 pred;
584
585                                         INT32 t0 = 0;
586                                         INT32 type = DCP_T[mode[idx]];
587
588                                         if (x > 0) {
589                                                 INT32 i0 = idx - 1;
590                                                 v[0] = block[i0 * 64];
591                                                 if (v[0] != NOT_CODED && DCP_T[mode[i0]] == type) {
592                                                         t0 += DCP_W[0][0];
593                                                 }
594                                         }
595
596                                         if (y > 0) {
597                                                 if (x > 0) {
598                                                         INT32 i1 = idx - bx - 1;
599                                                         v[1] = block[i1 * 64];
600                                                         if (v[1] != NOT_CODED && DCP_T[mode[i1]] == type) {
601                                                                 t0 += DCP_W[0][1];
602                                                         }
603                                                 }
604
605                                                 {
606                                                         INT32 i2 = idx - bx;
607                                                         v[2] = block[i2 * 64];
608                                                         if (v[2] != NOT_CODED && DCP_T[mode[i2]] == type) {
609                                                                 t0 += DCP_W[0][2];
610                                                         }
611                                                 }
612
613                                                 if (x < bx - 1) {
614                                                         INT32 i3 = idx - bx + 1;
615                                                         v[3] = block[i3 * 64];
616                                                         if (v[3] != NOT_CODED && DCP_T[mode[i3]] == type) {
617                                                                 t0 += DCP_W[0][3];
618                                                         }
619                                                 }
620                                         }
621
622                                         if (t0 > 0) {
623                                                 pred =
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;
628
629                                                 if ((t0 & 0x7) == 7) {
630                                                         INT32 d = pred - v[2]; /* D */
631                                                         if (d < -128 || d > 128) {
632                                                                 pred = v[2];
633                                                         } else if (d = pred - v[0], d < -128 || d > 128) { /* L */
634                                                                 pred = v[0];
635                                                         } else if (d = pred - v[1], d < -128 || d > 128) { /* DL */
636                                                                 pred = v[1];
637                                                         }
638                                                 }
639
640                                         } else {
641                                                 pred = last[type];
642                                         }
643
644                                         dc += pred;
645
646                                         block[idx * 64] = dc;
647
648                                         last[type] = dc;
649                                 }
650                         }
651                 }
652
653                 block += t->Index->BC[i] * 64;
654                 mode  += t->Index->BC[i];
655         }
656 }
657
658 /* */
659
660 static void Reconstruct_IntraBlock(
661         FrameDecoder_t* t,
662         Plane_t*        p,
663         INT32           x,
664         INT32           y,
665         const INT16*    block,
666         INT32           plane,
667         Plane_t*        r)
668 {
669         INT16 coeff[64];
670
671         if (block[0] == NOT_CODED) {
672                 if (r != NULL) {
673                         Block_CopyPlane8x8(p, x, y, r);
674                 }
675                 return;
676         }
677
678         Dequantize_DoDequantize(
679                 &(t->Dequantize),
680                 0,
681                 plane,
682                 coeff,
683                 block);
684
685         IDCT_8x8(coeff, coeff);
686
687         Block_CopyIntra8x8(p, x, y, coeff);
688 }
689
690 /* */
691
692 static void Reconstruct_InterBlock(
693         FrameDecoder_t* t,
694         Plane_t*        p,
695         INT32           x,
696         INT32           y,
697         const INT16*    block,
698         INT32           plane,
699         Plane_t*        r)
700 {
701         INT16 coeff[64];
702
703         if (block[0] == NOT_CODED) {
704                 if (r != NULL) {
705                         Block_CopyPlane8x8(p, x, y, r);
706                 }
707                 return;
708         }
709
710         Dequantize_DoDequantize(
711                 &(t->Dequantize),
712                 1,
713                 plane,
714                 coeff,
715                 block);
716
717         IDCT_8x8(coeff, coeff);
718
719         Block_ReviseInter8x8(p, x, y, coeff);
720 }
721
722 /* */
723
724 static void Reconstruct(
725         FrameDecoder_t* t)
726 {
727         INT32 x, y;
728
729         INT32 mx = t->Index->MX;
730         INT32 my = t->Index->MY;
731
732         Plane_t* g = t->Frame[0];
733         Plane_t* p = t->Frame[1];
734         Plane_t* r = t->Frame[2];
735
736         const UINT8* b0 = t->BMode;
737
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;
742
743         const MotionVector_t* mv0 = t->MV;
744
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) {
747                         switch (b0[x * 2]) {
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);
752
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);
759                                 break;
760
761                         case 1: /* INTRA */
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);
768                                 break;
769
770                         case 2: /* INTER_MV */
771                         case 3: /* INTER_MV_LAST */
772                         case 4: /* INTER_MV_LAST2 */
773                         {
774                                 const MotionVector_t* mv = mv0 + x * 2;
775
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);
779
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);
786                                 break;
787                         }
788
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);
793
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);
800                                 break;
801
802                         case 6: /* INTER_GOLDEN_MV */
803                         {
804                                 const MotionVector_t* mv = mv0 + x * 2;
805
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);
809
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);
816                                 break;
817                         }
818
819                         case 7: /* INTER_MV_FOUR */
820                         {
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;
825
826                                 INT32 cmx = v0->X + v1->X + v2->X + v3->X;
827                                 INT32 cmy = v0->Y + v1->Y + v2->Y + v3->Y;
828
829                                 MotionVector_t mv = {
830                                         (cmx + CMV[cmx < 0]) >> 2,
831                                         (cmy + CMV[cmy < 0]) >> 2
832                                 };
833
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);
840
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);
847                                 break;
848                         }
849
850                         } /* switch */
851                 }
852         }
853 }
854
855 /* */
856
857 static void LoopFilter(
858         FrameDecoder_t* t)
859 {
860         INT32 i;
861         INT32 x, y;
862
863         const INT16* b = t->Block;
864
865         Plane_t* plane = t->Frame[1];
866
867         for (i = 0; i < 3; i++, plane++) {
868                 INT32 bx = t->Index->BX[i];
869                 INT32 by = t->Index->BY[i];
870
871                 UINT8* r0 = plane->Plane;
872
873                 for (y = 0; y < by; y++, r0 += plane->Pitch * 8) {
874                         UINT8* r = r0;
875
876                         for (x = 0; x < bx; x++, r += 8, b += 64) {
877                                 if (*b != NOT_CODED) {
878                                         if (x > 0) {
879                                                 Filter_LoopFilterH(&(t->Filter), r, plane->Pitch);
880                                         }
881
882                                         if (y > 0) {
883                                                 Filter_LoopFilterV(&(t->Filter), r, plane->Pitch);
884                                         }
885
886                                         if (x < bx - 1 && b[64 *  1] == NOT_CODED) {
887                                                 Filter_LoopFilterH(&(t->Filter), r + 8, plane->Pitch);
888                                         }
889
890                                         if (y < by - 1 && b[64 * bx] == NOT_CODED) {
891                                                 Filter_LoopFilterV(&(t->Filter), r + 8 * plane->Pitch, plane->Pitch);
892                                         }
893                                 }
894                         }
895                 }
896         }
897 }
898
899 /* */
900
901 BOOL QT_FrameDecoder_DecodeFrame(
902         FrameDecoder_t* t,
903         BitDecoder_t*   d)
904 {
905         if (!FrameHeader_Decode(
906                 &(t->Header),
907                 d)) {
908                 return FALSE;
909         }
910
911         if (t->QIndex != t->Header.QIS[0]) {
912                 t->QIndex  = t->Header.QIS[0];
913                 Dequantize_MakeMatrix(
914                         &(t->Dequantize),
915                         &(t->Setup->Dequantize),
916                         t->QIndex);
917
918                 Filter_Setup(
919                         &(t->Filter),
920                         &(t->Setup->Filter),
921                         t->QIndex);
922         }
923
924         if (t->Header.Type == 0) { /* Intra */
925                 INT16* p = t->Block;
926                 INT16* e = p + 64 * t->Index->Blocks;
927                 for (; p < e; p += 64) {
928                         *p = 0;
929                 }
930
931                 memset(t->BMode, 1, t->Index->Blocks);
932
933                 t->Frame[0] = t->Plane + 0; /* G */
934                 t->Frame[1] = t->Plane + 0; /* C */
935                 t->Frame[2] = t->Plane + 3; /* R */
936
937         } else {
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;
947                 }
948
949                 if (!DecodeCodedBlockFlag(
950                         t,
951                         d)) {
952                         return FALSE;
953                 }
954
955                 if (!DecodeMacroBlockCodingModes(
956                         t,
957                         d)) {
958                         return FALSE;
959                 }
960
961                 if (!DecodeMotionVectors(
962                         t,
963                         d)) {
964                         return FALSE;
965                 }
966         }
967
968         if (!DecodeDCTCoefficients(
969                 t,
970                 d)) {
971                 return FALSE;
972         }
973
974         UndoDCPrediction(t);
975
976         Reconstruct(t);
977
978         if (t->Filter.Limit > 0) {
979                 LoopFilter(t);
980         }
981
982         return TRUE;
983 }
984