OSDN Git Service

add FrameReconstructor_SSE2.
[qtheora/main.git] / Lib / QTheoraEx / FrameDecoder.c
1 /* FrameDecoder.c */
2 /* 2009/06/29     */
3
4 #include "StdAfx.h"
5
6 #include "FrameDecoder.h"
7
8 #include "FrameReconstructor.h"
9
10 /* */
11
12 #pragma warning(disable : 4799)
13
14 /* */
15
16 static BOOL Dequantize_MakeMatrix(
17         DequantizeMatrix_t*      m,
18         const DequantizeTable_t* t,
19         INT32                    index)
20 {
21         INT32 i, j, p, q;
22
23         for (i = 0; i < 2; i++) {
24                 for (p = 0; p < 3; p++) {
25                         INT32 ss, qs;
26                         INT32 si, sj;
27
28                         const UINT8* mi;
29                         const UINT8* mj;
30
31                         INT16* mat;
32
33                         INT32 s = 0;
34                         for (q = 0; q < t->Count[i][p]; q++) {
35                                 s += t->Size[i][p][q];
36                                 if (s >= index) {
37                                         break;
38                                 }
39                         }
40
41                         ss = t->Size[i][p][q];
42                         qs = s - ss;
43                         si = s  - index;
44                         sj = qs - index;
45
46                         mi = t->Matrix[t->Base[i][p][q    ]];
47                         mj = t->Matrix[t->Base[i][p][q + 1]];
48
49                         mat = m->Matrix[i][p];
50
51                         for (j = 0; j < 64; j++) {
52                                 INT32 coeff = (2 * si * mi[j] - 2 * sj * mj[j] + ss) / (2 * ss);
53
54                                 INT32 qmin = 8 << ((j == 0) ? i + 1 : i);
55                                 INT32 qscl = (j == 0) ? t->DCScale[index] : t->ACScale[index];
56
57                                 INT32 v = ((qscl * coeff) / 100) * 4;
58
59                                 mat[j] = (INT16)((v < qmin) ? qmin : ((v > 4096) ? 4096 : v));
60                         }
61                 }
62         }
63
64         return TRUE;
65 }
66
67 static void Filter_Setup(
68         LoopFilter_t*        t,
69         const FilterTable_t* l,
70         INT32                q)
71 {
72         INT32 x;
73         INT32 lim = l->Limit[q];
74
75         INT16* d = t->Delta;
76
77         memset(t->Delta, 0, sizeof(t->Delta));
78
79         t->Limit = lim;
80
81         for (x = 0; x < lim; x++) {
82                 INT32 i = lim + x;
83
84                 if (127 - i >= 0) {
85                         d[127 - i] = x - lim;
86                 }
87
88                 d[127 - x] = -x;
89                 d[127 + x] =  x;
90
91                 if (127 + i < 256) {
92                         d[127 + i] = lim - x;
93                 }
94         }
95 }
96
97 /* */
98
99 static void RunLength_Start(
100         RunLength_t* t,
101         INT32        x)
102 {
103         t->Bit = !x; /* invert later */
104         t->Run = 0;
105 }
106
107 static const UINT8 HLONG[64] = {
108         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
109         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
110         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
111         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
112         0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
113         0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
114         0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
115         0x34,0x34,0x34,0x34,0x45,0x45,0x56,0x66
116 };
117
118 static const INT8 HLONG_BASE[7] = { 1, 2, 4, 6, 10, 18, 34 };
119 static const INT8 HLONG_BITS[7] = { 0, 1, 1, 2,  3,  4, 12 };
120
121 static const UINT8 HSHORT[32] = {
122         0x01,0x01,0x01,0x01,
123         0x01,0x01,0x01,0x01,
124         0x01,0x01,0x01,0x01,
125         0x01,0x01,0x01,0x01,
126         0x12,0x12,0x12,0x12,
127         0x12,0x12,0x12,0x12,
128         0x23,0x23,0x23,0x23,
129         0x34,0x34,0x45,0x55
130 };
131
132 static const INT8 HSHORT_BASE[6] = { 1, 3, 5, 7, 11, 15 };
133 static const INT8 HSHORT_BITS[6] = { 1, 1, 1, 2,  2,  4 };
134
135 #define RL_LONG_DECODE \
136         if (r.Run == 0) { INT32 token; LOAD_BITS token = HLONG[GET_BITS_I(6)]; RETIRE_BITS(token & 0xf); r.Run = HLONG_BASE[token >> 4]; \
137                 if (HLONG_BITS[token >> 4] > 0) { INT32 x; FETCH_BITS(x, HLONG_BITS[token >> 4]) r.Run += x; } r.Bit = !(r.Bit); }           \
138         (r.Run)--;
139
140 #define RL_SHORT_DECODE \
141         if (r.Run == 0) { INT32 token, x; LOAD_BITS token = HSHORT[GET_BITS_I(5)]; RETIRE_BITS(token & 0xf); r.Run = HSHORT_BASE[token >> 4]; \
142                 FETCH_BITS(x, HSHORT_BITS[token >> 4]) r.Run += x; r.Bit = !(r.Bit); }                                                            \
143         (r.Run)--;
144
145 /* */
146
147 static const UINT8 M_MODE[8][8] = {
148         { 0, 0, 0, 0, 0, 0, 0, 0 },
149         { 3, 4, 2, 0, 1, 5, 6, 7 },
150         { 3, 4, 0, 2, 1, 5, 6, 7 },
151         { 3, 2, 4, 0, 1, 5, 6, 7 },
152         { 3, 2, 0, 4, 1, 5, 6, 7 },
153         { 0, 3, 4, 2, 1, 5, 6, 7 },
154         { 0, 5, 3, 4, 2, 1, 6, 7 },
155         { 0, 1, 2, 3, 4, 5, 6, 7 }
156 };
157
158 static const UINT8 H_MODE[128] = {
159         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
160         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
161         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
162         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
163         0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
164         0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
165         0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
166         0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x45,0x45,0x45,0x45,0x56,0x56,0x67,0x77
167 };
168
169 /* */
170
171 static const INT8 MV_VAL[0x100] = {
172            0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
173            0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
174            1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,
175            1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,
176           -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
177           -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
178            2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
179           -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,
180            3,   3,   3,   3,   3,   3,   3,   3,   3,   3,   3,   3,   3,   3,   3,   3,
181           -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,
182            4,   4,   4,   4,  -4,  -4,  -4,  -4,
183            5,   5,   5,   5,  -5,  -5,  -5,  -5,
184            6,   6,   6,   6,  -6,  -6,  -6,  -6,
185            7,   7,   7,   7,  -7,  -7,  -7,  -7,
186            8,   8,  -8,  -8,
187            9,   9,  -9,  -9,
188           10,  10, -10, -10,
189           11,  11, -11, -11,
190           12,  12, -12, -12,
191           13,  13, -13, -13,
192           14,  14, -14, -14,
193           15,  15, -15, -15,
194           16, -16,
195           17, -17,
196           18, -18,
197           19, -19,
198           20, -20,
199           21, -21,
200           22, -22,
201           23, -23,
202           24, -24,
203           25, -25,
204           26, -26,
205           27, -27,
206           28, -28,
207           29, -29,
208           30, -30,
209           31, -31
210 };
211
212 static const INT8 MV_LEN[0x100] = {
213         3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
214         3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
215         3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
216         3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
217         3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
218         3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
219         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
220         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
221         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
222         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
223         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
224         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
225         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
226         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
227         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
228         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
229 };
230
231 /* */
232
233 static INT32 DecodeMV0(MotionVector_t* mv, INT32 bits)
234 {
235         INT32 s;
236         INT32 b0, b1;
237
238         b0 = (bits >> 8) & 0xff;
239         mv->X = MV_VAL[b0];
240         s  = MV_LEN[b0];
241
242         b1 = (bits >> (8 - s)) & 0xff;
243         mv->Y = MV_VAL[b1];
244         s += MV_LEN[b1];
245
246         return s;
247 }
248
249 static INT32 DecodeMV1(MotionVector_t* mv, INT32 bits)
250 {
251         INT32 b0 = (bits >> 10) & 0x3f;
252         INT32 b1 = (bits >>  4) & 0x3f;
253
254         INT8 v[2];
255
256         v[0] = b0 >> 1;
257         v[1] = -v[0];
258         mv->X = v[b0 & 1];
259
260         v[0] = b1 >> 1;
261         v[1] = -v[0];
262         mv->Y = v[b1 & 1];
263
264         return 12;
265 }
266
267 /* */
268
269 static const INT32 CMV[2] = { 2, 1 };
270
271 /* */
272
273 struct DCTCoefficientsContext {
274
275         INT8*  Run;
276         INT16* Coeff;
277
278         INT32 EOB_Run;
279
280         INT32* BlocksCoded;
281
282 }; /* DCTCoefficientsContext */
283
284 typedef struct DCTCoefficientsContext DCTCoefficientsContext_t;
285
286 /* */
287
288 static const INT8 EOB_BITS_LEN[7] = { 0, 0, 0, 2, 3,  4, 12 };
289 static const INT8 EOB_RUN_BASE[7] = { 1, 2, 3, 4, 8, 16,  0 };
290
291 static const INT8 COEFF_SIGN[2] = { 1, -1 };
292
293 static const INT8 COEFF_BITS_LEN[32 - 7] = {
294         0, 0, 0, 0, 0, 0,
295         1, 1, 1, 1,
296         2, 3, 4, 5, 6, 10,
297         1, 1, 1, 1, 1, 1, 1,
298         2, 2
299 };
300
301 static const INT8 COEFF_BASE[32 - 7] = {
302         0, 0, 1, -1, 2, -2,
303         3, 4, 5, 6,
304         7, 9, 13, 21, 37, 69,
305         1, 1, 1, 1, 1, 1, 1,
306         2, 2
307 };
308
309 static const INT8 RUN_BITS_LEN[32 - 7] = {
310         3, 6,
311         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
312         0, 0, 0, 0, 0,
313         2, 3, 0, 1
314 };
315
316 static const INT8 RUN_BASE[32 - 7] = {
317         0, 0,
318         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
319         1, 2, 3, 4, 5,
320         6, 10, 1, 2
321 };
322
323 /* */
324
325 static const INT8 COEFFS[5] = { 1, 5, 9, 13, 36 };
326
327 /* */
328
329 static void DecodeDCCoefficients(FrameDecoder_t* t)
330 {
331         INT32 i;
332
333         INT32 eob = 0;
334
335         const UINT8* c = t->Count;
336
337         INT16* dc = t->DC;
338
339         for (i = 0; i < 3; i++) {
340                 const INT8*  br = t->BRun  [i][0];
341                 const INT16* bc = t->BCoeff[i][0];
342
343                 const UINT8* end = c + t->Index->BC[i];
344
345                 const UINT16* bi = t->Index->BIndex[i];
346
347                 for (; ; ) {
348                         INT16 coeff = 0;
349
350                         for (; c < end && *c == 0xff; c++) {
351                                 dc[*(bi++)] = NOT_CODED;
352                         }
353
354                         if (c >= end) {
355                                 break;
356                         }
357
358                         if (eob == 0) {
359                                 INT8  rr = *(br++);
360                                 INT16 cc = *(bc++);
361
362                                 if (rr == 0) {
363                                         coeff = cc;
364
365                                 } else if (rr < 0) {
366                                         eob = cc;
367                                 }
368                         }
369
370                         if (eob > 0) {
371                                 eob--;
372                         }
373
374                         dc[*(bi++)] = coeff;
375
376                         c++;
377                 }
378
379                 dc += t->Index->BC[i];
380         }
381 }
382
383 /* */
384
385 static const INT32 DCP_W[16][4] = {
386         /*  L    DL    D   DR */
387         {   1,    2,   4,   8 },
388
389         { 128,    0,   0,   0 }, /*  1 */
390         {   0,  128,   0,   0 }, /*  2 */
391         { 128,    0,   0,   0 }, /*  3 */
392         {   0,    0, 128,   0 }, /*  4 */
393         {  64,    0,  64,   0 }, /*  5 */
394         {   0,    0, 128,   0 }, /*  6 */
395         { 116, -104, 116,   0 }, /*  7 */
396         {   0,    0,   0, 128 }, /*  8 */
397         {  75,    0,   0,  53 }, /*  9 */
398         {   0,   64,   0,  64 }, /* 10 */
399         {  75,    0,   0,  53 }, /* 11 */
400         {   0,    0, 128,   0 }, /* 12 */
401         {  75,    0,   0,  53 }, /* 13 */
402         {   0,   24,  80,  24 }, /* 14 */
403         { 116, -104, 116,   0 }  /* 15 */
404
405 };
406
407 static const INT32 DCP_T[8] = {
408         1, 0, 1, 1,
409         1, 2, 2, 1
410 };
411
412 /* */
413
414 static void UndoDCPrediction(
415         FrameDecoder_t* t)
416 {
417         INT32 i;
418         INT32 x, y;
419
420         INT16* DC = t->DC;
421
422         const UINT8* mode = t->BMode;
423
424         for (i = 0; i < 3; i++) {
425                 INT32 bx = t->Index->BX[i];
426                 INT32 by = t->Index->BY[i];
427
428                 INT16 last[3] = { 0 };
429
430                 INT32 v[4] = { 0 };
431
432                 INT32 idx = 0;
433
434                 for (y = 0; y < by; y++) {
435                         for (x = 0; x < bx; x++, idx++) {
436                                 INT32 dc = DC[idx];
437                                 if (dc != NOT_CODED) {
438                                         INT32 pred;
439
440                                         INT32 t0 = 0;
441                                         INT32 type = DCP_T[mode[idx]];
442
443                                         if (x > 0) {
444                                                 INT32 i0 = idx - 1;
445                                                 v[0] = DC[i0];
446                                                 if (v[0] != NOT_CODED && DCP_T[mode[i0]] == type) {
447                                                         t0 += DCP_W[0][0];
448                                                 }
449                                         }
450
451                                         if (y > 0) {
452                                                 if (x > 0) {
453                                                         INT32 i1 = idx - bx - 1;
454                                                         v[1] = DC[i1];
455                                                         if (v[1] != NOT_CODED && DCP_T[mode[i1]] == type) {
456                                                                 t0 += DCP_W[0][1];
457                                                         }
458                                                 }
459
460                                                 {
461                                                         INT32 i2 = idx - bx;
462                                                         v[2] = DC[i2];
463                                                         if (v[2] != NOT_CODED && DCP_T[mode[i2]] == type) {
464                                                                 t0 += DCP_W[0][2];
465                                                         }
466                                                 }
467
468                                                 if (x < bx - 1) {
469                                                         INT32 i3 = idx - bx + 1;
470                                                         v[3] = DC[i3];
471                                                         if (v[3] != NOT_CODED && DCP_T[mode[i3]] == type) {
472                                                                 t0 += DCP_W[0][3];
473                                                         }
474                                                 }
475                                         }
476
477                                         if (t0 > 0) {
478                                                 pred =
479                                                         ( v[0] * DCP_W[t0][0]
480                                                         + v[1] * DCP_W[t0][1]
481                                                         + v[2] * DCP_W[t0][2]
482                                                         + v[3] * DCP_W[t0][3] ) / 128;
483
484                                                 if ((t0 & 0x7) == 7) {
485                                                         INT32 d = pred - v[2]; /* D */
486                                                         if (d < -128 || d > 128) {
487                                                                 pred = v[2];
488                                                         } else if (d = pred - v[0], d < -128 || d > 128) { /* L */
489                                                                 pred = v[0];
490                                                         } else if (d = pred - v[1], d < -128 || d > 128) { /* DL */
491                                                                 pred = v[1];
492                                                         }
493                                                 }
494
495                                         } else {
496                                                 pred = last[type];
497                                         }
498
499                                         dc += pred;
500
501                                         DC[idx] = dc;
502
503                                         last[type] = dc;
504                                 }
505                         }
506                 }
507
508                 DC   += t->Index->BC[i];
509                 mode += t->Index->BC[i];
510         }
511 }
512
513 /* */
514
515 #define ARCH_C
516
517 #include "QTheoraArch.h"
518
519 #define FrameHeader_Decode FrameHeader_Decode_C
520
521 #define FrameDecoder_DecodeCodedBlockFlag        FrameDecoder_DecodeCodedBlockFlag_C
522 #define FrameDecoder_DecodeMacroBlockCodingModes FrameDecoder_DecodeMacroBlockCodingModes_C
523 #define FrameDecoder_DecodeMotionVectors         FrameDecoder_DecodeMotionVectors_C
524
525 #define FrameDecoder_DecodeBlocks          FrameDecoder_DecodeBlocks_C
526 #define FrameDecoder_DecodeDCTCoefficients FrameDecoder_DecodeDCTCoefficients_C
527
528 #define FrameDecoder_Decode FrameDecoder_Decode_C
529
530 #include "FrameDecoder_Impl.h"
531
532 #undef ARCH_C
533
534 /* */
535
536 #define ARCH_X86
537
538 #include "QTheoraArch.h"
539
540 #define FrameHeader_Decode FrameHeader_Decode_X86
541
542 #define FrameDecoder_DecodeCodedBlockFlag        FrameDecoder_DecodeCodedBlockFlag_X86
543 #define FrameDecoder_DecodeMacroBlockCodingModes FrameDecoder_DecodeMacroBlockCodingModes_X86
544 #define FrameDecoder_DecodeMotionVectors         FrameDecoder_DecodeMotionVectors_X86
545
546 #define FrameDecoder_DecodeBlocks          FrameDecoder_DecodeBlocks_X86
547 #define FrameDecoder_DecodeDCTCoefficients FrameDecoder_DecodeDCTCoefficients_X86
548
549 #define FrameDecoder_Decode FrameDecoder_Decode_X86
550
551 #include "FrameDecoder_Impl.h"
552
553 #undef ARCH_X86
554
555 /* */
556
557 #define ARCH_MMX
558
559 #include "QTheoraArch.h"
560
561 #define FrameHeader_Decode FrameHeader_Decode_MMX
562
563 #define FrameDecoder_DecodeCodedBlockFlag        FrameDecoder_DecodeCodedBlockFlag_MMX
564 #define FrameDecoder_DecodeMacroBlockCodingModes FrameDecoder_DecodeMacroBlockCodingModes_MMX
565 #define FrameDecoder_DecodeMotionVectors         FrameDecoder_DecodeMotionVectors_MMX
566
567 #define FrameDecoder_DecodeBlocks          FrameDecoder_DecodeBlocks_MMX
568 #define FrameDecoder_DecodeDCTCoefficients FrameDecoder_DecodeDCTCoefficients_MMX
569
570 #define FrameDecoder_Decode FrameDecoder_Decode_MMX
571
572 #include "FrameDecoder_Impl.h"
573
574 #undef ARCH_MMX
575
576 /* */
577
578 BOOL QT_FrameDecoder_Setup(
579         FrameDecoder_t*      t,
580         const BlockIndex_t*  index,
581         const SetupHeader_t* setup,
582         MemoryPool_t*        pool)
583 {
584         extern BOOL g_QT_Enable_X86;
585         extern BOOL g_QT_Enable_MMX;
586         extern BOOL g_QT_Enable_SSE2;
587
588         INT32 i, j;
589
590         /* */
591
592         memset(t, 0, sizeof(FrameDecoder_t));
593
594         t->Index = index;
595         t->Setup = setup;
596
597         /* */
598
599         for (i = 0; i < 3; i++) {
600                 Plane_t* r = t->Plane + i * 3;
601
602                 INT32 cb = index->MX * 16 * index->MY * 16;
603
604                 UINT8* p = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * cb);
605                 if (p == NULL) {
606                         return FALSE;
607                 }
608
609                 r->Plane = p;
610                 r->Pitch = index->MX * 16;
611                 r->CX    = index->MX * 16;
612                 r->CY    = index->MY * 16;
613
614                 for (j = 1; j < 3; j++) {
615                         p = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * cb / 4);
616                         if (p == NULL) {
617                                 return FALSE;
618                         }
619
620                         r[j].Plane = p;
621                         r[j].Pitch = index->MX * 8;
622                         r[j].CX    = index->MX * 8;
623                         r[j].CY    = index->MY * 8;
624                 }
625         }
626
627         for (i = 0; i < 3; i++) {
628                 t->Frame[i] = t->Plane + i * 3;
629         }
630
631         /* */
632
633         if (g_QT_Enable_SSE2) {
634                 t->Reconstructor = (FrameReconstructor_SSE2_t*)QT_MemoryPool_Allocate(pool, sizeof(FrameReconstructor_SSE2_t));
635                 if (t->Reconstructor == NULL) {
636                         return FALSE;
637                 }
638         }
639
640         /* */
641
642         t->QIndex = -1;
643
644         /* */
645
646         t->SBCoded = (INT8*)QT_MemoryPool_Allocate(pool, sizeof(INT8) * index->SBlocks);
647         if (t->SBCoded == NULL) {
648                 return FALSE;
649         }
650
651         t->Count = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->Blocks);
652         if (t->Count == NULL) {
653                 return FALSE;
654         }
655
656         t->MBMode = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->MC);
657         if (t->MBMode == NULL) {
658                 return FALSE;
659         }
660
661         t->BMode = (UINT8*)QT_MemoryPool_Allocate(pool, sizeof(UINT8) * index->Blocks);
662         if (t->BMode == NULL) {
663                 return FALSE;
664         }
665
666         t->MV = (MotionVector_t*)QT_MemoryPool_Allocate(pool, sizeof(MotionVector_t) * index->BC[0]);
667         if (t->MV == NULL) {
668                 return FALSE;
669         }
670
671         t->MVC = (MotionVector_t*)QT_MemoryPool_Allocate(pool, sizeof(MotionVector_t) * index->BC[1]);
672         if (t->MVC == NULL) {
673                 return FALSE;
674         }
675
676         t->DCTRun = (INT8*)QT_MemoryPool_Allocate(pool, sizeof(INT8) * index->Blocks * 64);
677         if (t->DCTRun == NULL) {
678                 return FALSE;
679         }
680
681         t->DCTCoeff = (INT16*)QT_MemoryPool_Allocate(pool, sizeof(INT16) * index->Blocks * 64);
682         if (t->DCTCoeff == NULL) {
683                 return FALSE;
684         }
685
686         t->DC = (INT16*)QT_MemoryPool_Allocate(pool, sizeof(INT16) * index->Blocks);
687         if (t->DC == NULL) {
688                 return FALSE;
689         }
690
691         /* */
692
693         if (g_QT_Enable_SSE2 || g_QT_Enable_MMX) {
694                 t->Decode = FrameDecoder_Decode_MMX;
695         } else if (g_QT_Enable_X86) {
696                 t->Decode = FrameDecoder_Decode_X86;
697         } else {
698                 t->Decode = FrameDecoder_Decode_C;
699         }
700
701         /* */
702
703         if (g_QT_Enable_SSE2) {
704                 t->UpdateDequantizeMatrix = QT_UpdateDequantizeMatrix_SSE2;
705         } else {
706                 t->UpdateDequantizeMatrix = NULL;
707         }
708
709         if (g_QT_Enable_SSE2) {
710                 t->Reconstruct = QT_ReconstructFrame_SSE2;
711         } else {
712                 t->Reconstruct = QT_ReconstructFrame;
713         }
714
715         /* */
716
717         return TRUE;
718 }
719
720 /* */
721
722 BOOL QT_FrameDecoder_DecodeFrame(
723         FrameDecoder_t* t,
724         const VOID*     p,
725         SIZE_T          size)
726 {
727         extern BOOL g_QT_Available_MMX;
728
729         BOOL b = t->Decode(t, p, size);
730
731         if (g_QT_Available_MMX) {
732                 _mm_empty();
733         }
734
735         return b;
736 }
737
738 /* */
739