home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Video / getpic.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  36.3 KB  |  1,293 lines

  1. /* getpic.c, picture decoding                                               */
  2. /* Last modified by PX3 July 30 2000  Optimizations done                    */
  3.  
  4. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  5.  
  6. /*
  7.  * Disclaimer of Warranty
  8.  *
  9.  * These software programs are available to the user without any license fee or
  10.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  11.  * any and all warranties, whether express, implied, or statuary, including any
  12.  * implied warranties or merchantability or of fitness for a particular
  13.  * purpose.  In no event shall the copyright-holder be liable for any
  14.  * incidental, punitive, or consequential damages of any kind whatsoever
  15.  * arising from the use of these programs.
  16.  *
  17.  * This disclaimer of warranty extends to the user of these programs and user's
  18.  * customers, employees, agents, transferees, successors, and assigns.
  19.  *
  20.  * The MPEG Software Simulation Group does not represent or warrant that the
  21.  * programs furnished hereunder are free of infringement of any third-party
  22.  * patents.
  23.  *
  24.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  25.  * are subject to royalty fees to patent holders.  Many of these patents are
  26.  * general enough such that they are unavoidable regardless of implementation
  27.  * design.
  28.  *
  29.  */
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "config.h"
  34. extern "C"
  35. {
  36. #include "global.h"
  37. }
  38. //#include "getbits.h"
  39.  
  40. #include "..\Video\VideoWrapper.h"
  41. extern VideoWrapper *myVideo;
  42. extern __int64 rawPTS[1024];
  43. extern int gopPOS;
  44. extern int gopNum;
  45. extern bool     synced;
  46. /* private prototypes*/
  47.  
  48. #ifdef TRACE
  49. #undef TRACE
  50. #endif
  51. void picture_data _ANSI_ARGS_(());
  52. __inline static void macroblock_modes _ANSI_ARGS_((int *pmacroblock_type, int *pstwtype,
  53.   int *pstwclass, int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv,
  54.   int *pmvscale, int *pdct_type));
  55. __inline static void Clear_Block _ANSI_ARGS_((int comp));
  56. static void Sum_Block _ANSI_ARGS_((int comp));
  57. static void Saturate _ANSI_ARGS_((short *bp));
  58. __inline static void Add_Block _ANSI_ARGS_((int comp, int bx, int by,
  59.   int dct_type, int addflag));
  60. static void Update_Picture_Buffers _ANSI_ARGS_((void));
  61. static void Decode_SNR_Macroblock _ANSI_ARGS_((int *SNRMBA, int *SNRMBAinc, 
  62.   int MBA, int MBAmax, int *dct_type));
  63.  
  64. __inline static void motion_compensation _ANSI_ARGS_((int MBA, int macroblock_type, 
  65.  int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2], 
  66.  int dmvector[2], int stwtype, int dct_type));
  67.  
  68. __inline static void skipped_macroblock _ANSI_ARGS_((int dc_dct_pred[3], 
  69.   int PMV[2][2][2], int *motion_type, int motion_vertical_field_select[2][2],
  70.   int *stwtype, int *macroblock_type));
  71.  
  72. static int slice _ANSI_ARGS_((int MBAmax));
  73.  
  74. __inline static int start_of_slice _ANSI_ARGS_ ((int MBAmax, int *MBA,
  75.   int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]));
  76.  
  77. __inline static int decode_macroblock _ANSI_ARGS_((int *macroblock_type, 
  78.   int *stwtype, int *stwclass, int *motion_type, int *dct_type,
  79.   int PMV[2][2][2], int dc_dct_pred[3], 
  80.   int motion_vertical_field_select[2][2], int dmvector[2]));
  81.  
  82. #define Show_Bits(N) ( ld->Bfr >> (32-N) )
  83.  
  84.  
  85. /* decode all macroblocks of the current picture */
  86. /* stages described in ISO/IEC 13818-2 section 7 */
  87. void picture_data()
  88. {
  89.   int MBAmax;
  90.   int ret;
  91.  
  92.   /* number of macroblocks per picture */
  93.   MBAmax = mb_width*mb_height;
  94.  
  95.   if (picture_structure!=FRAME_PICTURE)
  96.     MBAmax>>=1; /* field picture has half as mnay macroblocks as frame */
  97.  
  98.   for(;;)
  99.   {
  100.     if((ret=slice(MBAmax))<0)
  101.       return;
  102.   }
  103.  
  104. }
  105.  
  106.  
  107.  
  108. /* decode all macroblocks of the current picture */
  109. /* ISO/IEC 13818-2 section 6.3.16 */
  110. static int slice( int MBAmax )
  111. {
  112.   int MBA; 
  113.   int MBAinc, macroblock_type, motion_type, dct_type;
  114.   int dc_dct_pred[3];
  115.   int PMV[2][2][2], motion_vertical_field_select[2][2];
  116.   int dmvector[2];
  117.   int stwtype, stwclass;
  118.   int ret;
  119.  
  120.   MBA = 0; /* macroblock address */
  121.   MBAinc = 0;
  122.  
  123.   if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
  124.     return(ret);
  125.  
  126.   Fault_Flag=0;
  127.  
  128.   for (;;)
  129.   {
  130.  
  131.     /* this is how we properly exit out of picture */
  132.     if (MBA>=MBAmax)
  133.       return(-1); /* all macroblocks decoded */
  134.  
  135. #ifdef TRACE
  136.     if (Trace_Flag)
  137.       printf("frame %d, MB %d\n",framenum,MBA);
  138. #endif /* TRACE */
  139.  
  140. #ifdef DISPLAY
  141.     if (!progressive_frame && picture_structure==FRAME_PICTURE 
  142.       && MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11 
  143.        && !Display_Progressive_Flag)
  144.     {
  145.       Display_Second_Field();
  146.     }
  147. #endif
  148.  
  149.     ld = &base;
  150.  
  151.     if (MBAinc==0)
  152.     {
  153.       if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
  154.       {
  155. resync: /* if Fault_Flag: resynchronize to next next_start_code */
  156.         Fault_Flag = 0;
  157.         return(0);     /* trigger: go to next slice */
  158.       }
  159.       else /* neither next_start_code nor Fault_Flag */
  160.       {
  161.         /* decode macroblock address increment */
  162.         MBAinc = Get_macroblock_address_increment();
  163.  
  164.         if (Fault_Flag) goto resync;
  165.       }
  166.     }
  167.  
  168.     if (MBA>=MBAmax)
  169.     {
  170.       /* MBAinc points beyond picture dimensions */
  171.       if (!Quiet_Flag)
  172.         printf("Too many macroblocks in picture\n");
  173.       return(-1);
  174.     }
  175.  
  176.     if (MBAinc==1) /* not skipped */
  177.     {
  178.     
  179.         ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
  180.               &motion_type, &dct_type, PMV, dc_dct_pred, 
  181.               motion_vertical_field_select, dmvector);
  182.       
  183.       if(ret==-1)
  184.         return(-1);
  185.    
  186.       if(ret==0)
  187.         goto resync;
  188.  
  189.     }
  190.     else /* MBAinc!=1: skipped macroblock */
  191.     {      
  192.       /* ISO/IEC 13818-2 section 7.6.6 */
  193.       skipped_macroblock(dc_dct_pred, PMV, &motion_type, 
  194.         motion_vertical_field_select, &stwtype, ¯oblock_type);
  195.     }
  196.  
  197.     /* ISO/IEC 13818-2 section 7.6 */
  198.     motion_compensation(MBA, macroblock_type, motion_type, PMV, 
  199.       motion_vertical_field_select, dmvector, stwtype, dct_type);
  200.  
  201.  
  202.     /* advance to next macroblock */
  203.     MBA++;
  204.     MBAinc--;
  205.  
  206.     if (MBA>=MBAmax)
  207.       return(-1); /* all macroblocks decoded */
  208.   }
  209.  
  210. }
  211.  
  212.  
  213. /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  214. static void macroblock_modes(
  215.   int *pmacroblock_type, int *pstwtype, int *pstwclass,
  216.   int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv, int *pmvscale,
  217.   int *pdct_type )
  218. {
  219.   int macroblock_type;
  220.   int stwtype, stwcode, stwclass;
  221.   int motion_type = 0;
  222.   int motion_vector_count, mv_format, dmv, mvscale;
  223.   int dct_type;
  224.   static unsigned char stwc_table[3][4]
  225.     = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
  226.   static unsigned char stwclass_table[9]
  227.     = {0, 1, 2, 1, 1, 2, 3, 3, 4};
  228.  
  229.   /* get macroblock_type */
  230.   macroblock_type = Get_macroblock_type();
  231.  
  232.   if (Fault_Flag) return;
  233.  
  234.   /* get spatial_temporal_weight_code */
  235.   if (macroblock_type & MB_WEIGHT)
  236.   {
  237.     if (spatial_temporal_weight_code_table_index==0)
  238.       stwtype = 4;
  239.     else
  240.     {
  241.       stwcode = Get_Bits(2);
  242. #ifdef TRACE
  243.       if (Trace_Flag)
  244.       {
  245.         printf("spatial_temporal_weight_code (");
  246.         Print_Bits(stwcode,2,2);
  247.         printf("): %d\n",stwcode);
  248.       }
  249. #endif /* TRACE */
  250.       stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
  251.     }
  252.   }
  253.   else
  254.     stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
  255.  
  256.   /* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
  257.   stwclass = stwclass_table[stwtype];
  258.  
  259.   /* get frame/field motion type */
  260.   if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
  261.   {
  262.     if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
  263.     {
  264.       motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
  265. #ifdef TRACE
  266.       if (!frame_pred_frame_dct && Trace_Flag)
  267.       {
  268.         printf("frame_motion_type (");
  269.         Print_Bits(motion_type,2,2);
  270.         printf("): %s\n",motion_type==MC_FIELD?"Field":
  271.                          motion_type==MC_FRAME?"Frame":
  272.                          motion_type==MC_DMV?"Dual_Prime":"Invalid");
  273.       }
  274. #endif /* TRACE */
  275.     }
  276.     else /* field_motion_type */
  277.     {
  278.       motion_type = Get_Bits(2);
  279. #ifdef TRACE
  280.       if (Trace_Flag)
  281.       {
  282.         printf("field_motion_type (");
  283.         Print_Bits(motion_type,2,2);
  284.         printf("): %s\n",motion_type==MC_FIELD?"Field":
  285.                          motion_type==MC_16X8?"16x8 MC":
  286.                          motion_type==MC_DMV?"Dual_Prime":"Invalid");
  287.       }
  288. #endif /* TRACE */
  289.     }
  290.   }
  291.   else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
  292.   {
  293.     /* concealment motion vectors */
  294.     motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
  295.   }
  296. #if 0
  297.   else
  298.   {
  299.     printf("maroblock_modes(): unknown macroblock type\n");
  300.     motion_type = -1;
  301.   }
  302. #endif
  303.  
  304.   /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
  305.   if (picture_structure==FRAME_PICTURE)
  306.   {
  307.     motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
  308.     mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
  309.   }
  310.   else
  311.   {
  312.     motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
  313.     mv_format = MV_FIELD;
  314.   }
  315.  
  316.   dmv = (motion_type==MC_DMV); /* dual prime */
  317.  
  318.   /* field mv predictions in frame pictures have to be scaled
  319.    * ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
  320.    * IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
  321.    * it displaces the stage:
  322.    *
  323.    *    if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
  324.    *      prediction = PMV[r][s][t] DIV 2;
  325.    */
  326.  
  327.   mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
  328.  
  329.   /* get dct_type (frame DCT / field DCT) */
  330.   dct_type = (picture_structure==FRAME_PICTURE)
  331.              && (!frame_pred_frame_dct)
  332.              && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
  333.              ? Get_Bits(1)
  334.              : 0;
  335.  
  336. #ifdef TRACE
  337.   if (Trace_Flag  && (picture_structure==FRAME_PICTURE)
  338.              && (!frame_pred_frame_dct)
  339.              && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
  340.     printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
  341. #endif /* TRACE */
  342.  
  343.   /* return values */
  344.   *pmacroblock_type = macroblock_type;
  345.   *pstwtype = stwtype;
  346.   *pstwclass = stwclass;
  347.   *pmotion_type = motion_type;
  348.   *pmotion_vector_count = motion_vector_count;
  349.   *pmv_format = mv_format;
  350.   *pdmv = dmv;
  351.   *pmvscale = mvscale;
  352.   *pdct_type = dct_type;
  353. }
  354.  
  355.  
  356.  
  357. /* move/add 8x8-Block from block[comp] to backward_reference_frame */
  358. /* copy reconstructed 8x8 block from block[comp] to current_frame[]
  359.  * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
  360.  * This stage also embodies some of the operations implied by:
  361.  *   - ISO/IEC 13818-2 section 7.6.7: Combining predictions
  362.  *   - ISO/IEC 13818-2 section 6.1.3: Macroblock
  363. */
  364. static void Add_Block(int comp,int bx,int by,int dct_type,int addflag)
  365. {
  366.   //int cc,i, j, iincr;
  367.   int cc,iincr;
  368.   unsigned char *rfp;
  369.   short *bp;
  370.   DWORD *dwbp ;
  371.   static const __int64 mmMask128w = 0x0080008000800080;
  372.   DWORD ShiftLeftAmt;
  373.   DWORD ShiftRightAmt;
  374.  
  375.   
  376.   /* derive color component index */
  377.   /* equivalent to ISO/IEC 13818-2 Table 7-1 */
  378.   cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
  379.  
  380.   if (cc==0)
  381.   {
  382.     /* luminance */
  383.  
  384.     if (picture_structure==FRAME_PICTURE)
  385.       if (dct_type)
  386.       {
  387.         /* field DCT coding */
  388.         rfp = current_frame[0]
  389.               + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
  390.         iincr = (Coded_Picture_Width<<1) - 8;
  391.       }
  392.       else
  393.       {
  394.         /* frame DCT coding */
  395.         rfp = current_frame[0]
  396.               + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  397.         iincr = Coded_Picture_Width - 8;
  398.       }
  399.     else
  400.     {
  401.       /* field picture */
  402.       rfp = current_frame[0]
  403.             + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  404.       iincr = (Coded_Picture_Width<<1) - 8;
  405.     }
  406.   }
  407.   else
  408.   {
  409.     /* chrominance */
  410.  
  411.     /* scale coordinates */
  412.     if (chroma_format!=CHROMA444)
  413.     {
  414.         bx >>= 1;
  415.         if (chroma_format==CHROMA420)
  416.           by >>= 1;
  417.     }
  418.  
  419.     if (picture_structure==FRAME_PICTURE)
  420.     {
  421.       if (dct_type && (chroma_format!=CHROMA420))
  422.       {
  423.         /* field DCT coding */
  424.         rfp = current_frame[cc]
  425.               + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
  426.         iincr = (Chroma_Width<<1) - 8;
  427.       }
  428.       else
  429.       {
  430.         /* frame DCT coding */
  431.         rfp = current_frame[cc]
  432.               + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
  433.         iincr = Chroma_Width - 8;
  434.       }
  435.     }
  436.     else
  437.     {
  438.       /* field picture */
  439.       rfp = current_frame[cc]
  440.             + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
  441.       iincr = (Chroma_Width<<1) - 8;
  442.     }
  443.   }
  444.  
  445.   bp = ld->block[comp];
  446.  
  447.   /* Pixel processing routine, MMX optimized */
  448. /*
  449.   if (addflag)
  450.   {
  451.     for (i=0; i<8; i++)
  452.     {
  453.       for (j=0; j<8; j++)
  454.       {
  455.         *rfp = Clip[*bp++ + *rfp];
  456.         rfp++;
  457.       }
  458.  
  459.       rfp+= iincr;
  460.     }
  461.   }
  462.   else
  463.   {
  464.     for (i=0; i<8; i++)
  465.     {
  466.       for (j=0; j<8; j++)
  467.         *rfp++ = Clip[*bp++ + 128];
  468.  
  469.       rfp+= iincr;
  470.     }
  471.   }*/
  472.  
  473.   /* minor MMX optimization v0.16B22 */
  474.   // 0.16B23, aligned reads to speed up access 
  475.  
  476.   ShiftRightAmt = ((DWORD)rfp) & 0x07; // #bytes distance to QWORD alignment
  477.   ShiftRightAmt = (ShiftRightAmt << 3); // #bits to rshift
  478.   ShiftLeftAmt = 64 - ShiftRightAmt;    // #bits to lshift
  479.   dwbp = (DWORD *)bp;  // set dwbp, for MMX routine
  480.  
  481.   if (addflag)
  482.   {
  483.     __asm    // average source IDCT bp[] and destination pixel-buffer rfp[]
  484.     {
  485.         movd mm6, [ShiftLeftAmt];    // amount to lshift unaligned rfp[15..8]
  486.          pxor        mm0, mm0               ; //mm0 = 0
  487.     
  488.         mov         edx, dword ptr [rfp]    ; // edx <= &rfp[0]
  489.          mov eax, 0x08    ;eax <= i = 8
  490.  
  491.         mov            edi, [iincr]        ; // esi = iincr
  492.          mov       esi, edx;    // esi = aligned read-location rfp
  493.  
  494.         mov         ecx, dword ptr [dwbp]    ; // ecx <= &dwbp[0]
  495.          and      esi, 0xFFFFFFF8    ; // esi = QWORD aligned rfp[]
  496.  
  497.         movd mm7, [ShiftRightAmt];    // amount to rshift unaligned rfp[7..0]
  498.  
  499.         //for (i=0; i<8; i++)
  500.         jmp start1;  // skip first store-operation
  501.     
  502.     lp1_loop:
  503.         movq qword ptr [edx-8], mm1 ; //store rfp[0..7]
  504.          add edx, edi;        ; // (true address) rfp += iincr 
  505.     start1:
  506.  
  507.         // v0.16b23, the qword reads from rfp[] are now QWORD aligned.
  508.         // To implement this, there are two rfp[] pointers, esi & edx
  509.         // esi = (QWORD) aligned rfp[] (rounded down), for reading SOURCE data
  510.         // edx = non-aligned (true addr) rfp[], for writing back data
  511.         movq mm3, qword ptr [esi] ; //mm3 <= unaligned rfp[0..7]
  512.          add edx, 0x08;
  513.  
  514.         movq mm5, qword ptr [esi+8]; //mm5 <= unaligned rfp[15..8]
  515.          psrlq mm3, mm7;    // shift mm3 by right-shift-amount
  516.  
  517.         movq mm1, qword ptr [ecx] ;//mm1 <= short bp[0..3]  
  518.          psllq mm5, mm6;        // shift mm5 by left-shift-amount
  519.  
  520.         movq mm2, qword ptr [ecx+8]  ;//mm2 <= short bp[4..7]
  521.          por mm3, mm5;        // mm3 = the desired aligned rfp[0..7] INPUT
  522.         // mm3 now holds the desired source data rfp[0..7]
  523.         
  524.         movq mm4, mm3            ; //mm4 = unsigned char rfp[0..7]
  525.          punpcklbw mm3, mm0        ; //mm3 <= unsigned short ( rfp[0..3]  )
  526.  
  527.         // mm0 = 0x0000_0000
  528.         
  529.         punpckhbw mm4, mm0        ; //mm4 <= unsigned short ( rfp[4..7] )
  530.          paddsw mm1, mm3            ; //mm3 <= rfp[0..3] + bp[0..3]
  531.  
  532.         add esi, edi;        ; // (qword aligned) rfp += iincr 
  533.          paddsw mm2, mm4            ; //mm3 <= rfp[4..7] + bp[4..7]
  534.  
  535.         packuswb mm1, mm2       ; short rfp[0..7] -> unsigned char rfp[ 0..7]
  536.          add esi, 0x08;    // aligned rfp += 8;
  537.         
  538.         sub eax, 0x01        ; i -= 1
  539.          add ecx, 0x10        ; dwbp += 16;
  540.  
  541.     //  software-pipelining -- store moved to after branch
  542.     //    movq qword ptr [edx-8], mm1 ; store rfp[0..7]; // post-loop
  543.         
  544.         cmp eax, 0x00;    // compare i <> 0
  545.          jg lp1_loop        ; loop // end for ( i=0; i < 8; ++i )
  546.  
  547.         movq qword ptr [edx-8], mm1 ; store rfp[0..7]; // post-loop
  548.  
  549.     } // end __asm 
  550.   }
  551.   else
  552.   {
  553.     __asm
  554.     {    // create mask [ +128, +128, +128, +128 ]
  555.          movq    mm0, [mmMask128w];  // mm0 = 0x00800080_00800080
  556.          mov    eax, 0x00000008;    eax <= i = 8
  557.  
  558.         mov         ecx, dword ptr [dwbp]    ; // ecx <= &dwbp[0]
  559.  
  560.         mov            ebx, [iincr]        ; // ebx = iincr
  561.  
  562.         mov         edx, dword ptr [rfp]    ; // edx <= &rfp[0]
  563.          jmp lp2_loop;    // skip first store operation
  564.         //for (i=0; i<8; i++)
  565. /*
  566.     This is the original version of the MMX loop.  it's here for
  567.     ease of readability.  The actual implementation is slightly 
  568.     unrolled (software pipelining.)
  569.  
  570.     lp2_loop :
  571.         movq mm1, qword ptr [ecx] ;mm1 <= short bp[0..3]  
  572.          add edx, 0x08;
  573.  
  574.         movq mm2, qword ptr [ecx+8]  ;mm2 <= short bp[4..7]
  575.          add ecx, 0x10        ; dwbp += 16;
  576.  
  577.         paddsw mm1, mm0;    // bp[0..3] + [128,128,128,128]
  578.          paddsw mm2, mm0;    // bp[4..7] + [128,128,128,128]
  579.         
  580.         packuswb mm1, mm2       ; short rfp[0..7] -> unsigned char rfp[ 0..7]
  581.  
  582.         movq qword ptr [edx-8], mm1 ; store rfp[0..7]
  583.          add edx, ebx;        ; // rfp += iincr 
  584.  
  585.         sub eax, 0x01        ; i -= 1
  586.          jnz    lp2_loop        ; loop // end for ( i=0; i < 8; ++i )
  587. */
  588.     // this implementation unrolls the loop slightly.
  589.     // Instead of performing one row-operation per loop-iteration,
  590.     // this implementation performs 2 row operations
  591.     // MMX-instructions are better scheduled, there is now only
  592.     // 1 clock stall
  593.     lp2_loopa:
  594.         movq qword ptr [edx-8], mm3 ; store rfp[0..7]
  595.          add edx, ebx;        ; // rfp += iincr 
  596.     lp2_loop :
  597.         movq mm1, qword ptr [ecx] ;mm1 <= short bp[0..3]  
  598.          add edx, 0x10;        // rfp += 8; rfp += 8; twice
  599.  
  600.         movq mm2, qword ptr [ecx+8]  ;mm2 <= short bp[4..7]
  601.          paddsw mm1, mm0;    // bp[0..3] + [128,128,128,128]
  602.  
  603.         movq mm3, qword ptr [ecx+16] ;mm3 <= short bp[0..3]  
  604.          paddsw mm2, mm0;    // bp[4..7] + [128,128,128,128]
  605.  
  606.         movq mm4, qword ptr [ecx+24]  ;mm4 <= short bp[4..7]
  607.          packuswb mm1, mm2       ; short rfp[0..7] -> unsigned char rfp[ 0..7]
  608.  
  609.         paddsw mm3, mm0;    // bp[0..3] + [128,128,128,128]
  610.          paddsw mm4, mm0;    // bp[4..7] + [128,128,128,128]
  611.         
  612.         movq qword ptr [edx-16], mm1 ; store rfp[0..7]
  613.          packuswb mm3, mm4       ; short rfp[0..7] -> unsigned char rfp[ 0..7]
  614.  
  615.         add ecx, 0x20        ; dwbp += 16; dwbp+= 16 (twice)
  616.          add edx, ebx;        ; // rfp += iincr 
  617.  
  618.     // software pipelining -- store moved *after* the branch, to
  619.     // prevent 1-cycle stall
  620.  
  621.     //    movq qword ptr [edx-8], mm3 ; store rfp[0..7]
  622.     //     add edx, ebx;        ; // rfp += iincr 
  623.  
  624.         sub eax, 0x02        ; i -= 2
  625.          cmp eax, 0x00;        ; // compare ( i <> 0 )
  626.  
  627.         jg lp2_loopa        ; loop // end for ( i=0; i < 8; ++i )
  628.  
  629.     // post-loop clean-up write
  630.  
  631.         movq qword ptr [edx-8], mm3 ; store rfp[0..7]
  632.       } // end __asm 
  633.  
  634.  
  635.   } // end   if (addflag)
  636.  
  637.     __asm emms;  // restore processor state DO NOT COMMENT OUT !!!
  638. }
  639.  
  640.  
  641.  
  642. /* ISO/IEC 13818-2 section 7.8 */
  643. static void Decode_SNR_Macroblock(
  644.   int *SNRMBA, int *SNRMBAinc,
  645.   int MBA, int MBAmax,
  646.   int *dct_type)
  647. {
  648.   int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy; 
  649.   int slice_vert_pos_ext, quantizer_scale_code, comp, code;
  650.  
  651.   ld = &enhan;
  652.  
  653.   if (*SNRMBAinc==0)
  654.   {
  655.     if (!Show_Bits(23)) /* next_start_code */
  656.     {
  657.       next_start_code();
  658.       code = Show_Bits(32);
  659.  
  660.       if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  661.       {
  662.         /* only slice headers are allowed in picture_data */
  663.         if (!Quiet_Flag)
  664.           printf("SNR: Premature end of picture\n");
  665.         return;
  666.       }
  667.  
  668.       Flush_Buffer32();
  669.  
  670.       /* decode slice header (may change quantizer_scale) */
  671.       slice_vert_pos_ext = slice_header();
  672.  
  673.       /* decode macroblock address increment */
  674.       *SNRMBAinc = Get_macroblock_address_increment();
  675.  
  676.       /* set current location */
  677.       *SNRMBA =
  678.         ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
  679.  
  680.       *SNRMBAinc = 1; /* first macroblock in slice: not skipped */
  681.     }
  682.     else /* not next_start_code */
  683.     {
  684.       if (*SNRMBA>=MBAmax)
  685.       {
  686.         if (!Quiet_Flag)
  687.           printf("Too many macroblocks in picture\n");
  688.         return;
  689.       }
  690.  
  691.       /* decode macroblock address increment */
  692.       *SNRMBAinc = Get_macroblock_address_increment();
  693.     }
  694.   }
  695.  
  696.   if (*SNRMBA!=MBA)
  697.   {
  698.     /* streams out of sync */
  699.     if (!Quiet_Flag)
  700.       printf("Cant't synchronize streams\n");
  701.     return;
  702.   }
  703.  
  704.   if (*SNRMBAinc==1) /* not skipped */
  705.   {
  706.     macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
  707.       &dummy, &dummy, &dummy, &dummy, &dummy,
  708.       &SNRdct_type);
  709.  
  710.     if (SNRmacroblock_type & MACROBLOCK_PATTERN)
  711.       *dct_type = SNRdct_type;
  712.  
  713.     if (SNRmacroblock_type & MACROBLOCK_QUANT)
  714.     {
  715.       quantizer_scale_code = Get_Bits(5);
  716.       ld->quantizer_scale =
  717.         ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
  718.     }
  719.  
  720.     /* macroblock_pattern */
  721.     if (SNRmacroblock_type & MACROBLOCK_PATTERN)
  722.     {
  723.       SNRcoded_block_pattern = Get_coded_block_pattern();
  724.  
  725.       if (chroma_format==CHROMA422)
  726.         SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
  727.       else if (chroma_format==CHROMA444)
  728.         SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
  729.     }
  730.     else
  731.       SNRcoded_block_pattern = 0;
  732.  
  733.     /* decode blocks */
  734.     for (comp=0; comp<block_count; comp++)
  735.     {
  736.       Clear_Block(comp);
  737.  
  738.       if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
  739.         Decode_MPEG2_Non_Intra_Block(comp);
  740.     }
  741.   }
  742.   else /* SNRMBAinc!=1: skipped macroblock */
  743.   {
  744.     for (comp=0; comp<block_count; comp++)
  745.       Clear_Block(comp);
  746.   }
  747.  
  748.   ld = &base;
  749. }
  750.  
  751.  
  752.  
  753. /* IMPLEMENTATION: set scratch pad macroblock to zero */
  754. static void Clear_Block(int comp)
  755. {
  756.   //short *Block_Ptr;
  757.   long *Block_Ptr = (long *)ld->block[comp];
  758. //  int i;
  759.  
  760.    /* 0.16B22 optimization */
  761.   //for (i=0; i<32; i++)
  762.   //  Block_Ptr[ i ] = 0L;
  763.  
  764.   __asm
  765.   {
  766.       mov eax, dword ptr [ Block_Ptr ];
  767.        pxor mm0, mm0;
  768.  
  769.       movq [eax+0 ], mm0;
  770.       movq [eax+8 ], mm0;
  771.       movq [eax+16], mm0;
  772.       movq [eax+24], mm0;
  773.       movq [eax+32], mm0;
  774.       movq [eax+40], mm0;
  775.       movq [eax+48], mm0;
  776.       movq [eax+56], mm0;
  777.  
  778.       movq [eax+64], mm0;
  779.       movq [eax+72], mm0;
  780.       movq [eax+80], mm0;
  781.       movq [eax+88], mm0;
  782.       movq [eax+96], mm0;
  783.       movq [eax+104],mm0;
  784.       movq [eax+112],mm0;
  785.       movq [eax+120],mm0;
  786.   }
  787.  
  788.     __asm emms;  // restore processor state DO NOT COMMENT OUT !!!
  789.   //for (i=0; i<64; i++)
  790.   //  *Block_Ptr++ = 0;
  791. }
  792.  
  793.  
  794. /* SCALABILITY: add SNR enhancement layer block data to base layer */
  795. /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
  796. /*static void Sum_Block(
  797. int comp)
  798. {
  799.   short *Block_Ptr1, *Block_Ptr2;
  800.   int i;
  801.  
  802.   Block_Ptr1 = base.block[comp];
  803.   Block_Ptr2 = enhan.block[comp];
  804.  
  805.   for (i=0; i<64; i++)
  806.     *Block_Ptr1++ += *Block_Ptr2++;
  807. }*/
  808.  
  809.  
  810. /* limit coefficients to -2048..2047 */
  811. /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
  812. /*static void Saturate(
  813. short *Block_Ptr)
  814. {
  815.   int i, sum, val;
  816.  
  817.   sum = 0;
  818.  
  819.    ISO/IEC 13818-2 section 7.4.3: Saturation 
  820.   for (i=0; i<64; i++)
  821.   {
  822.     val = Block_Ptr[i];
  823.  
  824.     if (val>2047)
  825.       val = 2047;
  826.     else if (val<-2048)
  827.       val = -2048;
  828.  
  829.     Block_Ptr[i] = val;
  830.     sum+= val;
  831.   }
  832.  
  833.   /* ISO/IEC 13818-2 section 7.4.4: Mismatch control *//*
  834.   if ((sum&1)==0)
  835.     Block_Ptr[63]^= 1;
  836.  
  837. }
  838. */
  839.  
  840. /* reuse old picture buffers as soon as they are no longer needed 
  841.    based on life-time axioms of MPEG */
  842. static void Update_Picture_Buffers()
  843. {                           
  844.   int cc;              /* color component index */
  845.   unsigned char *tmp;  /* temporary swap pointer */
  846.  
  847.   for (cc=0; cc<3; cc++)
  848.   {
  849.     /* B pictures do not need to be save for future reference */
  850.     if (picture_coding_type==B_TYPE)
  851.     {
  852.       current_frame[cc] = auxframe[cc];
  853.     }
  854.     else
  855.     {
  856.       /* only update at the beginning of the coded frame */
  857.       if (!Second_Field)
  858.       {
  859.         tmp = forward_reference_frame[cc];
  860.  
  861.         /* the previously decoded reference frame is stored
  862.            coincident with the location where the backward 
  863.            reference frame is stored (backwards prediction is not
  864.            needed in P pictures) */
  865.         forward_reference_frame[cc] = backward_reference_frame[cc];
  866.         
  867.         /* update pointer for potential future B pictures */
  868.         backward_reference_frame[cc] = tmp;
  869.       }
  870.  
  871.       /* can erase over old backward reference frame since it is not used
  872.          in a P picture, and since any subsequent B pictures will use the 
  873.          previously decoded I or P frame as the backward_reference_frame */
  874.       current_frame[cc] = backward_reference_frame[cc];
  875.     }
  876.  
  877.     /* IMPLEMENTATION:
  878.        one-time folding of a line offset into the pointer which stores the
  879.        memory address of the current frame saves offsets and conditional 
  880.        branches throughout the remainder of the picture processing loop */
  881.     if (picture_structure==BOTTOM_FIELD)
  882.       current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
  883.   }
  884. }
  885.  
  886.  
  887.  
  888. /* ISO/IEC 13818-2 section 7.6 */
  889. static void motion_compensation(
  890. int MBA,
  891. int macroblock_type,
  892. int motion_type,
  893. int PMV[2][2][2],
  894. int motion_vertical_field_select[2][2],
  895. int dmvector[2],
  896. int stwtype,
  897. int dct_type)
  898. {
  899.   int bx, by;
  900.   int comp;
  901.  
  902.   /* derive current macroblock position within picture */
  903.   /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
  904.   bx = 16*(MBA%mb_width);
  905.   by = 16*(MBA/mb_width);
  906.  
  907.   /* motion compensation */
  908.   if (!(macroblock_type & MACROBLOCK_INTRA))
  909.     form_predictions(bx,by,macroblock_type,motion_type,PMV,
  910.       motion_vertical_field_select,dmvector,stwtype);
  911.  
  912.   /* copy or add block data into picture */
  913.   for (comp=0; comp<block_count; comp++)
  914.   {
  915.     myVideo->Idct( ld->block[comp] );
  916.     
  917.     /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
  918.     Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
  919.   }
  920.  
  921. }
  922.  
  923.  
  924.  
  925. /* ISO/IEC 13818-2 section 7.6.6 */
  926. static void skipped_macroblock(
  927. int dc_dct_pred[3],
  928. int PMV[2][2][2],
  929. int *motion_type,
  930. int motion_vertical_field_select[2][2],
  931. int *stwtype,
  932. int *macroblock_type )
  933. {
  934.   int comp;
  935.   
  936.   /* SCALABILITY: Data Paritioning */
  937. //snr  if (base.scalable_mode==SC_DP)
  938. //snr    ld = &base;
  939.  
  940.   for (comp=0; comp<block_count; comp++)
  941.     Clear_Block(comp);
  942.  
  943.  
  944.   /* reset intra_dc predictors */
  945.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  946.   dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  947.  
  948.   /* reset motion vector predictors */
  949.   /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  950.   if (picture_coding_type==P_TYPE)
  951.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  952.  
  953.   /* derive motion_type */
  954.   if (picture_structure==FRAME_PICTURE)
  955.     *motion_type = MC_FRAME;
  956.   else
  957.   {
  958.     *motion_type = MC_FIELD;
  959.  
  960.     /* predict from field of same parity */
  961.     /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
  962.        picture */
  963.     motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] = 
  964.       (picture_structure==BOTTOM_FIELD);
  965.   }
  966.  
  967.   /* skipped I are spatial-only predicted, */
  968.   /* skipped P and B are temporal-only predicted */
  969.   /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
  970.   *stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
  971.  
  972.  /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
  973.   *macroblock_type&= ~MACROBLOCK_INTRA;
  974.  
  975. }
  976.  
  977.  
  978.  
  979. /* return==-1 means go to next picture */
  980. /* the expression "start of slice" is used throughout the normative
  981.    body of the MPEG specification */
  982. static int start_of_slice(
  983. int MBAmax,
  984. int *MBA,
  985. int *MBAinc,
  986. int dc_dct_pred[3],
  987. int PMV[2][2][2] )
  988. {
  989.   unsigned int code;
  990.   int slice_vert_pos_ext;
  991.  
  992.   ld = &base;
  993.  
  994.   Fault_Flag = 0;
  995.  
  996.   next_start_code();
  997.   code = Show_Bits(32);
  998.  
  999.   if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  1000.   {
  1001.     /* only slice headers are allowed in picture_data */
  1002.     if (!Quiet_Flag)
  1003.       printf("start_of_slice(): Premature end of picture\n");
  1004.  
  1005.     return(-1);  /* trigger: go to next picture */
  1006.   }
  1007.  
  1008.   Flush_Buffer32(); 
  1009.  
  1010.   /* decode slice header (may change quantizer_scale) */
  1011.   slice_vert_pos_ext = slice_header();
  1012.  
  1013.  
  1014.   /* SCALABILITY: Data Partitioning */
  1015. //snr  if (base.scalable_mode==SC_DP)
  1016. //snr  {
  1017. //snr    ld = &enhan;
  1018. //snr    next_start_code();
  1019. //snr    code = Show_Bits(32);
  1020. //snr
  1021. //snr    if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  1022. //snr    {
  1023. //snr      /* only slice headers are allowed in picture_data */
  1024. //snr      if (!Quiet_Flag)
  1025. //snr        printf("DP: Premature end of picture\n");
  1026. //snr      return(-1);    /* trigger: go to next picture */
  1027. //snr    }
  1028. //snr
  1029. //snr    Flush_Buffer32();
  1030.  
  1031. //snr    /* decode slice header (may change quantizer_scale) */
  1032. //snr    slice_vert_pos_ext = slice_header();
  1033.  
  1034. //snr    if (base.priority_breakpoint!=1)
  1035. //snr      ld = &base;
  1036. //snr  }
  1037.  
  1038.   /* decode macroblock address increment */
  1039.   *MBAinc = Get_macroblock_address_increment();
  1040.  
  1041.   if (Fault_Flag) 
  1042.   {
  1043.     printf("start_of_slice(): MBAinc unsuccessful\n");
  1044.     return(0);   /* trigger: go to next slice */
  1045.   }
  1046.  
  1047.   /* set current location */
  1048.   /* NOTE: the arithmetic used to derive macroblock_address below is
  1049.    *       equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
  1050.    */
  1051.   *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
  1052.   *MBAinc = 1; /* first macroblock in slice: not skipped */
  1053.  
  1054.   /* reset all DC coefficient and motion vector predictors */
  1055.   /* reset all DC coefficient and motion vector predictors */
  1056.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  1057.   dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  1058.   
  1059.   /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1060.   PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1061.   PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1062.  
  1063.   /* successfull: trigger decode macroblocks in slice */
  1064.   return(1);
  1065. }
  1066.  
  1067.  
  1068. /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
  1069.  
  1070. static int decode_macroblock(
  1071. int *macroblock_type,
  1072. int *stwtype,
  1073. int *stwclass,
  1074. int *motion_type,
  1075. int *dct_type,
  1076. int PMV[2][2][2],
  1077. int dc_dct_pred[3],
  1078. int motion_vertical_field_select[2][2],
  1079. int dmvector[2] )
  1080. {
  1081.   /* locals */
  1082.   int quantizer_scale_code; 
  1083.   int comp;
  1084.  
  1085.   int motion_vector_count; 
  1086.   int mv_format; 
  1087.   int dmv; 
  1088.   int mvscale;
  1089.   int coded_block_pattern;
  1090.  
  1091.   /* SCALABILITY: Data Patitioning */
  1092. //snr  if (base.scalable_mode==SC_DP)
  1093. //snr  {
  1094. //snr    if (base.priority_breakpoint<=2)
  1095. //snr      ld = &enhan;
  1096. //snr    else
  1097. //snr      ld = &base;
  1098. //snr  }
  1099.  
  1100.   /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  1101.   macroblock_modes(macroblock_type, stwtype, stwclass,
  1102.     motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
  1103.     dct_type);
  1104.  
  1105.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1106.  
  1107.   if (*macroblock_type & MACROBLOCK_QUANT)
  1108.   {
  1109.     quantizer_scale_code = Get_Bits(5);
  1110.  
  1111. #ifdef TRACE
  1112.     if (Trace_Flag)
  1113.     {
  1114.       printf("quantiser_scale_code (");
  1115.       Print_Bits(quantizer_scale_code,5,5);
  1116.       printf("): %d\n",quantizer_scale_code);
  1117.     }
  1118. #endif /* TRACE */
  1119.  
  1120.     /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
  1121.     if (ld->MPEG2_Flag)
  1122.       ld->quantizer_scale =
  1123.       ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] 
  1124.        : (quantizer_scale_code << 1);
  1125.     else
  1126.       ld->quantizer_scale = quantizer_scale_code;
  1127.  
  1128.   }
  1129.  
  1130.   /* motion vectors */
  1131.  
  1132.  
  1133.   /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
  1134.  
  1135.   /* decode forward motion vectors */
  1136.   if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD) 
  1137.     || ((*macroblock_type & MACROBLOCK_INTRA) 
  1138.     && concealment_motion_vectors))
  1139.   {
  1140.     if (ld->MPEG2_Flag)
  1141.       motion_vectors(PMV,dmvector,motion_vertical_field_select,
  1142.         0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
  1143.         dmv,mvscale);
  1144.     else
  1145.       motion_vector(PMV[0][0],dmvector,
  1146.       forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
  1147.   }
  1148.  
  1149.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1150.  
  1151.   /* decode backward motion vectors */
  1152.   if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
  1153.   {
  1154.     if (ld->MPEG2_Flag)
  1155.       motion_vectors(PMV,dmvector,motion_vertical_field_select,
  1156.         1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
  1157.         mvscale);
  1158.     else
  1159.       motion_vector(PMV[0][1],dmvector,
  1160.         backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
  1161.   }
  1162.  
  1163.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1164.  
  1165.   if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
  1166.     Flush_Buffer(1); /* remove marker_bit */
  1167.  
  1168.   /* macroblock_pattern */
  1169.   /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
  1170.   if (*macroblock_type & MACROBLOCK_PATTERN)
  1171.   {
  1172.     coded_block_pattern = Get_coded_block_pattern();
  1173.  
  1174.     if (chroma_format==CHROMA422)
  1175.     {
  1176.       /* coded_block_pattern_1 */
  1177.       coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2); 
  1178.  
  1179. #ifdef TRACE
  1180.        if (Trace_Flag)
  1181.        {
  1182.          printf("coded_block_pattern_1: ");
  1183.          Print_Bits(coded_block_pattern,2,2);
  1184.          printf(" (%d)\n",coded_block_pattern&3);
  1185.        }
  1186. #endif /* TRACE */
  1187.      }
  1188.      else if (chroma_format==CHROMA444)
  1189.      {
  1190.       /* coded_block_pattern_2 */
  1191.       coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6); 
  1192.  
  1193. #ifdef TRACE
  1194.       if (Trace_Flag)
  1195.       {
  1196.         printf("coded_block_pattern_2: ");
  1197.         Print_Bits(coded_block_pattern,6,6);
  1198.         printf(" (%d)\n",coded_block_pattern&63);
  1199.       }
  1200. #endif /* TRACE */
  1201.     }
  1202.   }
  1203.   else
  1204.     coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ? 
  1205.       (1<<block_count)-1 : 0;
  1206.  
  1207.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1208.  
  1209.   /* decode blocks */
  1210.   for (comp=0; comp<block_count; comp++)
  1211.   {
  1212.     Clear_Block(comp);
  1213.  
  1214.     if (coded_block_pattern & (1<<(block_count-1-comp)))
  1215.     {
  1216.       if (*macroblock_type & MACROBLOCK_INTRA)
  1217.       {
  1218.         if (ld->MPEG2_Flag)
  1219.           Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
  1220.         else
  1221.           Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
  1222.       }
  1223.       else
  1224.       {
  1225.         if (ld->MPEG2_Flag)
  1226.           Decode_MPEG2_Non_Intra_Block(comp);
  1227.         else
  1228.           Decode_MPEG1_Non_Intra_Block(comp);
  1229.       }
  1230.  
  1231.       if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1232.     }
  1233.   }
  1234.  
  1235.   if(picture_coding_type==D_TYPE)
  1236.   {
  1237.     /* remove end_of_macroblock (always 1, prevents startcode emulation) */
  1238.     /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
  1239.     marker_bit("D picture end_of_macroblock bit");
  1240.   }
  1241.  
  1242.   /* reset intra_dc predictors */
  1243.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  1244.   if (!(*macroblock_type & MACROBLOCK_INTRA))
  1245.     dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  1246.  
  1247.   /* reset motion vector predictors */
  1248.   if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
  1249.   {
  1250.     /* intra mb without concealment motion vectors */
  1251.     /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1252.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1253.     PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1254.   }
  1255.  
  1256.   /* special "No_MC" macroblock_type case */
  1257.   /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
  1258.   if ((picture_coding_type==P_TYPE) 
  1259.     && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
  1260.   {
  1261.     /* non-intra mb without forward mv in a P picture */
  1262.     /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1263.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1264.  
  1265.     /* derive motion_type */
  1266.     /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
  1267.     if (picture_structure==FRAME_PICTURE)
  1268.       *motion_type = MC_FRAME;
  1269.     else
  1270.     {
  1271.       *motion_type = MC_FIELD;
  1272.       /* predict from field of same parity */
  1273.       motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
  1274.     }
  1275.   }
  1276.  
  1277. //snr  if (*stwclass==4)
  1278. //snr  {
  1279. //snr    /* purely spatially predicted macroblock */
  1280. //snr    /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
  1281. //snr    PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1282. //snr    PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1283. //snr  }
  1284.  
  1285.   /* successfully decoded macroblock */
  1286.  
  1287.  
  1288.   return(1);
  1289.  
  1290. } /* decode_macroblock */
  1291.  
  1292.  
  1293.