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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show 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 /*
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 const 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 const DCTtab * tab;
52 int mbaCount = 0;
53
54 int get_macroblock_modes()
55 {
56 int macroblock_modes;
57 const MBtab * tab;
58
59 switch (decoder.coding_type)
60 {
61 case I_TYPE:
62 macroblock_modes = UBITS(2);
63
64 if (macroblock_modes == 0) return 0; // error
65
66 tab = MB_I + (macroblock_modes >> 1);
67 DUMPBITS(tab->len);
68 macroblock_modes = tab->modes;
69
70 if ((!(decoder.frame_pred_frame_dct)) &&
71 (decoder.picture_structure == FRAME_PICTURE))
72 {
73 macroblock_modes |= GETBITS(1) * DCT_TYPE_INTERLACED;
74 }
75 return macroblock_modes;
76
77 case P_TYPE:
78 macroblock_modes = UBITS(6);
79
80 if (macroblock_modes == 0) return 0; // error
81
82 tab = MB_P + (macroblock_modes >> 1);
83 DUMPBITS(tab->len);
84 macroblock_modes = tab->modes;
85
86 if (decoder.picture_structure != FRAME_PICTURE)
87 {
88 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
89 {
90 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
91 }
92
93 return macroblock_modes;
94 }
95 else if (decoder.frame_pred_frame_dct)
96 {
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 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
107 }
108
109 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
110 {
111 macroblock_modes |= GETBITS(1) * DCT_TYPE_INTERLACED;
112 }
113
114 return macroblock_modes;
115 }
116
117 case B_TYPE:
118 macroblock_modes = UBITS(6);
119
120 if (macroblock_modes == 0) return 0; // error
121
122 tab = MB_B + macroblock_modes;
123 DUMPBITS(tab->len);
124 macroblock_modes = tab->modes;
125
126 if (decoder.picture_structure != FRAME_PICTURE)
127 {
128 if (!(macroblock_modes & MACROBLOCK_INTRA))
129 {
130 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
131 }
132
133 return macroblock_modes;
134 }
135 else if (decoder.frame_pred_frame_dct)
136 {
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 macroblock_modes |= GETBITS(2) * MOTION_TYPE_BASE;
146
147 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
148 {
149 intra:
150 macroblock_modes |= GETBITS(1) * DCT_TYPE_INTERLACED;
151 }
152
153 return macroblock_modes;
154 }
155
156 case D_TYPE:
157 macroblock_modes = GETBITS(1);
158
159 if (macroblock_modes == 0) return 0; // error
160 return MACROBLOCK_INTRA;
161
162 default:
163 return 0;
164 }
165 }
166
167 static __fi int get_quantizer_scale()
168 {
169 int quantizer_scale_code;
170
171 quantizer_scale_code = GETBITS(5);
172
173 if (decoder.q_scale_type)
174 return non_linear_quantizer_scale [quantizer_scale_code];
175 else
176 return quantizer_scale_code << 1;
177 }
178
179 static __fi int get_coded_block_pattern()
180 {
181 const CBPtab * tab;
182 u16 code = UBITS(16);
183
184 if (code >= 0x2000)
185 tab = CBP_7 + (UBITS(7) - 16);
186 else
187 tab = CBP_9 + UBITS(9);
188
189 DUMPBITS(tab->len);
190 return tab->cbp;
191 }
192
193 int __fi get_motion_delta(const int f_code)
194 {
195 int delta;
196 int sign;
197 const MVtab * tab;
198 u16 code = UBITS(16);
199
200 if ((code & 0x8000))
201 {
202 DUMPBITS(1);
203 return 0x00010000;
204 }
205 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
214 delta = tab->delta + 1;
215 DUMPBITS(tab->len);
216
217 sign = SBITS(1);
218 DUMPBITS(1);
219 return (delta ^ sign) - sign;
220 }
221
222 int __fi get_dmv()
223 {
224 const DMVtab * tab;
225
226 tab = DMV_2 + UBITS(2);
227 DUMPBITS(tab->len);
228 return tab->dmv;
229 }
230
231 int get_macroblock_address_increment()
232 {
233 const MBAtab *mba;
234
235 u16 code = UBITS(16);
236
237 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 {
243
244 case 8: /* macroblock_escape */
245 DUMPBITS(11);
246 return 0x23;
247
248 case 15: /* macroblock_stuffing (MPEG1 only) */
249 if (decoder.mpeg1)
250 {
251 DUMPBITS(11);
252 return 0x22;
253 }
254
255 default:
256 return 0;//error
257 }
258
259 DUMPBITS(mba->len);
260
261 return mba->mba + 1;
262 }
263
264 static __fi int get_luma_dc_dct_diff()
265 {
266 int size;
267 int dc_diff;
268 u16 code = UBITS(5);
269
270 if (code < 31)
271 {
272 size = DCtable.lum0[code].size;
273 DUMPBITS(DCtable.lum0[code].len);
274
275 // 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
283 // 9 bits max
284 }
285
286 if (size==0)
287 dc_diff = 0;
288 else
289 {
290 dc_diff = GETBITS(size);
291
292 // 6 for tab0 and 11 for tab1
293 if ((dc_diff & (1<<(size-1)))==0)
294 dc_diff-= (1<<size) - 1;
295 }
296
297 return dc_diff;
298 }
299
300 static __fi int get_chroma_dc_dct_diff()
301 {
302 int size;
303 int dc_diff;
304 u16 code = UBITS(5);
305
306 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
324 if ((dc_diff & (1<<(size-1)))==0)
325 {
326 dc_diff-= (1<<size) - 1;
327 }
328 }
329
330 return dc_diff;
331 }
332
333 #define SATURATE(val) \
334 do { \
335 if (((u32)(val + 2048) > 4095)) \
336 val = (((s32)val) >> 31) ^ 2047; \
337 } while (0)
338
339 static __fi bool get_intra_block()
340 {
341 int i;
342 int j;
343 int val;
344 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
350 /* 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
362 code = UBITS(16);
363
364 if (code >= 16384 && (!decoder.intra_vlc_format || decoder.mpeg1))
365 {
366 tab = &DCT.next[(code >> 12) - 4];
367 }
368 else if (code >= 1024)
369 {
370 if (decoder.intra_vlc_format && !decoder.mpeg1)
371 {
372 tab = &DCT.tab0a[(code >> 8) - 4];
373 }
374 else
375 {
376 tab = &DCT.tab0[(code >> 8) - 4];
377 }
378 }
379 else if (code >= 512)
380 {
381 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 }
390
391 // [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 {
398 tab = &DCT.tab2[(code >> 4) - 16];
399 }
400 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 else
417 {
418 ipu_cmd.pos[4] = 0;
419 return true;
420 }
421
422 DUMPBITS(tab->len);
423
424 if (tab->run==64) /* end_of_block */
425 {
426 ipu_cmd.pos[4] = 0;
427 return true;
428 }
429
430 i+= tab->run == 65 ? GETBITS(6) : tab->run;
431 if (i >= 64)
432 {
433 ipu_cmd.pos[4] = 0;
434 return true;
435 }
436 case 1:
437 if (!GETWORD())
438 {
439 ipu_cmd.pos[4] = i - 1;
440 ipu_cmd.pos[5] = 1;
441 return false;
442 }
443
444 j = scan[i];
445
446 if (tab->run==65) /* escape */
447 {
448 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
458 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 }
467 else
468 {
469 val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
470 if(decoder.mpeg1)
471 {
472 /* oddification */
473 val = (val - 1) | 1;
474 }
475
476 /* if (bitstream_get (1)) val = -val; */
477 val = (val ^ SBITS(1)) - SBITS(1);
478 DUMPBITS(1);
479 }
480
481 SATURATE(val);
482 dest[j] = val;
483 ipu_cmd.pos[5] = 0;
484 }
485 }
486
487 ipu_cmd.pos[4] = 0;
488 return true;
489 }
490
491 static __fi bool get_non_intra_block(int * last)
492 {
493 int i;
494 int j;
495 int val;
496 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
502 /* decode AC coefficients */
503 for (i= ipu_cmd.pos[4] ; ; i++)
504 {
505 switch (ipu_cmd.pos[5])
506 {
507 case 0:
508 if (!GETWORD())
509 {
510 ipu_cmd.pos[4] = i;
511 return false;
512 }
513
514 code = UBITS(16);
515
516 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
536 // [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
541 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
567 DUMPBITS(tab->len);
568
569 if (tab->run==64) /* end_of_block */
570 {
571 *last = i;
572 ipu_cmd.pos[4] = 0;
573 return true;
574 }
575
576 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
584 case 1:
585 if (!GETWORD())
586 {
587 ipu_cmd.pos[4] = i;
588 ipu_cmd.pos[5] = 1;
589 return false;
590 }
591
592 j = scan[i];
593
594 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
606 if (!(val & 0x7f))
607 {
608 val = GETBITS(8) + 2 * val;
609 }
610
611 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 {
617 val = ((2 * tab->level + 1) * quantizer_scale * quant_matrix[i]) >> 5;
618 val = (val ^ SBITS(1)) - SBITS(1);
619 DUMPBITS(1);
620 }
621
622 SATURATE(val);
623 dest[j] = val;
624 ipu_cmd.pos[5] = 0;
625 }
626 }
627
628 ipu_cmd.pos[4] = 0;
629 return true;
630 }
631
632 static __fi bool slice_intra_DCT(const int cc, u8 * const dest, const int stride, const bool skip)
633 {
634 if (!skip || ipu_cmd.pos[3])
635 {
636 ipu_cmd.pos[3] = 0;
637 if (!GETWORD())
638 {
639 ipu_cmd.pos[3] = 1;
640 return false;
641 }
642
643 /* 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 else
647 decoder.dc_dct_pred[cc] += get_chroma_dc_dct_diff();
648
649 decoder.DCTblock[0] = decoder.dc_dct_pred[cc] << (3 - decoder.intra_dc_precision);
650 }
651
652 if (!get_intra_block())
653 {
654 return false;
655 }
656
657 mpeg2_idct_copy(decoder.DCTblock, dest, stride);
658
659 return true;
660 }
661
662 static __fi bool slice_non_intra_DCT(s16 * const dest, const int stride, const bool skip)
663 {
664 int last;
665
666 if (!skip)
667 {
668 memzero_sse_a(decoder.DCTblock);
669 }
670
671 if (!get_non_intra_block(&last))
672 {
673 return false;
674 }
675
676 mpeg2_idct_add(last, decoder.DCTblock, dest, stride);
677
678 return true;
679 }
680
681 void __fi finishmpeg2sliceIDEC()
682 {
683 ipuRegs.ctrl.SCD = 0;
684 coded_block_pattern = decoder.coded_block_pattern;
685
686 g_BP.BP += decoder.bitstream_bits - 16;
687
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 bool mpeg2sliceIDEC()
701 {
702 u16 code;
703 u8 bit8;
704
705 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
712 decoder.mbc = 0;
713 ipuRegs.top = 0;
714 ipuRegs.ctrl.ECD = 0;
715
716 case 1:
717 ipu_cmd.pos[0] = 1;
718 if (!bitstream_init())
719 {
720 return false;
721 }
722
723 case 2:
724 ipu_cmd.pos[0] = 2;
725 while (1)
726 {
727 macroblock_8& mb8 = decoder.mb8;
728 macroblock_rgb16& rgb16 = decoder.rgb16;
729 macroblock_rgb32& rgb32 = decoder.rgb32;
730
731 int DCT_offset, DCT_stride;
732 const MBAtab * mba;
733
734 switch (ipu_cmd.pos[1])
735 {
736 case 0:
737 decoder.macroblock_modes = get_macroblock_modes();
738
739 if (decoder.macroblock_modes & MACROBLOCK_QUANT) //only IDEC
740 {
741 decoder.quantizer_scale = get_quantizer_scale();
742 }
743
744 decoder.coded_block_pattern = 0x3F;//all 6 blocks
745 memzero_sse_a(mb8);
746 memzero_sse_a(rgb32);
747
748 case 1:
749 ipu_cmd.pos[1] = 1;
750
751 if (decoder.macroblock_modes & DCT_TYPE_INTERLACED)
752 {
753 DCT_offset = decoder_stride;
754 DCT_stride = decoder_stride * 2;
755 }
756 else
757 {
758 DCT_offset = decoder_stride * 8;
759 DCT_stride = decoder_stride;
760 }
761
762 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 }
802
803 // 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 {
810 ipu_dither(rgb32, rgb16, decoder.dte);
811 decoder.SetOutputTo(rgb16);
812 }
813
814 case 2:
815 while (decoder.ipu0_data > 0)
816 {
817 uint read = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
818
819 if (read == 0)
820 {
821 ipu_cmd.pos[1] = 2;
822 return false;
823 }
824 else
825 {
826 decoder.AdvanceIpuDataBy(read);
827 }
828 }
829
830 decoder.mbc++;
831 mbaCount = 0;
832 case 3:
833 while (1)
834 {
835 if (!GETWORD())
836 {
837 ipu_cmd.pos[1] = 3;
838 return false;
839 }
840
841 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
858 case 15: /* macroblock_stuffing (MPEG1 only) */
859 DUMPBITS(11);
860 continue;
861
862 default: /* end of slice/frame, or error? */
863 {
864 goto finish_idec;
865 }
866 }
867 }
868
869 DUMPBITS(mba->len);
870 mbaCount += mba->mba;
871
872 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
878 decoder.mbc += mbaCount;
879 }
880
881 case 4:
882 if (!GETWORD())
883 {
884 ipu_cmd.pos[1] = 4;
885 return false;
886 }
887
888 break;
889 }
890
891 ipu_cmd.pos[1] = 0;
892 ipu_cmd.pos[2] = 0;
893 }
894
895 finish_idec:
896 finishmpeg2sliceIDEC();
897
898 case 3:
899 bit8 = 1;
900 if (!getBits8((u8*)&bit8, 0))
901 {
902 ipu_cmd.pos[0] = 3;
903 return false;
904 }
905
906 if (bit8 == 0)
907 {
908 if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
909
910 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 }
926
927 bool mpeg2_slice()
928 {
929 int DCT_offset, DCT_stride;
930 u8 bit8;
931
932 macroblock_8& mb8 = decoder.mb8;
933 macroblock_16& mb16 = decoder.mb16;
934
935 switch (ipu_cmd.pos[0])
936 {
937 case 0:
938 if (decoder.dcr)
939 {
940 decoder.dc_dct_pred[0] =
941 decoder.dc_dct_pred[1] =
942 decoder.dc_dct_pred[2] = 128 << decoder.intra_dc_precision;
943 }
944
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
956 case 2:
957 ipu_cmd.pos[0] = 2;
958
959 if (decoder.macroblock_modes & DCT_TYPE_INTERLACED)
960 {
961 DCT_offset = decoder_stride;
962 DCT_stride = decoder_stride * 2;
963 }
964 else
965 {
966 DCT_offset = decoder_stride * 8;
967 DCT_stride = decoder_stride;
968 }
969
970 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
1015 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
1084 // 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
1089 // 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
1095 // After BP is positioned correctly, we need to reload the old buffer
1096 // so that reading may continue properly
1097 ReorderBitstream();
1098 }
1099
1100 decoder.mbc = 1;
1101 decoder.SetOutputTo(mb16);
1102
1103 case 3:
1104 while (decoder.ipu0_data > 0)
1105 {
1106 uint size = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
1107
1108 if (size == 0)
1109 {
1110 ipu_cmd.pos[0] = 3;
1111 return false;
1112 }
1113 else
1114 {
1115 decoder.AdvanceIpuDataBy(size);
1116 }
1117 }
1118
1119 case 4:
1120 bit8 = 1;
1121 if (!getBits8((u8*)&bit8, 0))
1122 {
1123 ipu_cmd.pos[0] = 4;
1124 return false;
1125 }
1126
1127 if (bit8 == 0)
1128 {
1129 if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
1130
1131 ipuRegs.ctrl.SCD = 1;
1132 }
1133
1134 case 5:
1135 if (!getBits32((u8*)&ipuRegs.top, 0))
1136 {
1137 ipu_cmd.pos[0] = 5;
1138 return false;
1139 }
1140
1141 BigEndian(ipuRegs.top, ipuRegs.top);
1142 decoder.bitstream_bits = 0;
1143 break;
1144 }
1145
1146 return true;
1147 }

  ViewVC Help
Powered by ViewVC 1.1.22