/[pcsx2_0.9.7]/trunk/pcsx2/IPU/mpeg2lib/Mpeg.cpp
ViewVC logotype

Annotation of /trunk/pcsx2/IPU/mpeg2lib/Mpeg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (10 years ago) by william
File size: 34891 byte(s)
committing r3113 initial commit again...
1 william 31 /*
2     * Mpeg.c
3     * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4     * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5     * Modified by Florin for PCSX2 emu
6     *
7     * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8     * See http://libmpeg2.sourceforge.net/ for updates.
9     *
10     * mpeg2dec is free software; you can redistribute it and/or modify
11     * it under the terms of the GNU General Public License as published by
12     * the Free Software Foundation; either version 2 of the License, or
13     * (at your option) any later version.
14     *
15     * mpeg2dec is distributed in the hope that it will be useful,
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     * GNU General Public License for more details.
19     *
20     * You should have received a copy of the GNU General Public License
21     * along with this program; if not, write to the Free Software
22     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23     */
24    
25     // [Air] Note: many functions in this module are large and only used once, so they
26     // have been forced to inline since it won't bloat the program and gets rid of
27     // some call overhead.
28    
29     #include "PrecompiledHeader.h"
30    
31     #include "Common.h"
32     #include "IPU/IPU.h"
33     #include "Mpeg.h"
34     #include "Vlc.h"
35    
36     int non_linear_quantizer_scale [] =
37     {
38     0, 1, 2, 3, 4, 5, 6, 7,
39     8, 10, 12, 14, 16, 18, 20, 22,
40     24, 28, 32, 36, 40, 44, 48, 52,
41     56, 64, 72, 80, 88, 96, 104, 112
42     };
43    
44     /* Bitstream and buffer needs to be reallocated in order for successful
45     reading of the old data. Here the old data stored in the 2nd slot
46     of the internal buffer is copied to 1st slot, and the new data read
47     into 1st slot is copied to the 2nd slot. Which will later be copied
48     back to the 1st slot when 128bits have been read.
49     */
50     extern void ReorderBitstream();
51    
52     int get_macroblock_modes(decoder_t * const decoder)
53     {
54     #define bit_buf (decoder->bitstream_buf)
55     #define bits (decoder->bitstream_bits)
56     #define bit_ptr (decoder->bitstream_ptr)
57     int macroblock_modes;
58     const MBtab * tab;
59    
60     switch (decoder->coding_type)
61     {
62    
63     case I_TYPE:
64     macroblock_modes = UBITS(bit_buf, 2);
65    
66     if (macroblock_modes == 0) return 0; // error
67    
68     tab = MB_I + (macroblock_modes >> 1);
69     DUMPBITS(bit_buf, bits, tab->len);
70     macroblock_modes = tab->modes;
71    
72     if ((!(decoder->frame_pred_frame_dct)) &&
73     (decoder->picture_structure == FRAME_PICTURE))
74     {
75     macroblock_modes |= UBITS(bit_buf, 1) * DCT_TYPE_INTERLACED;
76     DUMPBITS(bit_buf, bits, 1);
77     }
78     return macroblock_modes;
79    
80     case P_TYPE:
81     macroblock_modes = UBITS(bit_buf, 6);
82    
83     if (macroblock_modes == 0) return 0; // error
84    
85     tab = MB_P + (macroblock_modes >> 1);
86     DUMPBITS(bit_buf, bits, tab->len);
87     macroblock_modes = tab->modes;
88    
89     if (decoder->picture_structure != FRAME_PICTURE)
90     {
91     if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
92     {
93     macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
94     DUMPBITS(bit_buf, bits, 2);
95     }
96    
97     return macroblock_modes;
98     }
99     else if (decoder->frame_pred_frame_dct)
100     {
101     if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
102     macroblock_modes |= MC_FRAME;
103    
104     return macroblock_modes;
105     }
106     else
107     {
108     if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
109     {
110     macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
111     DUMPBITS(bit_buf, bits, 2);
112     }
113    
114     if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
115     {
116     macroblock_modes |= UBITS(bit_buf, 1) * DCT_TYPE_INTERLACED;
117     DUMPBITS(bit_buf, bits, 1);
118     }
119    
120     return macroblock_modes;
121     }
122    
123     case B_TYPE:
124     macroblock_modes = UBITS(bit_buf, 6);
125    
126     if (macroblock_modes == 0) return 0; // error
127    
128     tab = MB_B + macroblock_modes;
129     DUMPBITS(bit_buf, bits, tab->len);
130     macroblock_modes = tab->modes;
131    
132     if (decoder->picture_structure != FRAME_PICTURE)
133     {
134     if (!(macroblock_modes & MACROBLOCK_INTRA))
135     {
136     macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
137     DUMPBITS(bit_buf, bits, 2);
138     }
139    
140     return macroblock_modes;
141     }
142     else if (decoder->frame_pred_frame_dct)
143     {
144     /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
145     macroblock_modes |= MC_FRAME;
146     return macroblock_modes;
147     }
148     else
149     {
150     if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
151    
152     macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
153     DUMPBITS(bit_buf, bits, 2);
154    
155     if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
156     {
157     intra:
158     macroblock_modes |= UBITS(bit_buf, 1) * DCT_TYPE_INTERLACED;
159     DUMPBITS(bit_buf, bits, 1);
160     }
161    
162     return macroblock_modes;
163     }
164    
165     case D_TYPE:
166     macroblock_modes = UBITS(bit_buf, 1);
167    
168     if (macroblock_modes == 0) return 0; // error
169    
170     DUMPBITS(bit_buf, bits, 1);
171     return MACROBLOCK_INTRA;
172    
173     default:
174     return 0;
175     }
176    
177     #undef bit_buf
178     #undef bits
179     #undef bit_ptr
180     }
181    
182     static __forceinline int get_quantizer_scale(decoder_t * const decoder)
183     {
184     int quantizer_scale_code;
185    
186     quantizer_scale_code = UBITS(decoder->bitstream_buf, 5);
187     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, 5);
188    
189     if (decoder->q_scale_type)
190     return non_linear_quantizer_scale [quantizer_scale_code];
191     else
192     return quantizer_scale_code << 1;
193     }
194    
195     static __forceinline int get_coded_block_pattern(decoder_t * const decoder)
196     {
197     const CBPtab * tab;
198    
199     NEEDBITS(decoder->bitstream_buf, decoder->bitstream_bits, decoder->bitstream_ptr);
200    
201     if (decoder->bitstream_buf >= 0x20000000)
202     tab = CBP_7 + (UBITS(decoder->bitstream_buf, 7) - 16);
203     else
204     tab = CBP_9 + UBITS(decoder->bitstream_buf, 9);
205    
206     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, tab->len);
207     return tab->cbp;
208     }
209    
210     static __forceinline int get_luma_dc_dct_diff(decoder_t * const decoder)
211     {
212     #define bit_buf (decoder->bitstream_buf)
213     #define bits (decoder->bitstream_bits)
214     #define bit_ptr (decoder->bitstream_ptr)
215    
216     const DCtab * tab;
217     int size;
218     int dc_diff;
219    
220     if (bit_buf < 0xf8000000)
221     {
222     tab = DC_lum_5 + UBITS(bit_buf, 5);
223     size = tab->size;
224    
225     if (size)
226     {
227     DUMPBITS(bit_buf, bits, tab->len);
228     bits += size;
229     dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
230     bit_buf <<= size;
231     return dc_diff;
232     }
233     else
234     {
235     DUMPBITS(bit_buf, bits, 3);
236     return 0;
237     }
238     }
239    
240     tab = DC_long + (UBITS(bit_buf, 9) - 0x1e0); //0x1e0);
241    
242     size = tab->size;
243     DUMPBITS(bit_buf, bits, tab->len);
244     NEEDBITS(bit_buf, bits, bit_ptr);
245     dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
246     DUMPBITS(bit_buf, bits, size);
247     return dc_diff;
248     #undef bit_buf
249     #undef bits
250     #undef bit_ptr
251     }
252    
253     static __forceinline int get_chroma_dc_dct_diff(decoder_t * const decoder)
254     {
255     #define bit_buf (decoder->bitstream_buf)
256     #define bits (decoder->bitstream_bits)
257     #define bit_ptr (decoder->bitstream_ptr)
258    
259     const DCtab * tab;
260     int size;
261     int dc_diff;
262    
263     if (bit_buf < 0xf8000000)
264     {
265     tab = DC_chrom_5 + UBITS(bit_buf, 5);
266     size = tab->size;
267    
268     if (size)
269     {
270     DUMPBITS(bit_buf, bits, tab->len);
271     bits += size;
272     dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
273     bit_buf <<= size;
274     return dc_diff;
275     }
276     else
277     {
278     DUMPBITS(bit_buf, bits, 2);
279     return 0;
280     }
281     }
282    
283     tab = DC_long + (UBITS(bit_buf, 10) - 0x3e0);
284    
285     size = tab->size;
286     DUMPBITS(bit_buf, bits, tab->len + 1);
287     NEEDBITS(bit_buf, bits, bit_ptr);
288     dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
289     DUMPBITS(bit_buf, bits, size);
290     return dc_diff;
291     #undef bit_buf
292     #undef bits
293     #undef bit_ptr
294     }
295    
296     #define SATURATE(val) \
297     do { \
298     if (((u32)(val + 2048) > 4095)) \
299     val = SBITS (val, 1) ^ 2047; \
300     } while (0)
301    
302     static __forceinline void get_intra_block_B14(decoder_t * const decoder)
303     {
304     int i;
305     int j;
306     int val;
307     const u8 * scan = decoder->scan;
308     const u8 * quant_matrix = decoder->intra_quantizer_matrix;
309     int quantizer_scale = decoder->quantizer_scale;
310     int mismatch;
311     const DCTtab * tab;
312     u32 bit_buf;
313     u8 * bit_ptr;
314     int bits;
315     s16 * dest;
316    
317     dest = decoder->DCTblock;
318     i = 0;
319     mismatch = ~dest[0];
320    
321     bit_buf = decoder->bitstream_buf;
322     bits = decoder->bitstream_bits;
323     bit_ptr = decoder->bitstream_ptr;
324    
325     NEEDBITS(bit_buf, bits, bit_ptr);
326    
327     while (1)
328     {
329     if (bit_buf >= 0x28000000)
330     {
331     tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
332     i += tab->run;
333     if (i >= 64) break; /* end of block */
334    
335     normal_code:
336     j = scan[i];
337     bit_buf <<= tab->len;
338     bits += tab->len + 1;
339    
340     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
341     val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
342    
343     /* if (bitstream_get (1)) val = -val; */
344     val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
345    
346     SATURATE(val);
347     dest[j] = val;
348     mismatch ^= val;
349     bit_buf <<= 1;
350     NEEDBITS(bit_buf, bits, bit_ptr);
351     continue;
352     }
353     else if (bit_buf >= 0x04000000)
354     {
355     tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
356     i += tab->run;
357    
358     if (i < 64) goto normal_code;
359    
360     /* escape code */
361    
362     i += UBITS(bit_buf << 6, 6) - 64;
363    
364     if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
365    
366     j = scan[i];
367    
368     DUMPBITS(bit_buf, bits, 12);
369     NEEDBITS(bit_buf, bits, bit_ptr);
370    
371     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
372     val = (SBITS(bit_buf, 12) * quantizer_scale * quant_matrix[i]) / 16;
373    
374     SATURATE(val);
375     dest[j] = val;
376     mismatch ^= val;
377     DUMPBITS(bit_buf, bits, 12);
378     NEEDBITS(bit_buf, bits, bit_ptr);
379     continue;
380    
381     }
382     else if (bit_buf >= 0x02000000)
383     {
384     tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
385     i += tab->run;
386    
387     if (i < 64) goto normal_code;
388     }
389     else if (bit_buf >= 0x00800000)
390     {
391     tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
392     i += tab->run;
393    
394     if (i < 64) goto normal_code;
395     }
396     else if (bit_buf >= 0x00200000)
397     {
398     tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
399     i += tab->run;
400    
401     if (i < 64) goto normal_code;
402     }
403     else
404     {
405     tab = DCT_16 + UBITS(bit_buf, 16);
406     bit_buf <<= 16;
407     GETWORD(&bit_buf, bits + 16);
408     i += tab->run;
409    
410     if (i < 64) goto normal_code;
411     }
412    
413     break; /* illegal, check needed to avoid buffer overflow */
414     }
415    
416     dest[63] ^= mismatch & 1;
417    
418     if ((bit_buf >> 30) != 0x2) ipuRegs->ctrl.ECD = 1;
419    
420     DUMPBITS(bit_buf, bits, tab->len); /* dump end of block code */
421    
422     decoder->bitstream_buf = bit_buf;
423     decoder->bitstream_bits = bits;
424     }
425    
426     static __forceinline void get_intra_block_B15(decoder_t * const decoder)
427     {
428     int i;
429     int j;
430     int val;
431     const u8 * scan = decoder->scan;
432     const u8 * quant_matrix = decoder->intra_quantizer_matrix;
433     int quantizer_scale = decoder->quantizer_scale;
434     int mismatch;
435     const DCTtab * tab;
436     u32 bit_buf;
437     u8 * bit_ptr;
438     int bits;
439     s16 * dest;
440    
441     dest = decoder->DCTblock;
442     i = 0;
443     mismatch = ~dest[0];
444    
445     bit_buf = decoder->bitstream_buf;
446     bits = decoder->bitstream_bits;
447     bit_ptr = decoder->bitstream_ptr;
448    
449     NEEDBITS(bit_buf, bits, bit_ptr);
450    
451     while (1)
452     {
453     if (bit_buf >= 0x04000000)
454     {
455     tab = DCT_B15_8 + (UBITS(bit_buf, 8) - 4);
456     i += tab->run;
457    
458     if (i < 64)
459     {
460     normal_code:
461     j = scan[i];
462     bit_buf <<= tab->len;
463     bits += tab->len + 1;
464     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
465     val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
466    
467     /* if (bitstream_get (1)) val = -val; */
468     val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
469    
470     SATURATE(val);
471     dest[j] = val;
472     mismatch ^= val;
473    
474     bit_buf <<= 1;
475     NEEDBITS(bit_buf, bits, bit_ptr);
476    
477     continue;
478     }
479     else
480     {
481     /* end of block. I commented out this code because if we */
482     /* dont exit here we will still exit at the later test :) */
483     //if (i >= 128) break; /* end of block */
484     /* escape code */
485    
486     i += UBITS(bit_buf << 6, 6) - 64;
487    
488     if (i >= 64) break; /* illegal, check against buffer overflow */
489    
490     j = scan[i];
491     DUMPBITS(bit_buf, bits, 12);
492     NEEDBITS(bit_buf, bits, bit_ptr);
493    
494     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
495     val = (SBITS(bit_buf, 12) * quantizer_scale * quant_matrix[i]) / 16;
496    
497     SATURATE(val);
498     dest[j] = val;
499     mismatch ^= val;
500     DUMPBITS(bit_buf, bits, 12);
501     NEEDBITS(bit_buf, bits, bit_ptr);
502     continue;
503     }
504     }
505     else if (bit_buf >= 0x02000000)
506     {
507     tab = DCT_B15_10 + (UBITS(bit_buf, 10) - 8);
508     i += tab->run;
509    
510     if (i < 64) goto normal_code;
511     }
512     else if (bit_buf >= 0x00800000)
513     {
514     tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
515     i += tab->run;
516    
517     if (i < 64) goto normal_code;
518     }
519     else if (bit_buf >= 0x00200000)
520     {
521     tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
522     i += tab->run;
523    
524     if (i < 64) goto normal_code;
525     }
526     else
527     {
528     tab = DCT_16 + UBITS(bit_buf, 16);
529     bit_buf <<= 16;
530     GETWORD(&bit_buf, bits + 16);
531     i += tab->run;
532    
533     if (i < 64) goto normal_code;
534     }
535    
536     break; /* illegal, check needed to avoid buffer overflow */
537     }
538    
539     dest[63] ^= mismatch & 1;
540    
541     if ((bit_buf >> 28) != 0x6)
542     ipuRegs->ctrl.ECD = 1;
543    
544     DUMPBITS(bit_buf, bits, tab->len); /* dump end of block code */
545    
546     decoder->bitstream_buf = bit_buf;
547    
548     decoder->bitstream_bits = bits;
549     }
550    
551     static __forceinline int get_non_intra_block(decoder_t * const decoder)
552     {
553     #define bit_buf (decoder->bitstream_buf)
554     #define bits (decoder->bitstream_bits)
555     #define bit_ptr (decoder->bitstream_ptr)
556     int i;
557     int j;
558     int val;
559     const u8 * scan = decoder->scan;
560     const u8 * quant_matrix = decoder->non_intra_quantizer_matrix;
561     int quantizer_scale = decoder->quantizer_scale;
562     int mismatch;
563     const DCTtab * tab;
564     s16 * dest;
565    
566     i = -1;
567     mismatch = -1;
568     dest = decoder->DCTblock;
569     NEEDBITS(bit_buf, bits, bit_ptr);
570    
571     if (bit_buf >= 0x28000000)
572     {
573     tab = DCT_B14DC_5 + (UBITS(bit_buf, 5) - 5);
574     goto entry_1;
575     }
576     else
577     goto entry_2;
578    
579     while (1)
580     {
581     if (bit_buf >= 0x28000000)
582     {
583     tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
584     entry_1:
585     i += tab->run;
586    
587     if (i >= 64) break; /* end of block */
588     normal_code:
589     j = scan[i];
590     bit_buf <<= tab->len;
591     bits += tab->len + 1;
592    
593     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
594     val = ((2 * tab->level + 1) * quantizer_scale * quant_matrix[i]) >> 5;
595    
596     /* if (bitstream_get (1)) val = -val; */
597     val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
598    
599     SATURATE(val);
600     dest[j] = val;
601     mismatch ^= val;
602     bit_buf <<= 1;
603     NEEDBITS(bit_buf, bits, bit_ptr);
604     continue;
605     }
606     entry_2:
607    
608     if (bit_buf >= 0x04000000)
609     {
610     tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
611     i += tab->run;
612    
613     if (i < 64) goto normal_code;
614    
615     /* escape code */
616    
617     i += UBITS(bit_buf << 6, 6) - 64;
618    
619     if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
620    
621     j = scan[i];
622     DUMPBITS(bit_buf, bits, 12);
623     NEEDBITS(bit_buf, bits, bit_ptr);
624     val = 2 * (SBITS(bit_buf, 12) + SBITS(bit_buf, 1)) + 1;
625    
626     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
627     val = (val * quantizer_scale * quant_matrix[i]) / 32;
628    
629     SATURATE(val);
630     dest[j] = val;
631     mismatch ^= val;
632     DUMPBITS(bit_buf, bits, 12);
633     NEEDBITS(bit_buf, bits, bit_ptr);
634     continue;
635     }
636     else if (bit_buf >= 0x02000000)
637     {
638     tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
639     i += tab->run;
640    
641     if (i < 64) goto normal_code;
642     }
643     else if (bit_buf >= 0x00800000)
644     {
645     tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
646     i += tab->run;
647    
648     if (i < 64) goto normal_code;
649     }
650     else if (bit_buf >= 0x00200000)
651     {
652     tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
653     i += tab->run;
654    
655     if (i < 64) goto normal_code;
656     }
657     else
658     {
659     tab = DCT_16 + UBITS(bit_buf, 16);
660     bit_buf <<= 16;
661     GETWORD(&bit_buf, bits + 16);
662     i += tab->run;
663    
664     if (i < 64) goto normal_code;
665     }
666     break; /* illegal, check needed to avoid buffer overflow */
667     }
668    
669     dest[63] ^= mismatch & 1;
670    
671     if ((bit_buf >> 30) != 0x2) ipuRegs->ctrl.ECD = 1;
672    
673     DUMPBITS(bit_buf, bits, tab->len); /* dump end of block code */
674    
675     decoder->bitstream_buf = bit_buf;
676     decoder->bitstream_bits = bits;
677     return i;
678    
679     #undef bit_buf
680     #undef bits
681     #undef bit_ptr
682     }
683    
684     static __forceinline void get_mpeg1_intra_block(decoder_t * const decoder)
685     {
686     int i;
687     int j;
688     int val;
689     const u8 * scan = decoder->scan;
690     const u8 * quant_matrix = decoder->intra_quantizer_matrix;
691     int quantizer_scale = decoder->quantizer_scale;
692     const DCTtab * tab;
693     u32 bit_buf;
694     int bits;
695     u8 * bit_ptr;
696     s16 * dest;
697    
698     i = 0;
699     dest = decoder->DCTblock;
700     bit_buf = decoder->bitstream_buf;
701     bits = decoder->bitstream_bits;
702     bit_ptr = decoder->bitstream_ptr;
703     NEEDBITS(bit_buf, bits, bit_ptr);
704    
705     while (1)
706     {
707     if (bit_buf >= 0x28000000)
708     {
709     tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
710     i += tab->run;
711    
712     if (i >= 64) break; /* end of block */
713    
714     normal_code:
715     j = scan[i];
716     bit_buf <<= tab->len;
717     bits += tab->len + 1;
718    
719     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
720     val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
721    
722     /* oddification */
723     val = (val - 1) | 1;
724    
725     /* if (bitstream_get (1)) val = -val; */
726     val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
727    
728     SATURATE(val);
729     dest[j] = val;
730     bit_buf <<= 1;
731     NEEDBITS(bit_buf, bits, bit_ptr);
732     continue;
733    
734     }
735     else if (bit_buf >= 0x04000000)
736     {
737     tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
738     i += tab->run;
739    
740     if (i < 64) goto normal_code;
741    
742     /* escape code */
743    
744     i += UBITS(bit_buf << 6, 6) - 64;
745    
746     if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
747    
748     j = scan[i];
749     DUMPBITS(bit_buf, bits, 12);
750     NEEDBITS(bit_buf, bits, bit_ptr);
751     val = SBITS(bit_buf, 8);
752    
753     if (!(val & 0x7f))
754     {
755     DUMPBITS(bit_buf, bits, 8);
756     val = UBITS(bit_buf, 8) + 2 * val;
757     }
758    
759     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
760     val = (val * quantizer_scale * quant_matrix[i]) >> 4;
761    
762     /* oddification */
763     val = (val + ~SBITS(val, 1)) | 1;
764    
765     SATURATE(val);
766     dest[j] = val;
767     DUMPBITS(bit_buf, bits, 8);
768     NEEDBITS(bit_buf, bits, bit_ptr);
769     continue;
770     }
771     else if (bit_buf >= 0x02000000)
772     {
773     tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
774     i += tab->run;
775    
776     if (i < 64) goto normal_code;
777     }
778     else if (bit_buf >= 0x00800000)
779     {
780     tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
781     i += tab->run;
782    
783     if (i < 64) goto normal_code;
784     }
785     else if (bit_buf >= 0x00200000)
786     {
787     tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
788     i += tab->run;
789    
790     if (i < 64) goto normal_code;
791     }
792     else
793     {
794     tab = DCT_16 + UBITS(bit_buf, 16);
795     bit_buf <<= 16;
796     GETWORD(&bit_buf, bits + 16);
797     i += tab->run;
798     goto normal_code;
799     }
800    
801     break; /* illegal, check needed to avoid buffer overflow */
802     }
803    
804     if ((bit_buf >> 30) != 0x2) ipuRegs->ctrl.ECD = 1;
805    
806     DUMPBITS(bit_buf, bits, 2); /* dump end of block code */
807     decoder->bitstream_buf = bit_buf;
808     decoder->bitstream_bits = bits;
809     }
810    
811     static __forceinline int get_mpeg1_non_intra_block(decoder_t * const decoder)
812     {
813     int i;
814     int j;
815     int val;
816     const u8 * scan = decoder->scan;
817     const u8 * quant_matrix = decoder->non_intra_quantizer_matrix;
818     int quantizer_scale = decoder->quantizer_scale;
819     const DCTtab * tab;
820     u32 bit_buf;
821     int bits;
822     u8 * bit_ptr;
823     s16 * dest;
824    
825     i = -1;
826     dest = decoder->DCTblock;
827    
828     bit_buf = decoder->bitstream_buf;
829     bits = decoder->bitstream_bits;
830     bit_ptr = decoder->bitstream_ptr;
831    
832     NEEDBITS(bit_buf, bits, bit_ptr);
833    
834     if (bit_buf >= 0x28000000)
835     {
836     tab = DCT_B14DC_5 + (UBITS(bit_buf, 5) - 5);
837     goto entry_1;
838     }
839     else
840     goto entry_2;
841    
842     while (1)
843     {
844     if (bit_buf >= 0x28000000)
845     {
846     tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
847     entry_1:
848     i += tab->run;
849    
850     if (i >= 64) break; /* end of block */
851    
852     normal_code:
853     j = scan[i];
854     bit_buf <<= tab->len;
855     bits += tab->len + 1;
856    
857     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
858     val = ((2 * tab->level + 1) * quantizer_scale * quant_matrix[i]) >> 5;
859    
860     /* oddification */
861     val = (val - 1) | 1;
862    
863     /* if (bitstream_get (1)) val = -val; */
864     val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
865    
866     SATURATE(val);
867     dest[j] = val;
868     bit_buf <<= 1;
869     NEEDBITS(bit_buf, bits, bit_ptr);
870     continue;
871     }
872     entry_2:
873     if (bit_buf >= 0x04000000)
874     {
875     tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
876     i += tab->run;
877    
878     if (i < 64) goto normal_code;
879    
880     /* escape code */
881    
882     i += UBITS(bit_buf << 6, 6) - 64;
883    
884     if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
885    
886     j = scan[i];
887     DUMPBITS(bit_buf, bits, 12);
888     NEEDBITS(bit_buf, bits, bit_ptr);
889     val = SBITS(bit_buf, 8);
890    
891     if (!(val & 0x7f))
892     {
893     DUMPBITS(bit_buf, bits, 8);
894     val = UBITS(bit_buf, 8) + 2 * val;
895     }
896    
897     val = 2 * (val + SBITS(val, 1)) + 1;
898    
899     /* JayteeMaster: 10 points! Replaced quant_matrix[j] by quant_matrix[i] as should be */
900     val = (val * quantizer_scale * quant_matrix[i]) / 32;
901    
902     /* oddification */
903     val = (val + ~SBITS(val, 1)) | 1;
904    
905     SATURATE(val);
906     dest[j] = val;
907     DUMPBITS(bit_buf, bits, 8);
908     NEEDBITS(bit_buf, bits, bit_ptr);
909     continue;
910     }
911     else if (bit_buf >= 0x02000000)
912     {
913     tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
914     i += tab->run;
915    
916     if (i < 64) goto normal_code;
917     }
918     else if (bit_buf >= 0x00800000)
919     {
920     tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
921     i += tab->run;
922    
923     if (i < 64) goto normal_code;
924     }
925     else if (bit_buf >= 0x00200000)
926     {
927     tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
928     i += tab->run;
929    
930     if (i < 64) goto normal_code;
931     }
932     else
933     {
934     tab = DCT_16 + UBITS(bit_buf, 16);
935     bit_buf <<= 16;
936     GETWORD(&bit_buf, bits + 16);
937     i += tab->run;
938    
939     if (i < 64) goto normal_code;
940     }
941    
942     break; /* illegal, check needed to avoid buffer overflow */
943     }
944    
945     if ((bit_buf >> 30) != 0x2) ipuRegs->ctrl.ECD = 1;
946    
947     DUMPBITS(bit_buf, bits, 2); /* dump end of block code */
948     decoder->bitstream_buf = bit_buf;
949     decoder->bitstream_bits = bits;
950     return i;
951     }
952    
953     static void __fastcall slice_intra_DCT(decoder_t * const decoder, const int cc,
954     u8 * const dest, const int stride)
955     {
956     NEEDBITS(decoder->bitstream_buf, decoder->bitstream_bits, decoder->bitstream_ptr);
957     /* Get the intra DC coefficient and inverse quantize it */
958    
959     if (cc == 0)
960     decoder->dc_dct_pred[0] += get_luma_dc_dct_diff(decoder);
961     else
962     decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff(decoder);
963    
964     decoder->DCTblock[0] = decoder->dc_dct_pred[cc] << (3 - decoder->intra_dc_precision);
965    
966     if (decoder->mpeg1)
967     {
968     get_mpeg1_intra_block(decoder);
969     }
970     else if (decoder->intra_vlc_format)
971     {
972     get_intra_block_B15(decoder);
973     }
974     else
975     {
976     get_intra_block_B14(decoder);
977     }
978    
979     mpeg2_idct_copy(decoder->DCTblock, dest, stride);
980     }
981    
982     /* JayteeMaster: changed dest to 16 bit signed */
983     static void __fastcall slice_non_intra_DCT(decoder_t * const decoder,
984     /*u8*/s16 * const dest, const int stride)
985     {
986     int last;
987     memzero(decoder->DCTblock);
988    
989     if (decoder->mpeg1)
990     last = get_mpeg1_non_intra_block(decoder);
991     else
992     last = get_non_intra_block(decoder);
993    
994     mpeg2_idct_add(last, decoder->DCTblock, dest, stride);
995     }
996    
997     #if defined(_MSC_VER)
998     #pragma pack(1)
999     #endif
1000    
1001     struct TGA_HEADER
1002     {
1003     u8 identsize; // size of ID field that follows 18 u8 header (0 usually)
1004     u8 colourmaptype; // type of colour map 0=none, 1=has palette
1005     u8 imagetype; // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
1006    
1007     s16 colourmapstart; // first colour map entry in palette
1008     s16 colourmaplength; // number of colours in palette
1009     u8 colourmapbits; // number of bits per palette entry 15,16,24,32
1010    
1011     s16 xstart; // image x origin
1012     s16 ystart; // image y origin
1013     s16 width; // image width in pixels
1014     s16 height; // image height in pixels
1015     u8 bits; // image bits per pixel 8,16,24,32
1016     u8 descriptor; // image descriptor bits (vh flip bits)
1017    
1018     // pixel data follows header
1019     } __packed;
1020    
1021     #if defined(_MSC_VER)
1022     # pragma pack()
1023     #endif
1024    
1025     void SaveTGA(const char* filename, int width, int height, void* pdata)
1026     {
1027     TGA_HEADER hdr;
1028     FILE* f = fopen(filename, "wb");
1029    
1030     if (f == NULL) return;
1031    
1032     assert(sizeof(TGA_HEADER) == 18 && sizeof(hdr) == 18);
1033    
1034     memzero(hdr);
1035     hdr.imagetype = 2;
1036     hdr.bits = 32;
1037     hdr.width = width;
1038     hdr.height = height;
1039     hdr.descriptor |= 8 | (1 << 5); // 8bit alpha, flip vertical
1040     fwrite(&hdr, sizeof(hdr), 1, f);
1041     fwrite(pdata, width*height*4, 1, f);
1042     fclose(f);
1043     }
1044    
1045     static int s_index = 0; //, s_frame = 0;
1046    
1047     void SaveRGB32(u8* ptr)
1048     {
1049     char filename[255];
1050     sprintf(filename, "frames/frame%.4d.tga", s_index++);
1051     SaveTGA(filename, 16, 16, ptr);
1052     }
1053    
1054     void waitForSCD()
1055     {
1056     u8 bit8 = 1;
1057    
1058     while (!getBits8((u8*)&bit8, 0))
1059     {
1060     so_resume();
1061     }
1062    
1063     if (bit8 == 0)
1064     {
1065     if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
1066    
1067     ipuRegs->ctrl.SCD = 1;
1068     }
1069    
1070     while (!getBits32((u8*)&ipuRegs->top, 0))
1071     {
1072     so_resume();
1073     }
1074    
1075     BigEndian(ipuRegs->top, ipuRegs->top);
1076    
1077     /*if(ipuRegs->ctrl.SCD)
1078     {
1079     switch(ipuRegs->top & 0xFFFFFFF0)
1080     {
1081     case 0x100:
1082     case 0x1A0:
1083     break;
1084     case 0x1B0:
1085     ipuRegs->ctrl.SCD = 0;
1086     if(ipuRegs->top == 0x1b4) ipuRegs->ctrl.ECD = 1;
1087     //else
1088     //{
1089     // do
1090     // {
1091     // while(!getBits32((u8*)&ipuRegs->top, 1))
1092     // {
1093     // so_resume();
1094     // }
1095    
1096     // BigEndian(ipuRegs->top, ipuRegs->top);
1097     // }
1098     // while((ipuRegs->top & 0xfffffff0) != 0x100);
1099     //}
1100     break;
1101     default:
1102     ipuRegs->ctrl.SCD = 0;
1103     break;
1104     }
1105     }*/
1106     }
1107    
1108     void __forceinline finishmpeg2sliceIDEC(decoder_t* &decoder)
1109     {
1110     ipuRegs->ctrl.SCD = 0;
1111     coded_block_pattern = decoder->coded_block_pattern;
1112    
1113     g_BP.BP += decoder->bitstream_bits - 16;
1114    
1115     if ((int)g_BP.BP < 0)
1116     {
1117     g_BP.BP = 128 + (int)g_BP.BP;
1118    
1119     // After BP is positioned correctly, we need to reload the old buffer
1120     // so that reading may continue properly
1121     ReorderBitstream();
1122     }
1123    
1124     FillInternalBuffer(&g_BP.BP, 1, 0);
1125    
1126     waitForSCD();
1127     }
1128    
1129     void mpeg2sliceIDEC(void* pdone)
1130     {
1131     u32 read;
1132    
1133     bool resumed = false;
1134     decoder_t *decoder = &g_decoder;
1135    
1136     *(int*)pdone = 0;
1137     bitstream_init(decoder);
1138    
1139     decoder->dc_dct_pred[0] =
1140     decoder->dc_dct_pred[1] =
1141     decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1142    
1143     decoder->mbc = 0;
1144     ipuRegs->ctrl.ECD = 0;
1145    
1146     if (UBITS(decoder->bitstream_buf, 2) == 0)
1147     {
1148     ipuRegs->ctrl.SCD = 0;
1149     }
1150     else
1151     {
1152     while (1)
1153     {
1154     int DCT_offset, DCT_stride;
1155     int mba_inc;
1156     const MBAtab * mba;
1157    
1158     NEEDBITS(decoder->bitstream_buf, decoder->bitstream_bits, decoder->bitstream_ptr);
1159     decoder->macroblock_modes = get_macroblock_modes(decoder);
1160    
1161     /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1162    
1163     if (decoder->macroblock_modes & MACROBLOCK_QUANT) //only IDEC
1164     {
1165     decoder->quantizer_scale = get_quantizer_scale(decoder);
1166     }
1167    
1168     if (decoder->macroblock_modes & DCT_TYPE_INTERLACED)
1169     {
1170     DCT_offset = decoder->stride;
1171     DCT_stride = decoder->stride * 2;
1172     }
1173     else
1174     {
1175     DCT_offset = decoder->stride * 8;
1176     DCT_stride = decoder->stride;
1177     }
1178    
1179     if (decoder->macroblock_modes & MACROBLOCK_INTRA)
1180     {
1181     decoder->coded_block_pattern = 0x3F;//all 6 blocks
1182     //ipuRegs->ctrl.CBP = 0x3f;
1183    
1184     memzero(*decoder->mb8);
1185     memzero(*decoder->rgb32);
1186    
1187     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y, DCT_stride);
1188     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y + 8, DCT_stride);
1189     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y + DCT_offset, DCT_stride);
1190     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y + DCT_offset + 8, DCT_stride);
1191     slice_intra_DCT(decoder, 1, (u8*)decoder->mb8->Cb, decoder->stride >> 1);
1192     slice_intra_DCT(decoder, 2, (u8*)decoder->mb8->Cr, decoder->stride >> 1);
1193    
1194     // Send The MacroBlock via DmaIpuFrom
1195    
1196     if (decoder->ofm == 0)
1197     {
1198     ipu_csc(decoder->mb8, decoder->rgb32, decoder->sgn);
1199    
1200     g_nIPU0Data = 64;
1201     g_pIPU0Pointer = (u8*)decoder->rgb32;
1202     //if ( s_frame >= 39 ) SaveRGB32(g_pIPU0Pointer);
1203     }
1204     else
1205     {
1206     ipu_csc(decoder->mb8, decoder->rgb32, decoder->sgn);
1207     ipu_dither(decoder->rgb32, decoder->rgb16, decoder->dte);
1208    
1209     g_nIPU0Data = 32;
1210     g_pIPU0Pointer = (u8*)decoder->rgb16;
1211     //if ( s_frame >= 39 ) SaveRGB32(g_pIPU0Pointer);
1212     }
1213    
1214     while (g_nIPU0Data > 0)
1215     {
1216     read = ipu_fifo.out.write((u32*)g_pIPU0Pointer, g_nIPU0Data);
1217    
1218     if (read == 0)
1219     {
1220     so_resume();
1221     resumed = true;
1222     }
1223     else
1224     {
1225     g_pIPU0Pointer += read * 16;
1226     g_nIPU0Data -= read;
1227    
1228     }
1229     }
1230    
1231     decoder->mbc++;
1232     }
1233    
1234     NEEDBITS(decoder->bitstream_buf, decoder->bitstream_bits, decoder->bitstream_ptr);
1235     mba_inc = 0;
1236    
1237     while (1)
1238     {
1239     if (decoder->bitstream_buf >= 0x10000000)
1240     {
1241     mba = MBA_5 + (UBITS(decoder->bitstream_buf, 5) - 2);
1242     break;
1243     }
1244     else if (decoder->bitstream_buf >= 0x03000000)
1245     {
1246     mba = MBA_11 + (UBITS(decoder->bitstream_buf, 11) - 24);
1247     break;
1248     }
1249     else switch (UBITS(decoder->bitstream_buf, 11))
1250     {
1251    
1252     case 8: /* macroblock_escape */
1253     mba_inc += 33;
1254     /* pass through */
1255    
1256     case 15: /* macroblock_stuffing (MPEG1 only) */
1257     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, 11);
1258     NEEDBITS(decoder->bitstream_buf, decoder->bitstream_bits, decoder->bitstream_ptr);
1259     continue;
1260    
1261     default: /* end of slice/frame, or error? */
1262     {
1263     #ifdef MPEGHACK
1264     if (!resumed) so_resume();
1265     #endif
1266     finishmpeg2sliceIDEC(decoder);
1267    
1268     *(int*)pdone = 1;
1269     so_exit();
1270     }
1271     }
1272     }
1273    
1274     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, mba->len);
1275     mba_inc += mba->mba;
1276    
1277     if (mba_inc)
1278     {
1279     decoder->dc_dct_pred[0] =
1280     decoder->dc_dct_pred[1] =
1281     decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1282    
1283     do
1284     {
1285     decoder->mbc++;
1286     }
1287     while (--mba_inc);
1288     }
1289     }
1290     }
1291    
1292     #ifdef MPEGHACK
1293     if (!resumed) so_resume();
1294     #endif
1295    
1296     finishmpeg2sliceIDEC(decoder);
1297    
1298     *(int*)pdone = 1;
1299     so_exit();
1300     }
1301    
1302     void mpeg2_slice(void* pdone)
1303     {
1304     int DCT_offset, DCT_stride;
1305     //u8 bit8=0;
1306     //u32 fp = g_BP.FP;
1307     u32 bp;
1308     decoder_t * decoder = &g_decoder;
1309     u32 size = 0;
1310    
1311     *(int*)pdone = 0;
1312     ipuRegs->ctrl.ECD = 0;
1313    
1314     memzero(*decoder->mb8);
1315     memzero(*decoder->mb16);
1316    
1317     bitstream_init(decoder);
1318    
1319     if (decoder->dcr)
1320     {
1321     decoder->dc_dct_pred[0] =
1322     decoder->dc_dct_pred[1] =
1323     decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1324     }
1325    
1326     if (decoder->macroblock_modes & DCT_TYPE_INTERLACED)
1327     {
1328     DCT_offset = decoder->stride;
1329     DCT_stride = decoder->stride * 2;
1330     }
1331     else
1332     {
1333     DCT_offset = decoder->stride * 8;
1334     DCT_stride = decoder->stride;
1335     }
1336    
1337     if (decoder->macroblock_modes & MACROBLOCK_INTRA)
1338     {
1339     decoder->coded_block_pattern = 0x3F;//all 6 blocks
1340     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y, DCT_stride);
1341     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y + 8, DCT_stride);
1342     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y + DCT_offset, DCT_stride);
1343     slice_intra_DCT(decoder, 0, (u8*)decoder->mb8->Y + DCT_offset + 8, DCT_stride);
1344     slice_intra_DCT(decoder, 1, (u8*)decoder->mb8->Cb, decoder->stride >> 1);
1345     slice_intra_DCT(decoder, 2, (u8*)decoder->mb8->Cr, decoder->stride >> 1);
1346     ipu_copy(decoder->mb8, decoder->mb16);
1347     }
1348     else
1349     {
1350     if (decoder->macroblock_modes & MACROBLOCK_PATTERN)
1351     {
1352     decoder->coded_block_pattern = get_coded_block_pattern(decoder);
1353     /* JayteeMaster: changed from mb8 to mb16 and from u8 to s16 */
1354    
1355     if (decoder->coded_block_pattern & 0x20) slice_non_intra_DCT(decoder, (s16*)decoder->mb16->Y, DCT_stride);
1356     if (decoder->coded_block_pattern & 0x10) slice_non_intra_DCT(decoder, (s16*)decoder->mb16->Y + 8, DCT_stride);
1357     if (decoder->coded_block_pattern & 0x08) slice_non_intra_DCT(decoder, (s16*)decoder->mb16->Y + DCT_offset, DCT_stride);
1358     if (decoder->coded_block_pattern & 0x04) slice_non_intra_DCT(decoder, (s16*)decoder->mb16->Y + DCT_offset + 8, DCT_stride);
1359     if (decoder->coded_block_pattern & 0x2) slice_non_intra_DCT(decoder, (s16*)decoder->mb16->Cb, decoder->stride >> 1);
1360     if (decoder->coded_block_pattern & 0x1) slice_non_intra_DCT(decoder, (s16*)decoder->mb16->Cr, decoder->stride >> 1);
1361    
1362     }
1363     }
1364    
1365     //Send The MacroBlock via DmaIpuFrom
1366    
1367     size = 0; // Reset
1368     ipuRegs->ctrl.SCD = 0;
1369     coded_block_pattern = decoder->coded_block_pattern;
1370     bp = g_BP.BP;
1371     g_BP.BP += ((int)decoder->bitstream_bits - 16);
1372    
1373     // BP goes from 0 to 128, so negative values mean to read old buffer
1374     // so we minus from 128 to get the correct BP
1375     if ((int)g_BP.BP < 0)
1376     {
1377     g_BP.BP = 128 + (int)g_BP.BP;
1378    
1379     // After BP is positioned correctly, we need to reload the old buffer
1380     // so that reading may continue properly
1381     ReorderBitstream();
1382     }
1383    
1384     FillInternalBuffer(&g_BP.BP, 1, 0);
1385    
1386     decoder->mbc = 1;
1387     g_nIPU0Data = 48;
1388     g_pIPU0Pointer = (u8*)decoder->mb16;
1389    
1390     while (g_nIPU0Data > 0)
1391     {
1392     size = ipu_fifo.out.write((u32*)g_pIPU0Pointer, g_nIPU0Data);
1393    
1394     if (size == 0)
1395     {
1396     so_resume();
1397     }
1398     else
1399     {
1400     g_pIPU0Pointer += size * 16;
1401     g_nIPU0Data -= size;
1402     }
1403     }
1404     waitForSCD();
1405    
1406     decoder->bitstream_bits = 0;
1407     *(int*)pdone = 1;
1408     so_exit();
1409     }
1410    
1411     int __forceinline get_motion_delta(decoder_t * const decoder,
1412     const int f_code)
1413     {
1414     #define bit_buf (decoder->bitstream_buf)
1415     #define bits (decoder->bitstream_bits)
1416     #define bit_ptr (decoder->bitstream_ptr)
1417    
1418     int delta;
1419     int sign;
1420     const MVtab * tab;
1421    
1422     if ((bit_buf & 0x80000000))
1423     {
1424     DUMPBITS(bit_buf, bits, 1);
1425     return 0x00010000;
1426     }
1427     else if ((bit_buf & 0xf0000000) || ((bit_buf & 0xfc000000) == 0x0c000000))
1428     {
1429    
1430     tab = MV_4 + UBITS(bit_buf, 4);
1431     delta = (tab->delta << f_code) + 1;
1432     bits += tab->len + f_code + 1;
1433     bit_buf <<= tab->len;
1434    
1435     sign = SBITS(bit_buf, 1);
1436     bit_buf <<= 1;
1437    
1438     if (f_code) delta += UBITS(bit_buf, f_code);
1439    
1440     bit_buf <<= f_code;
1441    
1442     return (delta ^ sign) - sign;
1443    
1444     }
1445     else
1446     {
1447     tab = MV_10 + UBITS(bit_buf, 10);
1448     delta = (tab->delta << f_code) + 1;
1449     bits += tab->len + 1;
1450     bit_buf <<= tab->len;
1451    
1452     sign = SBITS(bit_buf, 1);
1453     bit_buf <<= 1;
1454    
1455     if (f_code)
1456     {
1457     NEEDBITS(bit_buf, bits, bit_ptr);
1458     delta += UBITS(bit_buf, f_code);
1459     DUMPBITS(bit_buf, bits, f_code);
1460     }
1461    
1462     return (delta ^ sign) - sign;
1463    
1464     }
1465    
1466     #undef bit_buf
1467     #undef bits
1468     #undef bit_ptr
1469     }
1470    
1471     int __forceinline get_dmv(decoder_t * const decoder)
1472     {
1473     #define bit_buf (decoder->bitstream_buf)
1474     #define bits (decoder->bitstream_bits)
1475     #define bit_ptr (decoder->bitstream_ptr)
1476    
1477     const DMVtab * tab;
1478    
1479     tab = DMV_2 + UBITS(bit_buf, 2);
1480     DUMPBITS(bit_buf, bits, tab->len);
1481     return tab->dmv;
1482     #undef bit_buf
1483     #undef bits
1484     #undef bit_ptr
1485     }
1486    
1487     int get_macroblock_address_increment(decoder_t * const decoder)
1488     {
1489     const MBAtab *mba;
1490    
1491     if (decoder->bitstream_buf >= 0x10000000)
1492     mba = MBA_5 + (UBITS(decoder->bitstream_buf, 5) - 2);
1493     else if (decoder->bitstream_buf >= 0x03000000)
1494     mba = MBA_11 + (UBITS(decoder->bitstream_buf, 11) - 24);
1495     else switch (UBITS(decoder->bitstream_buf, 11))
1496     {
1497    
1498     case 8: /* macroblock_escape */
1499     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, 11);
1500     return 0x23;
1501    
1502     case 15: /* macroblock_stuffing (MPEG1 only) */
1503     if (decoder->mpeg1)
1504     {
1505     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, 11);
1506     return 0x22;
1507     }
1508    
1509     default:
1510     return 0;//error
1511     }
1512    
1513     DUMPBITS(decoder->bitstream_buf, decoder->bitstream_bits, mba->len);
1514    
1515     return mba->mba + 1;
1516     }

  ViewVC Help
Powered by ViewVC 1.1.22