/[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 62 - (hide annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (10 years ago) by william
File size: 22469 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
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 william 62 const int non_linear_quantizer_scale [] =
37 william 31 {
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 william 62 const DCTtab * tab;
52     int mbaCount = 0;
53 william 31
54 william 62 int get_macroblock_modes()
55 william 31 {
56     int macroblock_modes;
57     const MBtab * tab;
58    
59 william 62 switch (decoder.coding_type)
60 william 31 {
61     case I_TYPE:
62 william 62 macroblock_modes = UBITS(2);
63 william 31
64     if (macroblock_modes == 0) return 0; // error
65    
66     tab = MB_I + (macroblock_modes >> 1);
67 william 62 DUMPBITS(tab->len);
68 william 31 macroblock_modes = tab->modes;
69    
70 william 62 if ((!(decoder.frame_pred_frame_dct)) &&
71     (decoder.picture_structure == FRAME_PICTURE))
72 william 31 {
73 william 62 macroblock_modes |= GETBITS(1) * DCT_TYPE_INTERLACED;
74 william 31 }
75     return macroblock_modes;
76    
77     case P_TYPE:
78 william 62 macroblock_modes = UBITS(6);
79 william 31
80     if (macroblock_modes == 0) return 0; // error
81    
82     tab = MB_P + (macroblock_modes >> 1);
83 william 62 DUMPBITS(tab->len);
84 william 31 macroblock_modes = tab->modes;
85    
86 william 62 if (decoder.picture_structure != FRAME_PICTURE)
87 william 31 {
88     if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
89     {
90 william 62 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
91 william 31 }
92    
93     return macroblock_modes;
94     }
95 william 62 else if (decoder.frame_pred_frame_dct)
96 william 31 {
97     if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
98     macroblock_modes |= MC_FRAME;
99    
100     return macroblock_modes;
101     }
102     else
103     {
104     if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
105     {
106 william 62 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
107 william 31 }
108    
109     if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
110     {
111 william 62 macroblock_modes |= GETBITS(1) * DCT_TYPE_INTERLACED;
112 william 31 }
113    
114     return macroblock_modes;
115     }
116    
117     case B_TYPE:
118 william 62 macroblock_modes = UBITS(6);
119 william 31
120     if (macroblock_modes == 0) return 0; // error
121    
122     tab = MB_B + macroblock_modes;
123 william 62 DUMPBITS(tab->len);
124 william 31 macroblock_modes = tab->modes;
125    
126 william 62 if (decoder.picture_structure != FRAME_PICTURE)
127 william 31 {
128     if (!(macroblock_modes & MACROBLOCK_INTRA))
129     {
130 william 62 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
131 william 31 }
132    
133     return macroblock_modes;
134     }
135 william 62 else if (decoder.frame_pred_frame_dct)
136 william 31 {
137     /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
138     macroblock_modes |= MC_FRAME;
139     return macroblock_modes;
140     }
141     else
142     {
143     if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
144    
145 william 62 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
146 william 31
147     if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
148     {
149     intra:
150 william 62 macroblock_modes |= GETBITS(1) * DCT_TYPE_INTERLACED;
151 william 31 }
152    
153     return macroblock_modes;
154     }
155    
156     case D_TYPE:
157 william 62 macroblock_modes = GETBITS(1);
158 william 31
159     if (macroblock_modes == 0) return 0; // error
160     return MACROBLOCK_INTRA;
161    
162     default:
163     return 0;
164     }
165     }
166    
167 william 62 static __fi int get_quantizer_scale()
168 william 31 {
169     int quantizer_scale_code;
170    
171 william 62 quantizer_scale_code = GETBITS(5);
172 william 31
173 william 62 if (decoder.q_scale_type)
174 william 31 return non_linear_quantizer_scale [quantizer_scale_code];
175     else
176     return quantizer_scale_code << 1;
177     }
178    
179 william 62 static __fi int get_coded_block_pattern()
180 william 31 {
181     const CBPtab * tab;
182 william 62 u16 code = UBITS(16);
183 william 31
184 william 62 if (code >= 0x2000)
185     tab = CBP_7 + (UBITS(7) - 16);
186 william 31 else
187 william 62 tab = CBP_9 + UBITS(9);
188 william 31
189 william 62 DUMPBITS(tab->len);
190 william 31 return tab->cbp;
191     }
192    
193 william 62 int __fi get_motion_delta(const int f_code)
194 william 31 {
195 william 62 int delta;
196     int sign;
197     const MVtab * tab;
198     u16 code = UBITS(16);
199 william 31
200 william 62 if ((code & 0x8000))
201 william 31 {
202 william 62 DUMPBITS(1);
203     return 0x00010000;
204 william 31 }
205 william 62 else if ((code & 0xf000) || ((code & 0xfc00) == 0x0c00))
206     {
207     tab = MV_4 + UBITS(4);
208     }
209     else
210     {
211     tab = MV_10 + UBITS(10);
212     }
213 william 31
214 william 62 delta = tab->delta + 1;
215     DUMPBITS(tab->len);
216 william 31
217 william 62 sign = SBITS(1);
218     DUMPBITS(1);
219     return (delta ^ sign) - sign;
220 william 31 }
221    
222 william 62 int __fi get_dmv()
223 william 31 {
224 william 62 const DMVtab * tab;
225 william 31
226 william 62 tab = DMV_2 + UBITS(2);
227     DUMPBITS(tab->len);
228     return tab->dmv;
229 william 31 }
230    
231 william 62 int get_macroblock_address_increment()
232 william 31 {
233 william 62 const MBAtab *mba;
234    
235     u16 code = UBITS(16);
236 william 31
237 william 62 if (code >= 4096)
238     mba = MBA.mba5 + (UBITS(5) - 2);
239     else if (code >= 768)
240     mba = MBA.mba11 + (UBITS(11) - 24);
241     else switch (UBITS(11))
242 william 31 {
243    
244 william 62 case 8: /* macroblock_escape */
245     DUMPBITS(11);
246     return 0x23;
247 william 31
248 william 62 case 15: /* macroblock_stuffing (MPEG1 only) */
249     if (decoder.mpeg1)
250     {
251     DUMPBITS(11);
252     return 0x22;
253     }
254 william 31
255 william 62 default:
256     return 0;//error
257 william 31 }
258    
259 william 62 DUMPBITS(mba->len);
260 william 31
261 william 62 return mba->mba + 1;
262     }
263 william 31
264 william 62 static __fi int get_luma_dc_dct_diff()
265     {
266     int size;
267     int dc_diff;
268     u16 code = UBITS(5);
269 william 31
270 william 62 if (code < 31)
271     {
272     size = DCtable.lum0[code].size;
273     DUMPBITS(DCtable.lum0[code].len);
274 william 31
275 william 62 // 5 bits max
276     }
277     else
278     {
279     code = UBITS(9) - 0x1f0;
280     size = DCtable.lum1[code].size;
281     DUMPBITS(DCtable.lum1[code].len);
282 william 31
283 william 62 // 9 bits max
284     }
285    
286     if (size==0)
287     dc_diff = 0;
288     else
289     {
290     dc_diff = GETBITS(size);
291 william 31
292 william 62 // 6 for tab0 and 11 for tab1
293     if ((dc_diff & (1<<(size-1)))==0)
294     dc_diff-= (1<<size) - 1;
295     }
296 william 31
297 william 62 return dc_diff;
298     }
299 william 31
300 william 62 static __fi int get_chroma_dc_dct_diff()
301     {
302     int size;
303     int dc_diff;
304     u16 code = UBITS(5);
305 william 31
306 william 62 if (code<31)
307     {
308     size = DCtable.chrom0[code].size;
309     DUMPBITS(DCtable.chrom0[code].len);
310     }
311     else
312     {
313     code = UBITS(10) - 0x3e0;
314     size = DCtable.chrom1[code].size;
315     DUMPBITS(DCtable.chrom1[code].len);
316     }
317    
318     if (size==0)
319     dc_diff = 0;
320     else
321     {
322     dc_diff = GETBITS(size);
323 william 31
324 william 62 if ((dc_diff & (1<<(size-1)))==0)
325 william 31 {
326 william 62 dc_diff-= (1<<size) - 1;
327 william 31 }
328     }
329 william 62
330     return dc_diff;
331     }
332 william 31
333 william 62 #define SATURATE(val) \
334     do { \
335     if (((u32)(val + 2048) > 4095)) \
336     val = (((s32)val) >> 31) ^ 2047; \
337     } while (0)
338 william 31
339 william 62 static __fi bool get_intra_block()
340 william 31 {
341     int i;
342     int j;
343     int val;
344 william 62 const u8 * scan = decoder.scantype ? mpeg2_scan.alt : mpeg2_scan.norm;
345     const u8 (&quant_matrix)[64] = decoder.iq;
346     int quantizer_scale = decoder.quantizer_scale;
347     s16 * dest = decoder.DCTblock;
348     u16 code;
349 william 31
350 william 62 /* decode AC coefficients */
351     for (i=1 + ipu_cmd.pos[4]; ; i++)
352     {
353     switch (ipu_cmd.pos[5])
354     {
355     case 0:
356     if (!GETWORD())
357     {
358     ipu_cmd.pos[4] = i - 1;
359     return false;
360     }
361 william 31
362 william 62 code = UBITS(16);
363 william 31
364 william 62 if (code >= 16384 && (!decoder.intra_vlc_format || decoder.mpeg1))
365 william 31 {
366 william 62 tab = &DCT.next[(code >> 12) - 4];
367     }
368     else if (code >= 1024)
369     {
370     if (decoder.intra_vlc_format && !decoder.mpeg1)
371 william 31 {
372 william 62 tab = &DCT.tab0a[(code >> 8) - 4];
373 william 31 }
374     else
375     {
376 william 62 tab = &DCT.tab0[(code >> 8) - 4];
377 william 31 }
378     }
379 william 62 else if (code >= 512)
380 william 31 {
381 william 62 if (decoder.intra_vlc_format && !decoder.mpeg1)
382     {
383     tab = &DCT.tab1a[(code >> 6) - 8];
384     }
385     else
386     {
387     tab = &DCT.tab1[(code >> 6) - 8];
388     }
389 william 31 }
390    
391 william 62 // [TODO] Optimization: Following codes can all be done by a single "expedited" lookup
392     // that should use a single unrolled DCT table instead of five separate tables used
393     // here. Multiple conditional statements are very slow, while modern CPU data caches
394     // have lots of room to spare.
395    
396     else if (code >= 256)
397 william 31 {
398 william 62 tab = &DCT.tab2[(code >> 4) - 16];
399 william 31 }
400 william 62 else if (code >= 128)
401     {
402     tab = &DCT.tab3[(code >> 3) - 16];
403     }
404     else if (code >= 64)
405     {
406     tab = &DCT.tab4[(code >> 2) - 16];
407     }
408     else if (code >= 32)
409     {
410     tab = &DCT.tab5[(code >> 1) - 16];
411     }
412     else if (code >= 16)
413     {
414     tab = &DCT.tab6[code - 16];
415     }
416 william 31 else
417     {
418 william 62 ipu_cmd.pos[4] = 0;
419     return true;
420 william 31 }
421    
422 william 62 DUMPBITS(tab->len);
423 william 31
424 william 62 if (tab->run==64) /* end_of_block */
425 william 31 {
426 william 62 ipu_cmd.pos[4] = 0;
427     return true;
428 william 31 }
429 william 62
430     i+= tab->run == 65 ? GETBITS(6) : tab->run;
431     if (i >= 64)
432 william 31 {
433 william 62 ipu_cmd.pos[4] = 0;
434     return true;
435 william 31 }
436 william 62 case 1:
437     if (!GETWORD())
438 william 31 {
439 william 62 ipu_cmd.pos[4] = i - 1;
440     ipu_cmd.pos[5] = 1;
441     return false;
442 william 31 }
443    
444 william 62 j = scan[i];
445    
446     if (tab->run==65) /* escape */
447 william 31 {
448 william 62 if(!decoder.mpeg1)
449     {
450     val = (SBITS(12) * quantizer_scale * quant_matrix[i]) >> 4;
451     DUMPBITS(12);
452     }
453     else
454     {
455     val = SBITS(8);
456     DUMPBITS(8);
457 william 31
458 william 62 if (!(val & 0x7f))
459     {
460     val = GETBITS(8) + 2 * val;
461     }
462    
463     val = (val * quantizer_scale * quant_matrix[i]) >> 4;
464     val = (val + ~ (((s32)val) >> 31)) | 1;
465     }
466 william 31 }
467     else
468     {
469 william 62 val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
470     if(decoder.mpeg1)
471     {
472     /* oddification */
473     val = (val - 1) | 1;
474     }
475 william 31
476 william 62 /* if (bitstream_get (1)) val = -val; */
477     val = (val ^ SBITS(1)) - SBITS(1);
478     DUMPBITS(1);
479 william 31 }
480    
481 william 62 SATURATE(val);
482     dest[j] = val;
483     ipu_cmd.pos[5] = 0;
484     }
485     }
486 william 31
487 william 62 ipu_cmd.pos[4] = 0;
488     return true;
489 william 31 }
490    
491 william 62 static __fi bool get_non_intra_block(int * last)
492 william 31 {
493     int i;
494     int j;
495     int val;
496 william 62 const u8 * scan = decoder.scantype ? mpeg2_scan.alt : mpeg2_scan.norm;
497     const u8 (&quant_matrix)[64] = decoder.niq;
498     int quantizer_scale = decoder.quantizer_scale;
499     s16 * dest = decoder.DCTblock;
500     u16 code;
501 william 31
502 william 62 /* decode AC coefficients */
503     for (i= ipu_cmd.pos[4] ; ; i++)
504     {
505     switch (ipu_cmd.pos[5])
506 william 31 {
507 william 62 case 0:
508     if (!GETWORD())
509     {
510     ipu_cmd.pos[4] = i;
511     return false;
512     }
513 william 31
514 william 62 code = UBITS(16);
515 william 31
516 william 62 if (code >= 16384)
517     {
518     if (i==0)
519     {
520     tab = &DCT.first[(code >> 12) - 4];
521     }
522     else
523     {
524     tab = &DCT.next[(code >> 12)- 4];
525     }
526     }
527     else if (code >= 1024)
528     {
529     tab = &DCT.tab0[(code >> 8) - 4];
530     }
531     else if (code >= 512)
532     {
533     tab = &DCT.tab1[(code >> 6) - 8];
534     }
535 william 31
536 william 62 // [TODO] Optimization: Following codes can all be done by a single "expedited" lookup
537     // that should use a single unrolled DCT table instead of five separate tables used
538     // here. Multiple conditional statements are very slow, while modern CPU data caches
539     // have lots of room to spare.
540 william 31
541 william 62 else if (code >= 256)
542     {
543     tab = &DCT.tab2[(code >> 4) - 16];
544     }
545     else if (code >= 128)
546     {
547     tab = &DCT.tab3[(code >> 3) - 16];
548     }
549     else if (code >= 64)
550     {
551     tab = &DCT.tab4[(code >> 2) - 16];
552     }
553     else if (code >= 32)
554     {
555     tab = &DCT.tab5[(code >> 1) - 16];
556     }
557     else if (code >= 16)
558     {
559     tab = &DCT.tab6[code - 16];
560     }
561     else
562     {
563     ipu_cmd.pos[4] = 0;
564     return true;
565     }
566 william 31
567 william 62 DUMPBITS(tab->len);
568 william 31
569 william 62 if (tab->run==64) /* end_of_block */
570     {
571     *last = i;
572     ipu_cmd.pos[4] = 0;
573     return true;
574     }
575 william 31
576 william 62 i += (tab->run == 65) ? GETBITS(6) : tab->run;
577     if (i >= 64)
578     {
579     *last = i;
580     ipu_cmd.pos[4] = 0;
581     return true;
582     }
583 william 31
584 william 62 case 1:
585     if (!GETWORD())
586     {
587     ipu_cmd.pos[4] = i;
588     ipu_cmd.pos[5] = 1;
589     return false;
590     }
591 william 31
592 william 62 j = scan[i];
593 william 31
594 william 62 if (tab->run==65) /* escape */
595     {
596     if (!decoder.mpeg1)
597     {
598     val = ((2 * (SBITS(12) + SBITS(1)) + 1) * quantizer_scale * quant_matrix[i]) >> 5;
599     DUMPBITS(12);
600     }
601     else
602     {
603     val = SBITS(8);
604     DUMPBITS(8);
605 william 31
606 william 62 if (!(val & 0x7f))
607     {
608     val = GETBITS(8) + 2 * val;
609     }
610 william 31
611 william 62 val = ((2 * (val + (((s32)val) >> 31)) + 1) * quantizer_scale * quant_matrix[i]) / 32;
612     val = (val + ~ (((s32)val) >> 31)) | 1;
613     }
614     }
615     else
616 william 31 {
617 william 62 val = ((2 * tab->level + 1) * quantizer_scale * quant_matrix[i]) >> 5;
618     val = (val ^ SBITS(1)) - SBITS(1);
619     DUMPBITS(1);
620 william 31 }
621    
622     SATURATE(val);
623     dest[j] = val;
624 william 62 ipu_cmd.pos[5] = 0;
625 william 31 }
626     }
627    
628 william 62 ipu_cmd.pos[4] = 0;
629     return true;
630 william 31 }
631    
632 william 62 static __fi bool slice_intra_DCT(const int cc, u8 * const dest, const int stride, const bool skip)
633 william 31 {
634 william 62 if (!skip || ipu_cmd.pos[3])
635 william 31 {
636 william 62 ipu_cmd.pos[3] = 0;
637     if (!GETWORD())
638 william 31 {
639 william 62 ipu_cmd.pos[3] = 1;
640     return false;
641 william 31 }
642    
643 william 62 /* Get the intra DC coefficient and inverse quantize it */
644     if (cc == 0)
645     decoder.dc_dct_pred[0] += get_luma_dc_dct_diff();
646 william 31 else
647 william 62 decoder.dc_dct_pred[cc] += get_chroma_dc_dct_diff();
648 william 31
649 william 62 decoder.DCTblock[0] = decoder.dc_dct_pred[cc] << (3 - decoder.intra_dc_precision);
650 william 31 }
651    
652 william 62 if (!get_intra_block())
653 william 31 {
654 william 62 return false;
655 william 31 }
656    
657 william 62 mpeg2_idct_copy(decoder.DCTblock, dest, stride);
658    
659     return true;
660 william 31 }
661    
662 william 62 static __fi bool slice_non_intra_DCT(s16 * const dest, const int stride, const bool skip)
663 william 31 {
664     int last;
665    
666 william 62 if (!skip)
667 william 31 {
668 william 62 memzero_sse_a(decoder.DCTblock);
669 william 31 }
670    
671 william 62 if (!get_non_intra_block(&last))
672 william 31 {
673 william 62 return false;
674 william 31 }
675    
676 william 62 mpeg2_idct_add(last, decoder.DCTblock, dest, stride);
677 william 31
678 william 62 return true;
679 william 31 }
680    
681 william 62 void __fi finishmpeg2sliceIDEC()
682 william 31 {
683 william 62 ipuRegs.ctrl.SCD = 0;
684     coded_block_pattern = decoder.coded_block_pattern;
685 william 31
686 william 62 g_BP.BP += decoder.bitstream_bits - 16;
687 william 31
688     if ((int)g_BP.BP < 0)
689     {
690     g_BP.BP = 128 + (int)g_BP.BP;
691    
692     // After BP is positioned correctly, we need to reload the old buffer
693     // so that reading may continue properly
694     ReorderBitstream();
695     }
696    
697     FillInternalBuffer(&g_BP.BP, 1, 0);
698     }
699    
700 william 62 bool mpeg2sliceIDEC()
701 william 31 {
702 william 62 u16 code;
703     u8 bit8;
704 william 31
705 william 62 switch (ipu_cmd.pos[0])
706     {
707     case 0:
708     decoder.dc_dct_pred[0] =
709     decoder.dc_dct_pred[1] =
710     decoder.dc_dct_pred[2] = 128 << decoder.intra_dc_precision;
711 william 31
712 william 62 decoder.mbc = 0;
713     ipuRegs.top = 0;
714     ipuRegs.ctrl.ECD = 0;
715 william 31
716 william 62 case 1:
717     ipu_cmd.pos[0] = 1;
718     if (!bitstream_init())
719     {
720     return false;
721     }
722 william 31
723 william 62 case 2:
724     ipu_cmd.pos[0] = 2;
725 william 31 while (1)
726     {
727 william 62 macroblock_8& mb8 = decoder.mb8;
728     macroblock_rgb16& rgb16 = decoder.rgb16;
729     macroblock_rgb32& rgb32 = decoder.rgb32;
730    
731 william 31 int DCT_offset, DCT_stride;
732     const MBAtab * mba;
733    
734 william 62 switch (ipu_cmd.pos[1])
735 william 31 {
736 william 62 case 0:
737     decoder.macroblock_modes = get_macroblock_modes();
738 william 31
739 william 62 if (decoder.macroblock_modes & MACROBLOCK_QUANT) //only IDEC
740     {
741     decoder.quantizer_scale = get_quantizer_scale();
742     }
743 william 31
744 william 62 decoder.coded_block_pattern = 0x3F;//all 6 blocks
745     memzero_sse_a(mb8);
746     memzero_sse_a(rgb32);
747 william 31
748 william 62 case 1:
749     ipu_cmd.pos[1] = 1;
750 william 31
751 william 62 if (decoder.macroblock_modes & DCT_TYPE_INTERLACED)
752 william 31 {
753 william 62 DCT_offset = decoder_stride;
754     DCT_stride = decoder_stride * 2;
755 william 31 }
756     else
757     {
758 william 62 DCT_offset = decoder_stride * 8;
759     DCT_stride = decoder_stride;
760     }
761 william 31
762 william 62 switch (ipu_cmd.pos[2])
763     {
764     case 0:
765     case 1:
766     if (!slice_intra_DCT(0, (u8*)mb8.Y, DCT_stride, ipu_cmd.pos[2] == 1))
767     {
768     ipu_cmd.pos[2] = 1;
769     return false;
770     }
771     case 2:
772     if (!slice_intra_DCT(0, (u8*)mb8.Y + 8, DCT_stride, ipu_cmd.pos[2] == 2))
773     {
774     ipu_cmd.pos[2] = 2;
775     return false;
776     }
777     case 3:
778     if (!slice_intra_DCT(0, (u8*)mb8.Y + DCT_offset, DCT_stride, ipu_cmd.pos[2] == 3))
779     {
780     ipu_cmd.pos[2] = 3;
781     return false;
782     }
783     case 4:
784     if (!slice_intra_DCT(0, (u8*)mb8.Y + DCT_offset + 8, DCT_stride, ipu_cmd.pos[2] == 4))
785     {
786     ipu_cmd.pos[2] = 4;
787     return false;
788     }
789     case 5:
790     if (!slice_intra_DCT(1, (u8*)mb8.Cb, decoder_stride >> 1, ipu_cmd.pos[2] == 5))
791     {
792     ipu_cmd.pos[2] = 5;
793     return false;
794     }
795     case 6:
796     if (!slice_intra_DCT(2, (u8*)mb8.Cr, decoder_stride >> 1, ipu_cmd.pos[2] == 6))
797     {
798     ipu_cmd.pos[2] = 6;
799     return false;
800     }
801 william 31 }
802    
803 william 62 // Send The MacroBlock via DmaIpuFrom
804     ipu_csc(mb8, rgb32, decoder.sgn);
805    
806     if (decoder.ofm == 0)
807     decoder.SetOutputTo(rgb32);
808     else
809 william 31 {
810 william 62 ipu_dither(rgb32, rgb16, decoder.dte);
811     decoder.SetOutputTo(rgb16);
812     }
813 william 31
814 william 62 case 2:
815     while (decoder.ipu0_data > 0)
816     {
817     uint read = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
818    
819 william 31 if (read == 0)
820     {
821 william 62 ipu_cmd.pos[1] = 2;
822     return false;
823 william 31 }
824     else
825     {
826 william 62 decoder.AdvanceIpuDataBy(read);
827 william 31 }
828     }
829    
830 william 62 decoder.mbc++;
831     mbaCount = 0;
832     case 3:
833     while (1)
834 william 31 {
835 william 62 if (!GETWORD())
836 william 31 {
837 william 62 ipu_cmd.pos[1] = 3;
838     return false;
839     }
840 william 31
841 william 62 code = UBITS(16);
842     if (code >= 0x1000)
843     {
844     mba = MBA.mba5 + (UBITS(5) - 2);
845     break;
846     }
847     else if (code >= 0x0300)
848     {
849     mba = MBA.mba11 + (UBITS(11) - 24);
850     break;
851     }
852     else switch (UBITS(11))
853     {
854     case 8: /* macroblock_escape */
855     mbaCount += 33;
856     /* pass through */
857 william 31
858 william 62 case 15: /* macroblock_stuffing (MPEG1 only) */
859     DUMPBITS(11);
860     continue;
861 william 31
862 william 62 default: /* end of slice/frame, or error? */
863     {
864     goto finish_idec;
865     }
866 william 31 }
867 william 62 }
868 william 31
869 william 62 DUMPBITS(mba->len);
870     mbaCount += mba->mba;
871 william 31
872 william 62 if (mbaCount)
873     {
874     decoder.dc_dct_pred[0] =
875     decoder.dc_dct_pred[1] =
876     decoder.dc_dct_pred[2] = 128 << decoder.intra_dc_precision;
877 william 31
878 william 62 decoder.mbc += mbaCount;
879     }
880    
881     case 4:
882     if (!GETWORD())
883 william 31 {
884 william 62 ipu_cmd.pos[1] = 4;
885     return false;
886 william 31 }
887 william 62
888     break;
889 william 31 }
890 william 62
891     ipu_cmd.pos[1] = 0;
892     ipu_cmd.pos[2] = 0;
893 william 31 }
894 william 62
895     finish_idec:
896     finishmpeg2sliceIDEC();
897 william 31
898 william 62 case 3:
899     bit8 = 1;
900     if (!getBits8((u8*)&bit8, 0))
901     {
902     ipu_cmd.pos[0] = 3;
903     return false;
904     }
905 william 31
906 william 62 if (bit8 == 0)
907     {
908     if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
909 william 31
910 william 62 ipuRegs.ctrl.SCD = 1;
911     }
912    
913     case 4:
914     if (!getBits32((u8*)&ipuRegs.top, 0))
915     {
916     ipu_cmd.pos[0] = 4;
917     return false;
918     }
919    
920     BigEndian(ipuRegs.top, ipuRegs.top);
921     break;
922     }
923    
924     return true;
925 william 31 }
926    
927 william 62 bool mpeg2_slice()
928 william 31 {
929     int DCT_offset, DCT_stride;
930 william 62 u8 bit8;
931 william 31
932 william 62 macroblock_8& mb8 = decoder.mb8;
933     macroblock_16& mb16 = decoder.mb16;
934 william 31
935 william 62 switch (ipu_cmd.pos[0])
936 william 31 {
937 william 62 case 0:
938     if (decoder.dcr)
939 william 31 {
940 william 62 decoder.dc_dct_pred[0] =
941     decoder.dc_dct_pred[1] =
942     decoder.dc_dct_pred[2] = 128 << decoder.intra_dc_precision;
943 william 31 }
944 william 62
945     ipuRegs.ctrl.ECD = 0;
946     ipuRegs.top = 0;
947     memzero_sse_a(mb8);
948     memzero_sse_a(mb16);
949     case 1:
950     if (!bitstream_init())
951     {
952     ipu_cmd.pos[0] = 1;
953     return false;
954     }
955 william 31
956 william 62 case 2:
957     ipu_cmd.pos[0] = 2;
958 william 31
959 william 62 if (decoder.macroblock_modes & DCT_TYPE_INTERLACED)
960 william 31 {
961 william 62 DCT_offset = decoder_stride;
962     DCT_stride = decoder_stride * 2;
963 william 31 }
964     else
965     {
966 william 62 DCT_offset = decoder_stride * 8;
967     DCT_stride = decoder_stride;
968 william 31 }
969    
970 william 62 if (decoder.macroblock_modes & MACROBLOCK_INTRA)
971     {
972     switch(ipu_cmd.pos[1])
973     {
974     case 0:
975     decoder.coded_block_pattern = 0x3F;
976     case 1:
977     if (!slice_intra_DCT(0, (u8*)mb8.Y, DCT_stride, ipu_cmd.pos[1] == 1))
978     {
979     ipu_cmd.pos[1] = 1;
980     return false;
981     }
982     case 2:
983     if (!slice_intra_DCT(0, (u8*)mb8.Y + 8, DCT_stride, ipu_cmd.pos[1] == 2))
984     {
985     ipu_cmd.pos[1] = 2;
986     return false;
987     }
988     case 3:
989     if (!slice_intra_DCT(0, (u8*)mb8.Y + DCT_offset, DCT_stride, ipu_cmd.pos[1] == 3))
990     {
991     ipu_cmd.pos[1] = 3;
992     return false;
993     }
994     case 4:
995     if (!slice_intra_DCT(0, (u8*)mb8.Y + DCT_offset + 8, DCT_stride, ipu_cmd.pos[1] == 4))
996     {
997     ipu_cmd.pos[1] = 4;
998     return false;
999     }
1000     case 5:
1001     if (!slice_intra_DCT(1, (u8*)mb8.Cb, decoder_stride >> 1, ipu_cmd.pos[1] == 5))
1002     {
1003     ipu_cmd.pos[1] = 5;
1004     return false;
1005     }
1006     case 6:
1007     if (!slice_intra_DCT(2, (u8*)mb8.Cr, decoder_stride >> 1, ipu_cmd.pos[1] == 6))
1008     {
1009     ipu_cmd.pos[1] = 6;
1010     return false;
1011     }
1012     break;
1013     }
1014 william 31
1015 william 62 ipu_copy(mb8, mb16);
1016     }
1017     else
1018     {
1019     if (decoder.macroblock_modes & MACROBLOCK_PATTERN)
1020     {
1021     switch(ipu_cmd.pos[1])
1022     {
1023     case 0:
1024     decoder.coded_block_pattern = get_coded_block_pattern(); // max 9bits
1025     case 1:
1026     if (decoder.coded_block_pattern & 0x20)
1027     {
1028     if (!slice_non_intra_DCT((s16*)mb16.Y, DCT_stride, ipu_cmd.pos[1] == 1))
1029     {
1030     ipu_cmd.pos[1] = 1;
1031     return false;
1032     }
1033     }
1034     case 2:
1035     if (decoder.coded_block_pattern & 0x10)
1036     {
1037     if (!slice_non_intra_DCT((s16*)mb16.Y + 8, DCT_stride, ipu_cmd.pos[1] == 2))
1038     {
1039     ipu_cmd.pos[1] = 2;
1040     return false;
1041     }
1042     }
1043     case 3:
1044     if (decoder.coded_block_pattern & 0x08)
1045     {
1046     if (!slice_non_intra_DCT((s16*)mb16.Y + DCT_offset, DCT_stride, ipu_cmd.pos[1] == 3))
1047     {
1048     ipu_cmd.pos[1] = 3;
1049     return false;
1050     }
1051     }
1052     case 4:
1053     if (decoder.coded_block_pattern & 0x04)
1054     {
1055     if (!slice_non_intra_DCT((s16*)mb16.Y + DCT_offset + 8, DCT_stride, ipu_cmd.pos[1] == 4))
1056     {
1057     ipu_cmd.pos[1] = 4;
1058     return false;
1059     }
1060     }
1061     case 5:
1062     if (decoder.coded_block_pattern & 0x2)
1063     {
1064     if (!slice_non_intra_DCT((s16*)mb16.Cb, decoder_stride >> 1, ipu_cmd.pos[1] == 5))
1065     {
1066     ipu_cmd.pos[1] = 5;
1067     return false;
1068     }
1069     }
1070     case 6:
1071     if (decoder.coded_block_pattern & 0x1)
1072     {
1073     if (!slice_non_intra_DCT((s16*)mb16.Cr, decoder_stride >> 1, ipu_cmd.pos[1] == 6))
1074     {
1075     ipu_cmd.pos[1] = 6;
1076     return false;
1077     }
1078     }
1079     break;
1080     }
1081     }
1082     }
1083 william 31
1084 william 62 // Send The MacroBlock via DmaIpuFrom
1085     ipuRegs.ctrl.SCD = 0;
1086     coded_block_pattern = decoder.coded_block_pattern;
1087     g_BP.BP += (int)decoder.bitstream_bits - 16;
1088 william 31
1089 william 62 // BP goes from 0 to 128, so negative values mean to read old buffer
1090     // so we minus from 128 to get the correct BP
1091     if ((int)g_BP.BP < 0)
1092     {
1093     g_BP.BP = 128 + (int)g_BP.BP;
1094 william 31
1095 william 62 // After BP is positioned correctly, we need to reload the old buffer
1096     // so that reading may continue properly
1097     ReorderBitstream();
1098     }
1099 william 31
1100 william 62 decoder.mbc = 1;
1101     decoder.SetOutputTo(mb16);
1102 william 31
1103 william 62 case 3:
1104     while (decoder.ipu0_data > 0)
1105     {
1106     uint size = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
1107 william 31
1108 william 62 if (size == 0)
1109     {
1110     ipu_cmd.pos[0] = 3;
1111     return false;
1112     }
1113     else
1114     {
1115     decoder.AdvanceIpuDataBy(size);
1116     }
1117     }
1118 william 31
1119 william 62 case 4:
1120     bit8 = 1;
1121     if (!getBits8((u8*)&bit8, 0))
1122 william 31 {
1123 william 62 ipu_cmd.pos[0] = 4;
1124     return false;
1125 william 31 }
1126    
1127 william 62 if (bit8 == 0)
1128     {
1129     if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
1130 william 31
1131 william 62 ipuRegs.ctrl.SCD = 1;
1132     }
1133 william 31
1134 william 62 case 5:
1135     if (!getBits32((u8*)&ipuRegs.top, 0))
1136 william 31 {
1137 william 62 ipu_cmd.pos[0] = 5;
1138     return false;
1139 william 31 }
1140    
1141 william 62 BigEndian(ipuRegs.top, ipuRegs.top);
1142     decoder.bitstream_bits = 0;
1143     break;
1144     }
1145 william 31
1146 william 62 return true;
1147     }

  ViewVC Help
Powered by ViewVC 1.1.22