home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Video / getpic.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-19  |  33.4 KB  |  1,250 lines

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