macroblock.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:173k
源码类别:

Audio

开发平台:

Visual C++

  1.   int level = 1;
  2.   int cbp = currMB->cbp;
  3.   SyntaxElement currSE;
  4.   Slice *currSlice = img->currentSlice;
  5.   DataPartition *dP;
  6.   int *partMap = assignSE2partition[currSlice->dp_mode];
  7.   int boff_x, boff_y;
  8.   int qp_per, qp_rem; 
  9.   int uv = pl - 1; 
  10.   int64 cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position            
  11.   int64 *cur_cbp;
  12.   Boolean lossless_qpprime = (Boolean) ((img->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
  13.   int (*InvLevelScale8x8)[8] = NULL;
  14.   // select scan type
  15.   const byte (*pos_scan8x8) = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8[0] : FIELD_SCAN8x8[0];
  16.   if (cbp & (1<<b8))  // are there any coefficients in the current block
  17.   {
  18.     if (pl)
  19.     {
  20.       qp_per  = qp_per_matrix[ currMB->qp_scaled[pl] ];
  21.       qp_rem  = qp_rem_matrix[ currMB->qp_scaled[pl] ];
  22.       cur_cbp = &currMB->cbp_blk;
  23.     }
  24.     else
  25.     {
  26.       qp_per  = qp_per_matrix[ currMB->qp_scaled[pl] ];
  27.       qp_rem  = qp_rem_matrix[ currMB->qp_scaled[pl] ];
  28.       cur_cbp = &currMB->cbp_blk_CbCr[uv];
  29.     }
  30.     InvLevelScale8x8 = IS_INTRA(currMB)? InvLevelScale8x8_Intra[pl][qp_rem] : InvLevelScale8x8_Inter[pl][qp_rem];
  31.     img->is_intra_block = IS_INTRA(currMB);
  32.     // === set offset in current macroblock ===
  33.     boff_x = (b8&0x01) << 3;
  34.     boff_y = (b8 >> 1) << 3;
  35.     tcoeffs = &img->mb_rres[pl][boff_y];
  36.     img->subblock_x = boff_x >> 2; // position for coeff_count ctx
  37.     img->subblock_y = boff_y >> 2; // position for coeff_count ctx
  38.     if (pl==PLANE_Y || IS_INDEPENDENT(img))  
  39.       currSE.context = LUMA_8x8;
  40.     else if (pl==PLANE_U)
  41.       currSE.context = CB_8x8;
  42.     else
  43.       currSE.context = CR_8x8;  
  44.     currSE.reading = readRunLevel_CABAC;
  45.     if(!lossless_qpprime)
  46.     {
  47.       // Read DC
  48.       currSE.type = ((img->is_intra_block == 1) ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER ); // Intra or Inter?
  49.       dP = &(currSlice->partArr[partMap[currSE.type]]);
  50. #if TRACE
  51.       if (pl==PLANE_Y)
  52.         sprintf(currSE.tracestring, "Luma8x8 DC sng ");
  53.       else if (pl==PLANE_U)
  54.         sprintf(currSE.tracestring, "Cb  8x8 DC sng "); 
  55.       else 
  56.         sprintf(currSE.tracestring, "Cr  8x8 DC sng "); 
  57. #endif        
  58.       dP->readSyntaxElement(&currSE,img,dP);
  59.       level = currSE.value1;
  60.       //============ decode =============
  61.       if (level != 0)    /* leave if len=1 */
  62.       {
  63.         *cur_cbp |= cbp_mask; 
  64.         pos_scan8x8 += 2 * (currSE.value2);
  65.         i = *pos_scan8x8++;
  66.         j = *pos_scan8x8++;
  67.         tcoeffs[j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6); // dequantization
  68.         // AC coefficients
  69.         currSE.type    = ((img->is_intra_block == 1) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
  70.         dP = &(currSlice->partArr[partMap[currSE.type]]);
  71.         for(k = 1;(k < 65) && (level != 0);k++)
  72.         {
  73. #if TRACE
  74.           if (pl==PLANE_Y)
  75.             sprintf(currSE.tracestring, "Luma8x8 sng ");
  76.           else if (pl==PLANE_U)
  77.             sprintf(currSE.tracestring, "Cb  8x8 sng "); 
  78.           else 
  79.             sprintf(currSE.tracestring, "Cr  8x8 sng "); 
  80. #endif
  81.           dP->readSyntaxElement(&currSE,img,dP);
  82.           level = currSE.value1;
  83.           //============ decode =============
  84.           if (level != 0)    /* leave if len=1 */
  85.           {
  86.             pos_scan8x8 += 2 * (currSE.value2);
  87.             i = *pos_scan8x8++;
  88.             j = *pos_scan8x8++;
  89.             tcoeffs[ j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6); // dequantization
  90.           }
  91.         }
  92.       }
  93.     }
  94.     else
  95.     {
  96.       for(k=0; (k < 65) && (level != 0);k++)
  97.       {
  98.         //============ read =============
  99.         /*
  100.         * make distinction between INTRA and INTER coded
  101.         * luminance coefficients
  102.         */
  103.         currSE.type    = ((img->is_intra_block == 1)
  104.           ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA) 
  105.           : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
  106. #if TRACE
  107.         if (pl==PLANE_Y)
  108.           sprintf(currSE.tracestring, "Luma8x8 sng ");
  109.         else if (pl==PLANE_U)
  110.           sprintf(currSE.tracestring, "Cb  8x8 sng "); 
  111.         else 
  112.           sprintf(currSE.tracestring, "Cr  8x8 sng "); 
  113. #endif
  114.         dP = &(currSlice->partArr[partMap[currSE.type]]);
  115.         currSE.reading = readRunLevel_CABAC;
  116.         dP->readSyntaxElement(&currSE,img,dP);
  117.         level = currSE.value1;
  118.         //============ decode =============
  119.         if (level != 0)    /* leave if len=1 */
  120.         {
  121.           pos_scan8x8 += 2 * (currSE.value2);
  122.           i = *pos_scan8x8++;
  123.           j = *pos_scan8x8++;
  124.           *cur_cbp |= cbp_mask; 
  125.           tcoeffs[j][boff_x + i] = level;
  126.         }
  127.       }
  128.     }
  129.   }
  130. }
  131. /*!
  132.  ************************************************************************
  133.  * brief
  134.  *    Data partitioning: Check if neighboring macroblock is needed for 
  135.  *    CAVLC context decoding, and disable current MB if data partition
  136.  *    is missing.
  137.  ************************************************************************
  138.  */
  139. static void check_dp_neighbors (Macroblock *currMB)
  140. {
  141.   PixelPos up, left;
  142.   getNeighbour(currMB, -1,  0, img->mb_size[1], &left);
  143.   getNeighbour(currMB,  0, -1, img->mb_size[1], &up);
  144.   if (IS_INTER (currMB) || (IS_INTRA (currMB) && !(active_pps->constrained_intra_pred_flag)) )
  145.   {
  146.     if (left.available)
  147.     {
  148.       currMB->dpl_flag |= img->mb_data[left.mb_addr].dpl_flag;
  149.     }
  150.     if (up.available)
  151.     {
  152.       currMB->dpl_flag |= img->mb_data[up.mb_addr].dpl_flag;
  153.     }
  154.   }
  155. }
  156. /*!
  157.  ************************************************************************
  158.  * brief
  159.  *    Get coded block pattern and coefficients (run/level)
  160.  *    from the NAL
  161.  ************************************************************************
  162.  */
  163. static void readCBPandCoeffsFromNAL(ImageParameters *img, Macroblock *currMB)
  164. {
  165.   int i,j,k;
  166.   int level;
  167.   int mb_nr = img->current_mb_nr;
  168.   int ii,jj;
  169.   int cbp;
  170.   SyntaxElement currSE;
  171.   Slice *currSlice = img->currentSlice;
  172.   DataPartition *dP = NULL;
  173.   int *partMap = assignSE2partition[currSlice->dp_mode];
  174.   int coef_ctr, i0, j0, b8;
  175.   int ll;
  176.   int block_x,block_y;
  177.   int start_scan;
  178.   int levarr[16], runarr[16], numcoeff;
  179.   
  180.   int qp_per    = qp_per_matrix[ (img->qp + img->bitdepth_luma_qp_scale) ];
  181.   int qp_rem    = qp_rem_matrix[ (img->qp + img->bitdepth_luma_qp_scale) ];
  182.   int smb       = ((img->type==SP_SLICE) && IS_INTER (currMB)) || (img->type == SI_SLICE && currMB->mb_type == SI4MB);
  183.   
  184.   int uv; 
  185.   int qp_per_uv[2];
  186.   int qp_rem_uv[2];
  187.   
  188.   int intra = IS_INTRA (currMB);
  189.   int temp[4];
  190.   
  191.   int b4;
  192.   int yuv = dec_picture->chroma_format_idc - 1;
  193.   int m6[4];
  194.   
  195.   int need_transform_size_flag;
  196.   Boolean lossless_qpprime = (Boolean) ((img->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
  197.   
  198.   int (*InvLevelScale4x4)[4] = NULL;
  199.   int (*InvLevelScale8x8)[8] = NULL;
  200.   // select scan type
  201.   const byte (*pos_scan8x8)[2] = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8 : FIELD_SCAN8x8;
  202.   const byte (*pos_scan4x4)[2] = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
  203.   const byte *pos_scan_4x4 = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN[0] : FIELD_SCAN[0];
  204.   if(img->type==SP_SLICE  && currMB->mb_type!=I16MB )
  205.     smb=1;
  206.   
  207.   // QPI
  208.   //init constants for every chroma qp offset
  209.   if (dec_picture->chroma_format_idc != YUV400)
  210.   {
  211.     for (i=0; i<2; i++)
  212.     {
  213.       qp_per_uv[i] = qp_per_matrix[ currMB->qp_scaled[i + 1] ];
  214.       qp_rem_uv[i] = qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
  215.     }
  216.   }
  217.   
  218.   // read CBP if not new intra mode
  219.   if (!IS_NEWINTRA (currMB))
  220.   {
  221.     //=====   C B P   =====
  222.     //---------------------
  223.     currSE.type = (IS_OLDINTRA (currMB) || currMB->mb_type == SI4MB || currMB->mb_type == I8MB) 
  224.       ? SE_CBP_INTRA
  225.       : SE_CBP_INTER;
  226.     
  227.     dP = &(currSlice->partArr[partMap[currSE.type]]);
  228.     if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
  229.     {
  230.       currSE.mapping = (IS_OLDINTRA (currMB) || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
  231.         ? linfo_cbp_intra
  232.         : linfo_cbp_inter;
  233.     }
  234.     else
  235.     {
  236.       currSE.reading = readCBP_CABAC;
  237.     }
  238.     
  239.     TRACE_STRING("coded_block_pattern");
  240.     dP->readSyntaxElement(&currSE, img, dP);
  241.     currMB->cbp = cbp = currSE.value1;
  242.     
  243.     
  244.     //============= Transform size flag for INTER MBs =============
  245.     //-------------------------------------------------------------
  246.     need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
  247.                                 (IS_DIRECT(currMB) && active_sps->direct_8x8_inference_flag) ||
  248.                                 (currMB->NoMbPartLessThan8x8Flag))
  249.                                 && currMB->mb_type != I8MB && currMB->mb_type != I4MB
  250.                                 && (currMB->cbp&15)
  251.                                 && img->Transform8x8Mode);
  252.     if (need_transform_size_flag)
  253.     {
  254.       currSE.type   =  SE_HEADER;
  255.       dP = &(currSlice->partArr[partMap[SE_HEADER]]);
  256.       currSE.reading = readMB_transform_size_flag_CABAC;
  257.       TRACE_STRING("transform_size_8x8_flag");
  258.       // read UVLC transform_size_8x8_flag
  259.       if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
  260.       {
  261.         currSE.len = 1;
  262.         readSyntaxElement_FLC(&currSE, dP->bitstream);
  263.       } 
  264.       else
  265.       {
  266.         dP->readSyntaxElement(&currSE,img,dP);
  267.       }
  268.       currMB->luma_transform_size_8x8_flag = currSE.value1;
  269.     }
  270.     //=====   DQUANT   =====
  271.     //----------------------
  272.     // Delta quant only if nonzero coeffs
  273.     if (cbp !=0)
  274.     {
  275.       read_delta_quant(&currSE, dP, currSlice, img, currMB, partMap, (IS_INTER (currMB)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
  276.       if (currSlice->dp_mode)
  277.       {
  278.         if (IS_INTER (currMB) && currSlice->dpC_NotPresent ) 
  279.           currMB->dpl_flag = 1;
  280.         if( intra && currSlice->dpB_NotPresent )
  281.         {
  282.           currMB->ei_flag = 1;
  283.           currMB->dpl_flag = 1;
  284.         }
  285.         // check for prediction from neighbours
  286.         check_dp_neighbors (currMB);
  287.         if (currMB->dpl_flag)
  288.         {
  289.           cbp = 0; 
  290.           currMB->cbp = cbp;
  291.         }
  292.       }
  293.     }
  294.   }
  295.   else
  296.   {
  297.     cbp = currMB->cbp;
  298.   }
  299.   
  300.   memset(&img->cof[0][0][0], 0, MB_PIXELS * sizeof(int)); // reset luma coeffs   
  301.   memset(&img->fcf[0][0][0], 0, MB_PIXELS * sizeof(int)); // reset luma coeffs   
  302.     
  303.   if (IS_NEWINTRA (currMB)) // read DC coeffs for new intra modes
  304.   {
  305.     read_delta_quant(&currSE, dP, currSlice, img, currMB, partMap, SE_DELTA_QUANT_INTRA);
  306.     for (j=0;j<BLOCK_SIZE;j++)
  307.       memset(&img->ipredmode[img->block_y+j][img->block_x], DC_PRED,  BLOCK_SIZE * sizeof(byte));
  308.         
  309.     if (currSlice->dp_mode)
  310.     {  
  311.       if (currSlice->dpB_NotPresent)
  312.       {
  313.         currMB->ei_flag  = 1;
  314.         currMB->dpl_flag = 1;
  315.       }
  316.       check_dp_neighbors (currMB);
  317.       if (currMB->dpl_flag)
  318.       {
  319.         currMB->cbp = cbp = 0; 
  320.       }
  321.     }
  322.     if (!currMB->dpl_flag)
  323.     {
  324.       pos_scan_4x4 = pos_scan4x4[0];
  325.       if (active_pps->entropy_coding_mode_flag == UVLC)
  326.       {
  327.         readCoeff4x4_CAVLC(currMB, img, LUMA_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
  328.         for(k = 0; k < numcoeff; k++)
  329.         {
  330.           if (levarr[k] != 0)                     // leave if len=1
  331.           {
  332.             pos_scan_4x4 += 2 * runarr[k];
  333.             i0 = ((*pos_scan_4x4++) << 2);
  334.             j0 = ((*pos_scan_4x4++) << 2);
  335.             img->cof[0][j0][i0] = levarr[k];// add new intra DC coeff
  336.             img->fcf[0][j0][i0] = levarr[k];// add new intra DC coeff
  337.           }
  338.         }
  339.       }
  340.       else
  341.       {
  342.         currSE.type = SE_LUM_DC_INTRA;
  343.         dP = &(currSlice->partArr[partMap[currSE.type]]);
  344.         currSE.context      = LUMA_16DC;
  345.         currSE.type         = SE_LUM_DC_INTRA;
  346.         img->is_intra_block = 1;
  347.         if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
  348.         {
  349.           currSE.mapping = linfo_levrun_inter;
  350.         }
  351.         else
  352.         {
  353.           currSE.reading = readRunLevel_CABAC;
  354.         }
  355.         level = 1;                            // just to get inside the loop
  356.         for(k=0;(k<17) && (level!=0);k++)
  357.         {
  358. #if TRACE
  359.           snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
  360. #endif
  361.           dP->readSyntaxElement(&currSE, img, dP);
  362.           level = currSE.value1;
  363.           if (level != 0)                     // leave if len=1
  364.           {
  365.             pos_scan_4x4 += (2 * currSE.value2);
  366.             i0 = ((*pos_scan_4x4++) << 2);
  367.             j0 = ((*pos_scan_4x4++) << 2);
  368.             img->cof[0][j0][i0] = level;// add new intra DC coeff
  369.             img->fcf[0][j0][i0] = level;// add new intra DC coeff
  370.           }
  371.         }
  372.       }
  373.       if(!lossless_qpprime)
  374.         itrans_2(img, currMB, PLANE_Y);// transform new intra DC
  375.     }
  376.   }
  377.   update_qp(img, currMB, img->qp);
  378.   
  379.   qp_per = qp_per_matrix[ currMB->qp_scaled[img->colour_plane_id] ];
  380.   qp_rem = qp_rem_matrix[ currMB->qp_scaled[img->colour_plane_id] ];
  381.   //init quant parameters for chroma 
  382.   if (dec_picture->chroma_format_idc != YUV400)
  383.   {
  384.     for(i=0; i < 2; i++)
  385.     {
  386.       qp_per_uv[i] = qp_per_matrix[ currMB->qp_scaled[i + 1] ];
  387.       qp_rem_uv[i] = qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
  388.     }
  389.   }
  390.   InvLevelScale4x4 = intra? InvLevelScale4x4_Intra[img->colour_plane_id][qp_rem] : InvLevelScale4x4_Inter[img->colour_plane_id][qp_rem];
  391.   InvLevelScale8x8 = intra? InvLevelScale8x8_Intra[img->colour_plane_id][qp_rem] : InvLevelScale8x8_Inter[img->colour_plane_id][qp_rem];
  392.     
  393.   // luma coefficients
  394.   if (active_pps->entropy_coding_mode_flag == UVLC)
  395.   {
  396.     start_scan = IS_NEWINTRA(currMB) ? 1 : 0;
  397.     if (!lossless_qpprime)
  398.     {
  399.       if (!currMB->luma_transform_size_8x8_flag) // 4x4 transform
  400.       {
  401.         if (cbp)
  402.         {
  403.           int cur_context = IS_NEWINTRA(currMB) ? LUMA_INTRA16x16AC : LUMA;
  404.           for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  405.           {
  406.             for (block_x=0; block_x < 4; block_x += 2)
  407.             {
  408.               b8 = (block_y + (block_x >> 1));
  409.               if (cbp & (1 << b8))  // test if the block contains any coefficients
  410.               {
  411.                 for (j=block_y; j < block_y + 2; j++)
  412.                 {
  413.                   for (i=block_x; i < block_x + 2; i++)
  414.                   {
  415.                     readCoeff4x4_CAVLC(currMB, img, cur_context, i, j, levarr, runarr, &numcoeff);
  416.                     pos_scan_4x4 = pos_scan4x4[start_scan];
  417.                     for (k = 0; k < numcoeff; k++)
  418.                     {
  419.                       if (levarr[k] != 0)
  420.                       {
  421.                         pos_scan_4x4 += (runarr[k] << 1);
  422.                         i0 = *pos_scan_4x4++;
  423.                         j0 = *pos_scan_4x4++;
  424.                         // inverse quant for 4x4 transform only
  425.                         currMB->cbp_blk |= (int64) 1 << ((j<<2) + i);
  426.                         img->cof[0][(j<<2) + j0][(i<<2) + i0]= rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per, 4);
  427.                         img->fcf[0][(j<<2) + j0][(i<<2) + i0]= levarr[k];
  428.                       }
  429.                     }
  430.                   }
  431.                 }
  432.               }
  433.               else
  434.               {
  435.                 for (j=block_y; j < block_y + 2; j++)
  436.                 {
  437.                   memset(&img->nz_coeff[mb_nr][0][j][block_x], 0, 2 * sizeof(int));
  438.                 }
  439.               }
  440.             }
  441.           }
  442.         }
  443.         else
  444.         {
  445.           memset(&img->nz_coeff[mb_nr][0][0][0], 0, BLOCK_SIZE * BLOCK_SIZE * sizeof(int));
  446.         }
  447.       }
  448.       else // 8x8 transform
  449.       {
  450.         if (cbp)
  451.         {
  452.           int cur_context = IS_NEWINTRA(currMB) ? LUMA_INTRA16x16AC : LUMA;
  453.           for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  454.           {
  455.             for (block_x=0; block_x < 4; block_x += 2)
  456.             {
  457.               b8 = 2*(block_y>>1) + (block_x>>1);
  458.               if (cbp & (1<<b8))  /* are there any coeff in current block at all */
  459.               {
  460.                 for (j=block_y; j < block_y+2; j++)
  461.                 {
  462.                   for (i=block_x; i < block_x+2; i++)
  463.                   {
  464.                     readCoeff4x4_CAVLC(currMB, img, cur_context, i, j, levarr, runarr, &numcoeff);
  465.                     coef_ctr = start_scan - 1;
  466.                     for (k = 0; k < numcoeff; k++)
  467.                     {
  468.                       if (levarr[k] != 0)
  469.                       {
  470.                         coef_ctr += runarr[k]+1;
  471.                         // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
  472.                         //as containing coefficients
  473.                         currMB->cbp_blk  |= 51 << ((block_y<<2) + block_x);
  474.                         b4 = (coef_ctr << 2) + 2*(j - block_y)+(i - block_x);
  475.                         i0 = pos_scan8x8[b4][0];
  476.                         j0 = pos_scan8x8[b4][1];
  477.                         img->mb_rres[0][block_y*4 +j0][block_x*4 +i0] = rshift_rnd_sf((levarr[k] * InvLevelScale8x8[j0][i0])<<qp_per, 6); // dequantization
  478.                       }
  479.                     }//else (!currMB->luma_transform_size_8x8_flag)
  480.                   }
  481.                 }
  482.               }
  483.               else
  484.               {
  485.                 for (j=block_y; j < block_y+2; j++)
  486.                 {
  487.                   memset(&img->nz_coeff[mb_nr][0][j][block_x], 0, 2 * sizeof(int));
  488.                 }
  489.               }
  490.             }
  491.           }
  492.         }
  493.         else
  494.         {
  495.           memset(&img->nz_coeff[mb_nr][0][0][0], 0, BLOCK_SIZE * BLOCK_SIZE * sizeof(int));
  496.         }
  497.       }
  498.     }
  499.     else // if (!lossless_qpprime)
  500.     {
  501.       int cur_context = IS_NEWINTRA(currMB) ? LUMA_INTRA16x16AC : LUMA;
  502.       for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  503.       {
  504.         for (block_x=0; block_x < 4; block_x += 2)
  505.         {
  506.           b8 = 2*(block_y>>1) + (block_x>>1);
  507.           for (j=block_y; j < block_y+2; j++)
  508.           {
  509.             for (i=block_x; i < block_x+2; i++)
  510.             {
  511.               if (cbp & (1<<b8))  /* are there any coeff in current block at all */
  512.               {
  513.                 readCoeff4x4_CAVLC(currMB, img, cur_context, i, j, levarr, runarr, &numcoeff);
  514.                 coef_ctr = start_scan - 1;
  515.                 if (!currMB->luma_transform_size_8x8_flag) // inverse quant for 4x4 transform
  516.                 {
  517.                   for (k = 0; k < numcoeff; k++)
  518.                   {
  519.                     if (levarr[k] != 0)
  520.                     {
  521.                       coef_ctr += runarr[k]+1;
  522.                       i0=pos_scan4x4[coef_ctr][0];
  523.                       j0=pos_scan4x4[coef_ctr][1];
  524.                       currMB->cbp_blk      |= (int64) 1 << ((j<<2) + i);
  525.                       img->cof[0][(j<<2) + j0][(i<<2) + i0]= levarr[k];
  526.                       img->fcf[0][(j<<2) + j0][(i<<2) + i0]= levarr[k];
  527.                     }
  528.                   }
  529.                 }
  530.                 else // inverse quant for 8x8 transform
  531.                 {
  532.                   int iz, jz;
  533.                   for (k = 0; k < numcoeff; k++)
  534.                   {
  535.                     if (levarr[k] != 0)
  536.                     {
  537.                       coef_ctr += runarr[k]+1;
  538.                       // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
  539.                       //as containing coefficients
  540.                       currMB->cbp_blk  |= 51 << ((block_y<<2) + block_x);
  541.                       b4 = 2*(j-block_y)+(i-block_x);
  542.                       iz=pos_scan8x8[coef_ctr*4+b4][0];
  543.                       jz=pos_scan8x8[coef_ctr*4+b4][1];
  544.                       img->mb_rres[0][block_y*4 +jz][block_x*4 +iz] = levarr[k];
  545.                     }
  546.                   }
  547.                 }//else (!currMB->luma_transform_size_8x8_flag)
  548.               }
  549.               else
  550.               {
  551.                 img->nz_coeff[mb_nr][0][j][i] = 0;
  552.               }
  553.             }
  554.           }
  555.         }
  556.       }
  557.     }
  558.   } // VLC
  559.   else
  560.   {
  561.     //======= Other Modes & CABAC ========
  562.     //------------------------------------          
  563.     if(currMB->luma_transform_size_8x8_flag)
  564.     {
  565.       for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  566.       {
  567.         for (b8 = block_y; b8 < block_y + 2; b8 += 1)
  568.         {      
  569.           readLumaCoeff8x8_CABAC(img, currMB, PLANE_Y, b8); //======= 8x8 trannsform size & CABAC ========
  570.         }
  571.       }
  572.     }
  573.     else
  574.     {
  575.       if(!lossless_qpprime)
  576.       {
  577.         if (cbp)
  578.         {
  579.           start_scan = IS_NEWINTRA (currMB)? 1 : 0;          
  580.           img->is_intra_block = intra;
  581.           currSE.context = (IS_NEWINTRA(currMB) ? LUMA_16AC : LUMA_4x4);
  582.           for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  583.           {
  584.             for (block_x=0; block_x < 4; block_x += 2)
  585.             {
  586.               b8 = 2*(block_y>>1) + (block_x>>1);
  587.               if (cbp & (1<<b8))  // are there any coeff in current block at all
  588.               {
  589.                 for (j = (block_y<<2); j < (block_y<<2) + 8; j += 4)
  590.                 {
  591.                   img->subblock_y = (j>>2); // position for coeff_count ctx
  592.                   for (i = block_x; i < block_x + 2; i++)
  593.                   {
  594.                     img->subblock_x = i; // position for coeff_count ctx
  595.                     pos_scan_4x4 = pos_scan4x4[start_scan];
  596.                     level    = 1;
  597.                     for(k=start_scan;(k<17) && (level!=0);k++)
  598.                     {
  599.                       /*
  600.                       * make distinction between INTRA and INTER coded
  601.                       * luminance coefficients
  602.                       */
  603.                       currSE.type = (img->is_intra_block 
  604.                         ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA) 
  605.                         : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));                                      
  606. #if TRACE
  607.                       sprintf(currSE.tracestring, "Luma sng ");
  608. #endif
  609.                       dP = &(currSlice->partArr[partMap[currSE.type]]);
  610.                       if (dP->bitstream->ei_flag)  
  611.                         currSE.mapping = linfo_levrun_inter;
  612.                       else                                                     
  613.                         currSE.reading = readRunLevel_CABAC;
  614.                       dP->readSyntaxElement(&currSE,img,dP);
  615.                       level = currSE.value1;
  616.                       if (level != 0)    /* leave if len=1 */
  617.                       {
  618.                         pos_scan_4x4 += 2 * currSE.value2;
  619.                         
  620.                         i0 = *pos_scan_4x4++;
  621.                         j0 = *pos_scan_4x4++;
  622.                         currMB->cbp_blk |= (int64)1 << (j + i) ;
  623.                         img->cof[0][j + j0][(i<<2) + i0]= rshift_rnd_sf((level * InvLevelScale4x4[j0][i0]) << qp_per, 4);
  624.                         img->fcf[0][j + j0][(i<<2) + i0]= level;
  625.                       }
  626.                     }
  627.                   }
  628.                 }
  629.               }
  630.             }
  631.           }
  632.         }
  633.       }
  634.       else
  635.       {        
  636.         start_scan = IS_NEWINTRA (currMB)? 1 : 0;              
  637.         img->is_intra_block = intra;
  638.         currSE.context = (IS_NEWINTRA(currMB) ? LUMA_16AC : LUMA_4x4);
  639.         for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  640.         {
  641.           for (block_x=0; block_x < 4; block_x += 2)
  642.           {      
  643.             b8 = 2*(block_y>>1) + (block_x>>1);
  644.             for (j = block_y; j < block_y + 2; j++)
  645.             {
  646.               img->subblock_y = j; // position for coeff_count ctx
  647.               for (i = block_x; i < block_x + 2; i++)
  648.               {
  649.                 img->subblock_x = i; // position for coeff_count ctx
  650.                 if (cbp & (1<<b8))  // are there any coeff in current block at all
  651.                 {
  652.                   coef_ctr = start_scan - 1;
  653.                   level    = 1;
  654.                   for(k=start_scan;(k<17) && (level!=0);k++)
  655.                   {
  656.                     /*
  657.                     * make distinction between INTRA and INTER coded
  658.                     * luminance coefficients
  659.                     */
  660.                     
  661.                     currSE.type    = (img->is_intra_block 
  662.                       ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA) 
  663.                       : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));                                      
  664. #if TRACE
  665.                     sprintf(currSE.tracestring, "Luma sng ");
  666. #endif
  667.                     dP = &(currSlice->partArr[partMap[currSE.type]]);
  668.                     if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)  
  669.                       currSE.mapping = linfo_levrun_inter;
  670.                     else                                                     
  671.                       currSE.reading = readRunLevel_CABAC;
  672.                     dP->readSyntaxElement(&currSE,img,dP);
  673.                     level = currSE.value1;
  674.                     if (level != 0)    /* leave if len=1 */
  675.                     {
  676.                       coef_ctr += currSE.value2+1;
  677.                       i0=pos_scan4x4[coef_ctr][0];
  678.                       j0=pos_scan4x4[coef_ctr][1];
  679.                       currMB->cbp_blk |= (int64)1 << ((j<<2) + i) ;
  680.                       img->cof[0][(j<<2) + j0][(i<<2) + i0] = level;
  681.                       img->fcf[0][(j<<2) + j0][(i<<2) + i0] = level;
  682.                     }
  683.                   }
  684.                 }
  685.               }
  686.             }
  687.           }
  688.         }
  689.       }
  690.     }
  691.   }
  692.   if ( active_sps->chroma_format_idc==YUV444 && !IS_INDEPENDENT(img) ) 
  693.   {
  694.     for (uv = 0; uv < 2; uv++ )
  695.     {
  696.       memset(&img->cof[uv + 1][0][0], 0, MB_PIXELS * sizeof(int));
  697.       memset(&img->fcf[uv + 1][0][0], 0, MB_PIXELS * sizeof(int));
  698.       /*----------------------16x16DC Luma_Add----------------------*/
  699.       if (IS_NEWINTRA (currMB)) // read DC coeffs for new intra modes       
  700.       {
  701.         for (i=0;i<BLOCK_SIZE;i++)
  702.           for (j=0;j<BLOCK_SIZE;j++)
  703.             img->ipredmode[img->block_y+j][img->block_x+i]=DC_PRED;
  704.         if (active_pps->entropy_coding_mode_flag == UVLC)
  705.         {
  706.           if (uv == 0)
  707.             readCoeff4x4_CAVLC(currMB, img, CB_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
  708.           else
  709.             readCoeff4x4_CAVLC(currMB, img, CR_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
  710.           coef_ctr=-1;
  711.           level = 1;                            // just to get inside the loop
  712.           for(k = 0; k < numcoeff; k++)
  713.           {
  714.             if (levarr[k] != 0)                     // leave if len=1
  715.             {
  716.               coef_ctr += runarr[k] + 1;
  717.               i0=pos_scan4x4[coef_ctr][0];
  718.               j0=pos_scan4x4[coef_ctr][1];
  719.               img->cof[uv + 1][j0<<2][i0<<2] = levarr[k];// add new intra DC coeff
  720.               img->fcf[uv + 1][j0<<2][i0<<2] = levarr[k];// add new intra DC coeff
  721.             } //if leavarr[k]
  722.           } //k loop
  723.         } //UVLC
  724.         else // else UVLC
  725.         {              
  726.           currSE.type = SE_LUM_DC_INTRA;
  727.           dP = &(currSlice->partArr[partMap[currSE.type]]);
  728.           if (uv==0)
  729.             currSE.context   = CB_16DC; 
  730.           else
  731.             currSE.context   = CR_16DC; 
  732.           if( IS_INDEPENDENT(img) )
  733.             currSE.context   = LUMA_16DC; 
  734.           currSE.type         = SE_LUM_DC_INTRA;
  735.           img->is_intra_block = 1;
  736.           if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
  737.           {
  738.             currSE.mapping = linfo_levrun_inter;
  739.           }
  740.           else
  741.           {
  742.             currSE.reading = readRunLevel_CABAC;
  743.           }
  744.           coef_ctr = -1;
  745.           level = 1;                            // just to get inside the loop
  746.           for(k=0;(k<17) && (level!=0);k++)
  747.           {
  748. #if TRACE
  749.             if (uv == 0)
  750.               snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Cb   16x16 "); 
  751.             else
  752.               snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Cr   16x16 ");
  753. #endif
  754.             dP->readSyntaxElement(&currSE,img,dP);
  755.             level = currSE.value1;
  756.             //run   = currSE.value2;
  757.             //len   = currSE.len;
  758.             if (level != 0)                     // leave if len=1
  759.             {
  760.               coef_ctr += currSE.value2 + 1;
  761.               i0=pos_scan4x4[coef_ctr][0];
  762.               j0=pos_scan4x4[coef_ctr][1];
  763.               img->cof[uv + 1][j0<<2][i0<<2] = level;
  764.               img->fcf[uv + 1][j0<<2][i0<<2] = level;
  765.             }                        
  766.           } //k loop
  767.         } // else UVLC
  768.         if(!lossless_qpprime)
  769.         {
  770.           itrans_2(img, currMB, (ColorPlane) (uv + 1)); // transform new intra DC
  771.         }
  772.       } //IS_NEWINTRA
  773.       update_qp(img, currMB, img->qp);
  774.       qp_per    = qp_per_matrix[ (img->qp + img->bitdepth_luma_qp_scale) ];
  775.       qp_rem    = qp_rem_matrix[ (img->qp + img->bitdepth_luma_qp_scale) ];
  776.       //init constants for every chroma qp offset
  777.       qp_per_uv[uv] = qp_per_matrix[ (currMB->qpc[uv] + img->bitdepth_chroma_qp_scale) ];
  778.       qp_rem_uv[uv] = qp_rem_matrix[ (currMB->qpc[uv] + img->bitdepth_chroma_qp_scale) ];
  779.       InvLevelScale4x4 = intra? InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  780.       InvLevelScale8x8 = intra? InvLevelScale8x8_Intra[uv + 1][qp_rem_uv[uv]] : InvLevelScale8x8_Inter[uv + 1][qp_rem_uv[uv]];
  781.       // luma_add coefficients
  782.       for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  783.       {
  784.         for (block_x=0; block_x < 4; block_x += 2)
  785.         {
  786.           b8 = 2*(block_y>>1) + (block_x>>1);         
  787.           if (active_pps->entropy_coding_mode_flag == UVLC)
  788.           {
  789.             for (j=block_y; j < block_y+2; j++)
  790.             {
  791.               for (i=block_x; i < block_x+2; i++)
  792.               {
  793.                 ii = block_x >> 1;
  794.                 jj = block_y >> 1;
  795.                 b8 = 2 * jj + ii;
  796.                 if (cbp & (1<<b8))  /* are there any coeff in current block at all */
  797.                 {
  798.                   if (uv==0)
  799.                     readCoeff4x4_CAVLC(currMB, img, (IS_NEWINTRA(currMB) ? CB_INTRA16x16AC : CB), i, j, levarr, runarr, &numcoeff);
  800.                   else
  801.                     readCoeff4x4_CAVLC(currMB, img, (IS_NEWINTRA(currMB) ? CR_INTRA16x16AC : CR), i, j, levarr, runarr, &numcoeff);
  802.                   start_scan = IS_NEWINTRA(currMB) ? 1 : 0;
  803.                   coef_ctr = start_scan - 1;
  804.                   if(!lossless_qpprime)
  805.                   {
  806.                     if (!currMB->luma_transform_size_8x8_flag) // 4x4 transform
  807.                     {
  808.                       for (k = 0; k < numcoeff; k++)
  809.                       {
  810.                         if (levarr[k] != 0)
  811.                         {
  812.                           coef_ctr += runarr[k]+1;
  813.                           i0 = pos_scan4x4[coef_ctr][0];
  814.                           j0 = pos_scan4x4[coef_ctr][1];
  815.                           // inverse quant for 4x4 transform only
  816.                           currMB->cbp_blk_CbCr[uv] |= (int64) 1 << ((j<<2) + i);
  817.                           img->cof[uv + 1][(j << 2) + j0][(i << 2) + i0] = rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv],4);          
  818.                           img->fcf[uv + 1][(j << 2) + j0][(i << 2) + i0] = levarr[k];
  819.                         } //levarr[k] != 0
  820.                       }//k loop
  821.                     } //4x4
  822.                     else //8x8
  823.                     {
  824.                       int iz, jz;
  825.                       for (k = 0; k < numcoeff; k++)
  826.                       {
  827.                         if (levarr[k] != 0)
  828.                         {
  829.                           coef_ctr += runarr[k]+1;
  830.                           // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
  831.                           //as containing coefficients
  832.                           currMB->cbp_blk_CbCr[uv]  |= 51 << ((block_y<<2) + block_x);
  833.                           b4 = 2*(j - block_y)+(i - block_x);
  834.                           iz = pos_scan8x8[(coef_ctr << 2) + b4][0];
  835.                           jz = pos_scan8x8[(coef_ctr << 2) + b4][1];
  836.                           img->mb_rres[uv+1][block_y*4 +jz][block_x*4 +iz] = rshift_rnd_sf((levarr[k]*InvLevelScale8x8[jz][iz])<<qp_per_uv[uv], 6); // dequantization 444_TEMP_NOTE 
  837.                         } //if levarr
  838.                       }//8x8
  839.                     }//k loop
  840.                   } // !lossless
  841.                   else //lossless
  842.                   {
  843.                     if (!currMB->luma_transform_size_8x8_flag) // inverse quant for 4x4 transform
  844.                     {
  845.                       for (k = 0; k < numcoeff; k++)
  846.                       {
  847.                         if (levarr[k] != 0)
  848.                         {
  849.                           coef_ctr += runarr[k]+1;
  850.                           i0=pos_scan4x4[coef_ctr][0];
  851.                           j0=pos_scan4x4[coef_ctr][1];
  852.                           currMB->cbp_blk_CbCr[uv] |= (int64) 1 << ((j<<2) + i);
  853.                           img->cof[uv + 1][(j << 2) + j0][(i << 2) + i0] = levarr[k];
  854.                           img->fcf[uv + 1][(j << 2) + j0][(i << 2) + i0] = levarr[k];
  855.                         } //levarr[k]
  856.                       } //k loop
  857.                     } //4x4
  858.                     else //8x8
  859.                     {
  860.                       int iz, jz;
  861.                       for (k = 0; k < numcoeff; k++)
  862.                       {
  863.                         if (levarr[k] != 0)
  864.                         {
  865.                           coef_ctr += runarr[k]+1;
  866.                           // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
  867.                           //as containing coefficients
  868.                           currMB->cbp_blk_CbCr[uv]  |= 51 << ((block_y<<2) + block_x);
  869.                           b4 = 2*(j-block_y)+(i-block_x);
  870.                           iz=pos_scan8x8[coef_ctr*4+b4][0];
  871.                           jz=pos_scan8x8[coef_ctr*4+b4][1];
  872.                           img->mb_rres[uv+1][block_y*4 +jz][block_x*4 +iz] = levarr[k];
  873.                         } //levarr[k]
  874.                       } //k loop
  875.                     } //8x8
  876.                   }// loseless
  877.                 } //if (cbp & (1<<b8))                                
  878.                 else //!(cbp & (1<<b8))
  879.                 {
  880.                   img->nz_coeff[mb_nr][uv + 1][j][i] = 0;
  881.                 } //!(cbp & (1<<b8))
  882.               } //i=block_x
  883.             } //j=block_y
  884.           } //UVCL
  885.           else // CABAC
  886.           {            
  887.             if(currMB->luma_transform_size_8x8_flag) 
  888.             {
  889.               readLumaCoeff8x8_CABAC(img, currMB, (ColorPlane) (PLANE_U + uv), b8); //======= 8x8 trannsform size & CABAC ========
  890.             }
  891.             else //4x4
  892.             {              
  893.               //======= Other Modes & CABAC ========
  894.               //------------------------------------
  895.               for (j=block_y; j < block_y+2; j++)
  896.               {
  897.                 for (i=block_x; i < block_x+2; i++)
  898.                 {
  899.                   start_scan = IS_NEWINTRA (currMB)? 1 : 0;
  900.                   img->subblock_x = i; // position for coeff_count ctx
  901.                   img->subblock_y = j; // position for coeff_count ctx
  902.                   if (cbp & (1<<b8))  // are there any coeff in current block at all
  903.                   {
  904.                     coef_ctr = start_scan - 1;
  905.                     level    = 1;
  906.                     img->is_intra_block = intra;
  907.                     if(!lossless_qpprime)
  908.                     {
  909.                       for(k=start_scan;(k<17) && (level!=0);k++)
  910.                       {
  911.                         /*
  912.                         * make distinction between INTRA and INTER coded
  913.                         * luminance coefficients
  914.                         */
  915.                         if (uv == 0)
  916.                           currSE.context = (IS_NEWINTRA(currMB) ? CB_16AC: CB_4x4);
  917.                         else
  918.                           currSE.context = (IS_NEWINTRA(currMB) ? CR_16AC: CR_4x4);
  919.                         if( IS_INDEPENDENT(img) )
  920.                           currSE.context = (IS_NEWINTRA(currMB) ? LUMA_16AC: LUMA_4x4);
  921.                         currSE.type         = (img->is_intra_block 
  922.                           ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA) 
  923.                           : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER)); 
  924. #if TRACE
  925.                         if (uv == 0)
  926.                           sprintf(currSE.tracestring, "Cb   sng ");
  927.                         else
  928.                           sprintf(currSE.tracestring, "Cr   sng ");  
  929. #endif
  930.                         dP = &(currSlice->partArr[partMap[currSE.type]]);
  931.                         if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)  
  932.                           currSE.mapping = linfo_levrun_inter;
  933.                         else                                                     
  934.                           currSE.reading = readRunLevel_CABAC;
  935.                         dP->readSyntaxElement(&currSE,img,dP);
  936.                         level = currSE.value1;
  937.                         //run   = currSE.value2;
  938.                         //len   = currSE.len;
  939.                         if (level != 0)    /* leave if len=1 */
  940.                         {
  941.                           coef_ctr += currSE.value2+1;
  942.                           i0=pos_scan4x4[coef_ctr][0];
  943.                           j0=pos_scan4x4[coef_ctr][1];
  944.                           currMB->cbp_blk_CbCr[uv] |= (int64)1 << ((j<<2) + i) ;
  945.                           img->cof[uv + 1][(j << 2) + j0][(i << 2) + i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0]) << qp_per_uv[uv], 4); //444_TEMP_NOTE
  946.                           img->fcf[uv + 1][(j << 2) + j0][(i << 2) + i0] = level;
  947.                         }//level != 0
  948.                       } //k loop
  949.                     }//!lossless
  950.                     else //(lossless_qpprime)
  951.                     {
  952.                       for(k=start_scan;(k<17) && (level!=0);k++)
  953.                       {
  954.                         /*
  955.                         * make distinction between INTRA and INTER coded
  956.                         * luminance coefficients
  957.                         */
  958.                         if (uv == 0)
  959.                           currSE.context = (IS_NEWINTRA(currMB) ? CB_16AC: CB_4x4);
  960.                         else
  961.                           currSE.context = (IS_NEWINTRA(currMB) ? CR_16AC: CR_4x4);
  962.                         if( IS_INDEPENDENT(img) )
  963.                           currSE.context = (IS_NEWINTRA(currMB) ? LUMA_16AC: LUMA_4x4);
  964.                         currSE.type         = (img->is_intra_block 
  965.                           ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA) 
  966.                           : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));                                      
  967. #if TRACE
  968.                         if (uv == 0)
  969.                           sprintf(currSE.tracestring, "Cb   sng ");
  970.                         else
  971.                           sprintf(currSE.tracestring, "Cr   sng ");  
  972. #endif
  973.                         dP = &(currSlice->partArr[partMap[currSE.type]]);
  974.                         if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)  
  975.                           currSE.mapping = linfo_levrun_inter;
  976.                         else                                                     
  977.                           currSE.reading = readRunLevel_CABAC;
  978.                         dP->readSyntaxElement(&currSE,img,dP);
  979.                         level = currSE.value1;
  980.                         //run   = currSE.value2;
  981.                         //len   = currSE.len;
  982.                         if (level != 0)    /* leave if len=1 */
  983.                         {
  984.                           coef_ctr += currSE.value2 + 1;
  985.                           i0=pos_scan4x4[coef_ctr][0];
  986.                           j0=pos_scan4x4[coef_ctr][1];
  987.                           currMB->cbp_blk_CbCr[uv] |= (int64)1 << ((j<<2) + i);
  988.                           img->cof[uv + 1][(j << 2) + j0][(i << 2) + i0] = level;
  989.                           img->fcf[uv + 1][(j << 2) + j0][(i << 2) + i0] = level;
  990.                         } //level != 0
  991.                       }//k loop
  992.                     } //lossless                    
  993.                   } //(cbp & (1<<b8))                   
  994.                 } //i=block_x
  995.               }//j=block_y              
  996.             } //4x4            
  997.           } //CABAC      
  998.         } //block_x 
  999.       } //block_y      
  1000.     } //uv loop    
  1001.   } //444
  1002.   else  if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
  1003.   {
  1004.     memset(&img->cof[1][0][0], 0, MB_PIXELS * sizeof(int));
  1005.     memset(&img->cof[2][0][0], 0, MB_PIXELS * sizeof(int));
  1006.     memset(&img->fcf[1][0][0], 0, MB_PIXELS * sizeof(int));
  1007.     memset(&img->fcf[2][0][0], 0, MB_PIXELS * sizeof(int));
  1008.     //========================== CHROMA DC ============================
  1009.     //-----------------------------------------------------------------
  1010.     // chroma DC coeff
  1011.     if(cbp>15)
  1012.     {
  1013.       if (dec_picture->chroma_format_idc == YUV420)
  1014.       {    
  1015.         for (ll=0;ll<3;ll+=2)
  1016.         {
  1017.           uv = ll>>1;          
  1018.           if (active_pps->entropy_coding_mode_flag == UVLC)
  1019.           {
  1020.             InvLevelScale4x4 = intra ? InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  1021.             //===================== CHROMA DC YUV420 ======================
  1022.             memset(&img->cofu[0], 0, 4 *sizeof(int));
  1023.             readCoeff4x4_CAVLC(currMB, img, CHROMA_DC, 0, 0, levarr, runarr, &numcoeff);
  1024.             coef_ctr=-1;
  1025.             for(k = 0; k < numcoeff; k++)
  1026.             {
  1027.               if (levarr[k] != 0)
  1028.               {
  1029.                 currMB->cbp_blk |= 0xf0000 << (ll<<1) ;
  1030.                 coef_ctr += runarr[k] + 1;
  1031.                 img->cofu[coef_ctr]=levarr[k];
  1032.               }
  1033.             }
  1034.           }
  1035.           else
  1036.           {
  1037.             InvLevelScale4x4 = intra ? InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  1038.             //===================== CHROMA DC YUV420 ======================
  1039.             memset(&img->cofu[0], 0, 4 *sizeof(int));
  1040.             coef_ctr=-1;
  1041.             level=1;
  1042.             img->is_intra_block =  intra;
  1043.             img->is_v_block     = ll;
  1044.             currSE.context      = CHROMA_DC;
  1045.             currSE.type         = (intra ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
  1046.             dP = &(currSlice->partArr[partMap[currSE.type]]);
  1047.             if (dP->bitstream->ei_flag)
  1048.               currSE.mapping = linfo_levrun_c2x2;
  1049.             else
  1050.               currSE.reading = readRunLevel_CABAC;
  1051.             for(k = 0; (k < (img->num_cdc_coeff + 1))&&(level!=0);k++)
  1052.             {
  1053. #if TRACE
  1054.               snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x2 DC Chroma ");
  1055. #endif
  1056.               dP->readSyntaxElement(&currSE,img,dP);
  1057.               level = currSE.value1;
  1058.               if (level != 0)
  1059.               {
  1060.                 currMB->cbp_blk |= 0xf0000 << (ll<<1) ;
  1061.                 coef_ctr += currSE.value2 + 1;
  1062.                 // Bug: img->cofu has only 4 entries, hence coef_ctr MUST be <4 (which is
  1063.                 // caught by the assert().  If it is bigger than 4, it starts patching the
  1064.                 // img->predmode pointer, which leads to bugs later on.
  1065.                 //
  1066.                 // This assert() should be left in the code, because it captures a very likely
  1067.                 // bug early when testing in error prone environments (or when testing NAL
  1068.                 // functionality).
  1069.                 assert (coef_ctr < img->num_cdc_coeff);
  1070.                 img->cofu[coef_ctr]=level;
  1071.               }
  1072.             }
  1073.           }
  1074.           if (smb || lossless_qpprime) // check to see if MB type is SPred or SIntra4x4
  1075.           {                
  1076.             img->cof[uv + 1][0][0] = img->cofu[0];
  1077.             img->cof[uv + 1][4][0] = img->cofu[1];
  1078.             img->cof[uv + 1][0][4] = img->cofu[2];
  1079.             img->cof[uv + 1][4][4] = img->cofu[3];
  1080.             img->fcf[uv + 1][0][0] = img->cofu[0];
  1081.             img->fcf[uv + 1][4][0] = img->cofu[1];
  1082.             img->fcf[uv + 1][0][4] = img->cofu[2];
  1083.             img->fcf[uv + 1][4][4] = img->cofu[3];
  1084.           }
  1085.           else
  1086.           {
  1087.             ihadamard2x2(img->cofu, temp);
  1088.             img->fcf[uv + 1][0][0] = temp[0];
  1089.             img->fcf[uv + 1][0][4] = temp[1];
  1090.             img->fcf[uv + 1][4][0] = temp[2];
  1091.             img->fcf[uv + 1][4][4] = temp[3];
  1092.             for (i=0;i<img->num_cdc_coeff;i++)
  1093.             {
  1094.               temp[i]= (((temp[i] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  1095.             }
  1096.             img->cof[uv + 1][0][0] = temp[0];
  1097.             img->cof[uv + 1][0][4] = temp[1];
  1098.             img->cof[uv + 1][4][0] = temp[2];
  1099.             img->cof[uv + 1][4][4] = temp[3];
  1100.           }          
  1101.         }
  1102.       }
  1103.       else if (dec_picture->chroma_format_idc == YUV422)
  1104.       {
  1105.         for (ll=0;ll<3;ll+=2)
  1106.         {
  1107.           int (*InvLevelScale4x4)[4] = NULL;
  1108.           uv = ll>>1;
  1109.           {
  1110.             int (*imgcof)[16] = img->cof[uv + 1];
  1111.             int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
  1112.             int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
  1113.             int qp_per_uv_dc = qp_per_matrix[ (currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale) ];       //for YUV422 only
  1114.             int qp_rem_uv_dc = qp_rem_matrix[ (currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale) ];       //for YUV422 only
  1115.             if (intra)
  1116.               InvLevelScale4x4 = InvLevelScale4x4_Intra[uv + 1][qp_rem_uv_dc];
  1117.             else 
  1118.               InvLevelScale4x4 = InvLevelScale4x4_Inter[uv + 1][qp_rem_uv_dc];
  1119.             //===================== CHROMA DC YUV422 ======================
  1120.             if (active_pps->entropy_coding_mode_flag == UVLC)
  1121.             {
  1122.               readCoeff4x4_CAVLC(currMB, img, CHROMA_DC, 0, 0, levarr, runarr, &numcoeff);
  1123.               coef_ctr=-1;
  1124.               level=1;
  1125.               for(k = 0; k < numcoeff; k++)
  1126.               {
  1127.                 if (levarr[k] != 0)
  1128.                 {
  1129.                   currMB->cbp_blk |= ((int64)0xff0000) << (ll<<2);
  1130.                   coef_ctr += runarr[k]+1;
  1131.                   i0 = SCAN_YUV422[coef_ctr][0];
  1132.                   j0 = SCAN_YUV422[coef_ctr][1];
  1133.                   m3[i0][j0]=levarr[k];
  1134.                 }
  1135.               }
  1136.             }
  1137.             else
  1138.             {
  1139.               coef_ctr=-1;
  1140.               level=1;
  1141.               for(k=0;(k<9)&&(level!=0);k++)
  1142.               {
  1143.                 currSE.context      = CHROMA_DC_2x4;
  1144.                 currSE.type         = (IS_INTRA(currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
  1145.                 img->is_intra_block =  IS_INTRA(currMB);
  1146.                 img->is_v_block     = ll;
  1147. #if TRACE
  1148.                 snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x4 DC Chroma ");
  1149. #endif
  1150.                 dP = &(currSlice->partArr[partMap[currSE.type]]);
  1151.                 if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
  1152.                   currSE.mapping = linfo_levrun_c2x2;
  1153.                 else
  1154.                   currSE.reading = readRunLevel_CABAC;
  1155.                 dP->readSyntaxElement(&currSE,img,dP);
  1156.                 level = currSE.value1;
  1157.                 if (level != 0)
  1158.                 {
  1159.                   currMB->cbp_blk |= ((int64)0xff0000) << (ll<<2) ;
  1160.                   coef_ctr += currSE.value2 + 1;
  1161.                   assert (coef_ctr < img->num_cdc_coeff);
  1162.                   i0=SCAN_YUV422[coef_ctr][0];
  1163.                   j0=SCAN_YUV422[coef_ctr][1];
  1164.                   m3[i0][j0]=level;
  1165.                 }
  1166.               }
  1167.             }
  1168.             // inverse CHROMA DC YUV422 transform
  1169.             // horizontal
  1170.             if(!lossless_qpprime)
  1171.             {
  1172.               m4[0][0] = m3[0][0] + m3[1][0];
  1173.               m4[0][1] = m3[0][1] + m3[1][1];
  1174.               m4[0][2] = m3[0][2] + m3[1][2];
  1175.               m4[0][3] = m3[0][3] + m3[1][3];
  1176.               m4[1][0] = m3[0][0] - m3[1][0];
  1177.               m4[1][1] = m3[0][1] - m3[1][1];
  1178.               m4[1][2] = m3[0][2] - m3[1][2];
  1179.               m4[1][3] = m3[0][3] - m3[1][3];
  1180.               for (i = 0; i < 2; i++)
  1181.               {
  1182.                 m6[0] = m4[i][0] + m4[i][2];
  1183.                 m6[1] = m4[i][0] - m4[i][2];
  1184.                 m6[2] = m4[i][1] - m4[i][3];
  1185.                 m6[3] = m4[i][1] + m4[i][3];
  1186.                 imgcof[ 0][i<<2] = m6[0] + m6[3];
  1187.                 imgcof[ 4][i<<2] = m6[1] + m6[2];
  1188.                 imgcof[ 8][i<<2] = m6[1] - m6[2];
  1189.                 imgcof[12][i<<2] = m6[0] - m6[3];
  1190.               }//for (i=0;i<2;i++)
  1191.             }
  1192.             else
  1193.             {
  1194.               for(j=0;j<4;j++)
  1195.               {
  1196.                 for(i=0;i<2;i++)                
  1197.                 {
  1198.                   img->cof[uv + 1][j<<2][i<<2] = m3[i][j];
  1199.                   img->fcf[uv + 1][j<<2][i<<2] = m3[i][j];
  1200.                 }
  1201.               }
  1202.             }
  1203.             for(j = 0;j < img->mb_cr_size_y; j += BLOCK_SIZE)
  1204.             {
  1205.               for(i=0;i < img->mb_cr_size_x;i+=BLOCK_SIZE)
  1206.               {
  1207.                 imgcof[j][i] = rshift_rnd_sf((imgcof[j][i] * InvLevelScale4x4[0][0]) << qp_per_uv_dc, 6);
  1208.               }
  1209.             }
  1210.           }
  1211.         }//for (ll=0;ll<3;ll+=2)
  1212.       }//else if (dec_picture->chroma_format_idc == YUV422)
  1213.     }
  1214.     //========================== CHROMA AC ============================
  1215.     //-----------------------------------------------------------------
  1216.     // chroma AC coeff, all zero fram start_scan
  1217.     if (cbp<=31)
  1218.     {
  1219.       if (active_pps->entropy_coding_mode_flag == UVLC)
  1220.         memset(&img->nz_coeff [mb_nr ][1][0][0], 0, 2 * BLOCK_SIZE * BLOCK_SIZE * sizeof(int));
  1221.     }
  1222.     else
  1223.     {
  1224.       if (active_pps->entropy_coding_mode_flag == UVLC)
  1225.       {
  1226.         if(!lossless_qpprime)
  1227.         {
  1228.           for (b8=0; b8 < img->num_blk8x8_uv; b8++)
  1229.           {
  1230.             img->is_v_block = uv = (b8 > ((img->num_uv_blocks) - 1 ));
  1231.             InvLevelScale4x4 = intra ? InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  1232.             for (b4=0; b4 < 4; b4++)
  1233.             {
  1234.               i = cofuv_blk_x[yuv][b8][b4];
  1235.               j = cofuv_blk_y[yuv][b8][b4];
  1236.               readCoeff4x4_CAVLC(currMB, img, CHROMA_AC, i + 2*uv, j + 4, levarr, runarr, &numcoeff);
  1237.               coef_ctr = 0;
  1238.               for(k = 0; k < numcoeff;k++)
  1239.               {
  1240.                 if (levarr[k] != 0)
  1241.                 {
  1242.                   currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
  1243.                   coef_ctr += runarr[k] + 1;
  1244.                   i0=pos_scan4x4[coef_ctr][0];
  1245.                   j0=pos_scan4x4[coef_ctr][1];
  1246.                   img->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
  1247.                   img->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = levarr[k];
  1248.                 }
  1249.               }
  1250.             }
  1251.           }
  1252.         }
  1253.         else
  1254.         {
  1255.           for (b8=0; b8 < img->num_blk8x8_uv; b8++)
  1256.           {
  1257.             img->is_v_block = uv = (b8 > ((img->num_uv_blocks) - 1 ));
  1258.             for (b4=0; b4 < 4; b4++)
  1259.             {
  1260.               i = cofuv_blk_x[yuv][b8][b4];
  1261.               j = cofuv_blk_y[yuv][b8][b4];
  1262.               readCoeff4x4_CAVLC(currMB, img, CHROMA_AC, i, j, levarr, runarr, &numcoeff);
  1263.               coef_ctr=0;
  1264.               level=1;
  1265.               for(k = 0; k < numcoeff;k++)
  1266.               {
  1267.                 if (levarr[k] != 0)
  1268.                 {
  1269.                   currMB->cbp_blk |= ((int64) 1) << cbp_blk_chroma[b8][b4];
  1270.                   coef_ctr += runarr[k] + 1;
  1271.                   i0 = pos_scan4x4[coef_ctr][0];
  1272.                   j0 = pos_scan4x4[coef_ctr][1];
  1273.                   img->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = levarr[k];
  1274.                   img->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = levarr[k];
  1275.                 }
  1276.               }
  1277.             }
  1278.           }
  1279.         }
  1280.       }
  1281.       else
  1282.       {
  1283.         img->is_intra_block =  IS_INTRA(currMB);
  1284.         currSE.context      = CHROMA_AC;
  1285.         currSE.type         = (img->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
  1286.         dP = &(currSlice->partArr[partMap[currSE.type]]);
  1287.         if (dP->bitstream->ei_flag)
  1288.           currSE.mapping = linfo_levrun_inter;
  1289.         else
  1290.           currSE.reading = readRunLevel_CABAC;
  1291.         if(!lossless_qpprime)
  1292.         {          
  1293.           for (b8=0; b8 < img->num_blk8x8_uv; b8++)
  1294.           {
  1295.             img->is_v_block = uv = (b8 > ((img->num_uv_blocks) - 1 ));
  1296.             InvLevelScale4x4 = intra ? InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  1297.             for (b4 = 0; b4 < 4; b4++)
  1298.             {
  1299.               i = cofuv_blk_x[yuv][b8][b4];
  1300.               j = cofuv_blk_y[yuv][b8][b4];
  1301.               img->subblock_y = subblk_offset_y[yuv][b8][b4]>>2;
  1302.               img->subblock_x = subblk_offset_x[yuv][b8][b4]>>2;
  1303.               pos_scan_4x4 = pos_scan4x4[1];
  1304.               level=1;
  1305.               for(k = 0; (k < 16) && (level != 0);k++)
  1306.               {
  1307. #if TRACE
  1308.                 snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
  1309. #endif
  1310.                 dP->readSyntaxElement(&currSE,img,dP);
  1311.                 level = currSE.value1;
  1312.                 if (level != 0)
  1313.                 {
  1314.                   currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
  1315.                   pos_scan_4x4 += (currSE.value2 << 1);
  1316.                   
  1317.                   i0 = *pos_scan_4x4++;
  1318.                   j0 = *pos_scan_4x4++;
  1319.                   img->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
  1320.                   img->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
  1321.                 }
  1322.               } //for(k=0;(k<16)&&(level!=0);k++)
  1323.             }
  1324.           }
  1325.         }
  1326.         else
  1327.         {
  1328.           for (b8=0; b8 < img->num_blk8x8_uv; b8++)
  1329.           {
  1330.             img->is_v_block = uv = (b8 > ((img->num_uv_blocks) - 1 ));
  1331.             for (b4=0; b4 < 4; b4++)
  1332.             {
  1333.               i = cofuv_blk_x[yuv][b8][b4];
  1334.               j = cofuv_blk_y[yuv][b8][b4];
  1335.               pos_scan_4x4 = pos_scan4x4[1];
  1336.               level=1;
  1337.               img->subblock_y = subblk_offset_y[yuv][b8][b4]>>2;
  1338.               img->subblock_x = subblk_offset_x[yuv][b8][b4]>>2;
  1339.               for(k=0;(k<16)&&(level!=0);k++)
  1340.               {
  1341. #if TRACE
  1342.                 snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
  1343. #endif
  1344.                 dP->readSyntaxElement(&currSE,img,dP);
  1345.                 level = currSE.value1;
  1346.                 if (level != 0)
  1347.                 {
  1348.                   currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
  1349.                   pos_scan_4x4 += (currSE.value2 << 1);
  1350.                   
  1351.                   i0 = *pos_scan_4x4++;
  1352.                   j0 = *pos_scan_4x4++;
  1353.                   img->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
  1354.                   img->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
  1355.                 }
  1356.               } 
  1357.             }
  1358.           } 
  1359.         } //for (b4=0; b4 < 4; b4++)
  1360.       } //for (b8=0; b8 < img->num_blk8x8_uv; b8++)
  1361.     } //if (dec_picture->chroma_format_idc != YUV400)
  1362.   }
  1363. }
  1364. /*!
  1365.  ************************************************************************
  1366.  * brief
  1367.  *    Copy IPCM coefficients to decoded picture buffer and set parameters for this MB
  1368.  *    (for IPCM CABAC and IPCM CAVLC  28/11/2003)
  1369.  *
  1370.  * author
  1371.  *    Dong Wang <Dong.Wang@bristol.ac.uk>
  1372.  ************************************************************************
  1373.  */
  1374. void decode_ipcm_mb(ImageParameters *img, Macroblock *currMB)
  1375. {
  1376.   int i, j, k;
  1377.   int mb_nr = img->current_mb_nr;
  1378.   //Copy coefficients to decoded picture buffer
  1379.   //IPCM coefficients are stored in img->cof which is set in function readIPCMcoeffsFromNAL()
  1380.   for(i = 0; i < MB_BLOCK_SIZE; i++)
  1381.   {
  1382.     for(j = 0;j < MB_BLOCK_SIZE ; j++)
  1383.     {
  1384.       dec_picture->imgY[img->pix_y+i][img->pix_x+j] = (imgpel) img->cof[0][i][j];
  1385.     }
  1386.   }
  1387.   if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(img))
  1388.   {
  1389.     for (k = 0; k < 2; k++)
  1390.     {
  1391.       for(i = 0; i < img->mb_cr_size_y; i++)
  1392.       {
  1393.         for(j = 0;j < img->mb_cr_size_x; j++)
  1394.         {
  1395.           dec_picture->imgUV[k][img->pix_c_y+i][img->pix_c_x+j] = (imgpel) img->cof[k + 1][i][j];  
  1396.         }
  1397.       }
  1398.     }
  1399.   }
  1400.   // for deblocking filter
  1401.   update_qp(img, currMB, 0);
  1402.   // for CAVLC: Set the nz_coeff to 16.
  1403.   // These parameters are to be used in CAVLC decoding of neighbour blocks  
  1404.   for (k = 0; k < 3; k++)
  1405.   {
  1406.     for (j = 0; j < BLOCK_SIZE; j++)
  1407.     {
  1408.       for(i = 0; i < BLOCK_SIZE; i++)
  1409.       {
  1410.         img->nz_coeff[mb_nr][k][j][i] = 16;
  1411.       }
  1412.     }
  1413.   }
  1414.   // for CABAC decoding of MB skip flag
  1415.   currMB->skip_flag = 0;
  1416.   //for deblocking filter CABAC
  1417.   currMB->cbp_blk = 0xFFFF;
  1418.   //For CABAC decoding of Dquant
  1419.   last_dquant = 0;
  1420. }
  1421. /*!
  1422.  ************************************************************************
  1423.  * brief
  1424.  *    decode one macroblock
  1425.  ************************************************************************
  1426.  */
  1427. int decode_one_macroblock(ImageParameters *img, Macroblock *currMB, StorablePicture *dec_picture)
  1428. {
  1429.   int i=0,j=0,k,l,ii=0,jj=0, j4=0,i4=0, j6;  
  1430.   int refList;
  1431.   int uv, hv;
  1432.   int ioff,joff;
  1433.   int block8x8;   // needed for ABT
  1434.   int j_pos, i_pos;
  1435.   static const byte decode_block_scan[16] = {0,1,4,5,2,3,6,7,8,9,12,13,10,11,14,15};
  1436.   int mb_nr     = img->current_mb_nr;
  1437.   short ref_idx;
  1438.   int mv_mode, pred_dir; // = currMB->ref_frame;  
  1439.   int block_size_x, block_size_y;
  1440.   int mv_scale;
  1441.   static imgpel **curComp;
  1442.   static imgpel (*mpr) [16];
  1443.   static imgpel *cur_line;
  1444.   static int    *cur_m7;
  1445.   int smb = ((img->type==SP_SLICE) && IS_INTER (currMB)) || (img->type == SI_SLICE && currMB->mb_type == SI4MB);
  1446.   int list_offset;
  1447.   char l0_rFrame = -1, l1_rFrame = -1;
  1448.   short pmvl0[2]={0,0}, pmvl1[2]={0,0};
  1449.   int direct_pdir=-1;
  1450.   int curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
  1451.   static MotionParams *colocated;
  1452.   int need_4x4_transform = (!currMB->luma_transform_size_8x8_flag);
  1453.   int yuv = dec_picture->chroma_format_idc - 1;
  1454.   //For residual DPCM
  1455.   Boolean lossless_qpprime = (Boolean) (((img->qp + img->bitdepth_luma_qp_scale) == 0) && (img->lossless_qpprime_flag == 1));  
  1456.   ipmode_DPCM = NO_INTRA_PMODE; 
  1457.   if (img->structure != FRAME || (img->MbaffFrameFlag && currMB->mb_field))
  1458.     img->max_mb_vmv_r = img->max_vmv_r >> 1;
  1459.   else
  1460.     img->max_mb_vmv_r = img->max_vmv_r;
  1461.   if(currMB->mb_type == IPCM)
  1462.   {
  1463.     //copy readed data into imgY and set parameters
  1464.     decode_ipcm_mb(img, currMB);
  1465.     return 0;
  1466.   }
  1467.   if (img->type==SP_SLICE && currMB->mb_type!=I16MB)
  1468.     smb = 1;// modif ES added
  1469.   //////////////////////////
  1470.   // find out the correct list offsets
  1471.   if (curr_mb_field)
  1472.   {
  1473.     if(mb_nr&0x01)
  1474.     {
  1475.       list_offset = 4; // top field mb
  1476.       colocated = &Co_located->bottom;
  1477.     }
  1478.     else
  1479.     {
  1480.       list_offset = 2; // bottom field mb
  1481.       colocated = &Co_located->top;
  1482.     }
  1483.   }
  1484.   else
  1485.   {
  1486.     list_offset = 0;  // no mb aff or frame mb
  1487.     colocated = &Co_located->frame;
  1488.   }
  1489.   if (!img->MbaffFrameFlag)
  1490.   {
  1491.     for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
  1492.     {
  1493.       for(k = 0; k < listXsize[l]; k++)
  1494.       {
  1495.         listX[l][k]->chroma_vector_adjustment= 0;
  1496.         if(img->structure == TOP_FIELD && img->structure != listX[l][k]->structure)
  1497.           listX[l][k]->chroma_vector_adjustment = -2;
  1498.         if(img->structure == BOTTOM_FIELD && img->structure != listX[l][k]->structure)
  1499.           listX[l][k]->chroma_vector_adjustment = 2;
  1500.       }
  1501.     }
  1502.   }
  1503.   else
  1504.   {
  1505.     if (curr_mb_field)
  1506.     {
  1507.       for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
  1508.       {
  1509.         for(k = 0; k < listXsize[l]; k++)
  1510.         {
  1511.           listX[l][k]->chroma_vector_adjustment= 0;
  1512.           if(mb_nr % 2 == 0 && listX[l][k]->structure == BOTTOM_FIELD)
  1513.             listX[l][k]->chroma_vector_adjustment = -2;
  1514.           if(mb_nr % 2 == 1 && listX[l][k]->structure == TOP_FIELD)
  1515.             listX[l][k]->chroma_vector_adjustment = 2;
  1516.         }
  1517.       }
  1518.     }
  1519.     else
  1520.     {
  1521.       for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
  1522.       {
  1523.         for(k = 0; k < listXsize[l]; k++)
  1524.         {
  1525.           listX[l][k]->chroma_vector_adjustment= 0;
  1526.         }
  1527.       }
  1528.     }
  1529.   }
  1530.   // luma decoding **************************************************
  1531.   // get prediction for INTRA_MB_16x16
  1532.   if (IS_NEWINTRA (currMB))
  1533.   {
  1534.     intrapred_luma_16x16(img, currMB, PLANE_Y, currMB->i16mode);
  1535.     ipmode_DPCM = currMB->i16mode; //For residual DPCM
  1536.     // =============== 4x4 itrans ================
  1537.     // -------------------------------------------
  1538.     iMBtrans4x4(PLANE_Y, img, smb);
  1539.     // chroma decoding *******************************************************
  1540.     if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444)) 
  1541.     {
  1542.       intra_cr_decoding(currMB, yuv, img, smb);
  1543.     }
  1544.   }
  1545.   else if (currMB->mb_type == I4MB)
  1546.   {
  1547.     itrans_4x4 = (!lossless_qpprime) ? itrans4x4 : Inv_Residual_trans_4x4;
  1548.     for (block8x8 = 0; block8x8 < 4; block8x8++)
  1549.     {
  1550.       for (k = block8x8 * 4; k < block8x8 * 4 + 4; k ++)
  1551.       {
  1552.         i =  (decode_block_scan[k] & 3);
  1553.         j = ((decode_block_scan[k] >> 2) & 3);
  1554.         ioff = (i << 2);
  1555.         joff = (j << 2);
  1556.         i4   = img->block_x + i;
  1557.         j4   = img->block_y + j;
  1558.         j_pos = j4 * BLOCK_SIZE;
  1559.         i_pos = i4 * BLOCK_SIZE;
  1560.         // PREDICTION
  1561.         //===== INTRA PREDICTION =====
  1562.         if (intrapred(img, currMB, PLANE_Y, ioff,joff,i4,j4) == SEARCH_SYNC)  /* make 4x4 prediction block mpr from given prediction img->mb_mode */
  1563.           return SEARCH_SYNC;                   /* bit error */
  1564.         // =============== 4x4 itrans ================
  1565.         // -------------------------------------------
  1566.         itrans_4x4  (img, (ColorPlane) LumaComp, ioff, joff);      // use DCT transform and make 4x4 block m7 from prediction block mpr
  1567.         for(jj=0;jj<BLOCK_SIZE;jj++)
  1568.         {
  1569.           cur_m7 = &img->mb_rres[0][jj + joff][ioff];
  1570.           cur_line = &dec_picture->imgY[j_pos + jj][i_pos];
  1571.           for(ii=0;ii<BLOCK_SIZE;ii++)
  1572.           {
  1573.             *(cur_line++) = (imgpel) (*cur_m7++); // construct picture from 4x4 blocks
  1574.           }
  1575.         }
  1576.       }
  1577.     }
  1578.     // chroma decoding *******************************************************
  1579.     if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444)) 
  1580.     {
  1581.       intra_cr_decoding(currMB, yuv, img, smb);
  1582.     }
  1583.   }
  1584.   else if (currMB->mb_type == I8MB) 
  1585.   {
  1586.     itrans_8x8 = (!lossless_qpprime) ? itrans8x8 : Inv_Residual_trans_8x8;
  1587.     for (block8x8 = 0; block8x8 < 4; block8x8++)
  1588.     {
  1589.       //=========== 8x8 BLOCK TYPE ============
  1590.       ioff = 8 * (block8x8 & 0x01);
  1591.       joff = 8 * (block8x8 >> 1);
  1592.       //PREDICTION
  1593.       intrapred8x8(img, currMB, PLANE_Y, block8x8);
  1594.       itrans_8x8(img, currMB, PLANE_Y, ioff,joff);      // use DCT transform and make 8x8 block m7 from prediction block mpr
  1595.       for(jj = joff; jj < joff + 8;jj++)
  1596.       {
  1597.         cur_m7 = &img->mb_rres[0][jj][ioff];
  1598.         cur_line = &dec_picture->imgY[img->pix_y + jj][img->pix_x + ioff];
  1599.         for(ii = 0; ii < 8; ii++)
  1600.         {
  1601.           *(cur_line++) = (imgpel) *(cur_m7++); // construct picture from 8x8 blocks
  1602.         }
  1603.       }
  1604.     } 
  1605.     // chroma decoding *******************************************************
  1606.     if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444)) 
  1607.     {
  1608.       intra_cr_decoding(currMB, yuv, img, smb);
  1609.     }
  1610.   }
  1611.   else if ((img->type == P_SLICE) && (currMB->mb_type == PSKIP))
  1612.   {   
  1613.     block_size_x = MB_BLOCK_SIZE;
  1614.     block_size_y = MB_BLOCK_SIZE;
  1615.     i = 0;
  1616.     j = 0;
  1617.     pred_dir = LIST_0;   
  1618.     perform_mc(PLANE_Y, dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1619.     mpr = img->mb_pred[LumaComp];
  1620.     curComp = &dec_picture->imgY[img->pix_y];
  1621.     for(j = 0; j < img->mb_size[0][1]; j++)
  1622.     {                
  1623.       memcpy(&(curComp[j][img->pix_x]), &(mpr[j][0]), img->mb_size[0][0] * sizeof(imgpel));
  1624.     }
  1625.     if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444)) 
  1626.     {
  1627.       for(uv=0;uv<2;uv++)
  1628.       {
  1629.         curComp = &dec_picture->imgUV[uv][img->pix_c_y]; 
  1630.         mpr = img->mb_pred[uv + 1];
  1631.         for(jj = 0; jj < img->mb_size[1][1]; jj++)
  1632.           memcpy(&(curComp[jj][img->pix_c_x]), &(mpr[jj][0]), img->mb_size[1][0] * sizeof(imgpel));
  1633.       }
  1634.     }
  1635.   }
  1636.   else if (currMB->mb_type == P16x16)
  1637.   {
  1638.     block_size_x = MB_BLOCK_SIZE;
  1639.     block_size_y = MB_BLOCK_SIZE;
  1640.     i = 0;
  1641.     j = 0;
  1642.     pred_dir = currMB->b8pdir[0];   
  1643.     perform_mc(PLANE_Y, dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1644.     iTransform(img, currMB, PLANE_Y, need_4x4_transform, smb);
  1645.   }
  1646.   else if (currMB->mb_type == P16x8)
  1647.   {   
  1648.     block_size_x = MB_BLOCK_SIZE;
  1649.     block_size_y = 8;    
  1650.     for (block8x8 = 0; block8x8 < 4; block8x8 += 2)
  1651.     {
  1652.       i = 0;
  1653.       j = block8x8;      
  1654.       pred_dir = currMB->b8pdir[block8x8];
  1655.       perform_mc(PLANE_Y, dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1656.     }
  1657.     iTransform(img, currMB, PLANE_Y, need_4x4_transform, smb); 
  1658.   }
  1659.   else if (currMB->mb_type == P8x16)
  1660.   {   
  1661.     block_size_x = 8;
  1662.     block_size_y = 16;
  1663.     for (block8x8 = 0; block8x8 < 2; block8x8 ++)
  1664.     {
  1665.       i = block8x8<<1;
  1666.       j = 0;      
  1667.       pred_dir = currMB->b8pdir[block8x8];
  1668.       assert (pred_dir<=2);
  1669.       perform_mc(PLANE_Y, dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1670.     }
  1671.     iTransform(img, currMB, PLANE_Y, need_4x4_transform, smb);
  1672.   }
  1673.   else
  1674.   {
  1675.     // prepare direct modes
  1676.     if (img->type==B_SLICE && img->direct_spatial_mv_pred_flag && (IS_DIRECT (currMB) ||
  1677.       (IS_P8x8(currMB) && !(currMB->b8mode[0] && currMB->b8mode[1] && currMB->b8mode[2] && currMB->b8mode[3]))))
  1678.       prepare_direct_params(currMB, dec_picture, img, pmvl0, pmvl1, &l0_rFrame, &l1_rFrame);
  1679.     for (block8x8=0; block8x8<4; block8x8++)
  1680.     {
  1681.       mv_mode  = currMB->b8mode[block8x8];
  1682.       pred_dir = currMB->b8pdir[block8x8];
  1683.       //if ( mv_mode == SMB8x8 || mv_mode == SMB8x4 || mv_mode == SMB4x8 || mv_mode == SMB4x4 )
  1684.       if ( mv_mode != 0 )
  1685.       {
  1686.         int k_start = (block8x8 << 2);
  1687.         int k_inc = (mv_mode == SMB8x4) ? 2 : 1;
  1688.         int k_end = (mv_mode == SMB8x8) ? k_start + 1 : ((mv_mode == SMB4x4) ? k_start + 4 : k_start + k_inc + 1);
  1689.         block_size_x = ( mv_mode == SMB8x4 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
  1690.         block_size_y = ( mv_mode == SMB4x8 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
  1691.         for (k = k_start; k < k_end; k += k_inc)
  1692.         {
  1693.           i =  (decode_block_scan[k] & 3);
  1694.           j = ((decode_block_scan[k] >> 2) & 3);
  1695.           perform_mc(PLANE_Y, dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1696.         }        
  1697.       }
  1698.       else
  1699.       {
  1700.         int k_start = (block8x8 << 2);
  1701.         int k_end = k_start;
  1702.         if (active_sps->direct_8x8_inference_flag)
  1703.         {
  1704.           block_size_x = SMB_BLOCK_SIZE;
  1705.           block_size_y = SMB_BLOCK_SIZE;
  1706.           k_end ++;
  1707.         }
  1708.         else
  1709.         {
  1710.           block_size_x = BLOCK_SIZE;
  1711.           block_size_y = BLOCK_SIZE;
  1712.           k_end += BLOCK_MULTIPLE;
  1713.         }
  1714.         // Prepare mvs (needed for deblocking and mv prediction
  1715.         if (img->direct_spatial_mv_pred_flag)
  1716.         {
  1717.           for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
  1718.           {
  1719.             i =  (decode_block_scan[k] & 3);
  1720.             j = ((decode_block_scan[k] >> 2) & 3);
  1721.             ioff = (i << 2);
  1722.             i4   = img->block_x + i;
  1723.             joff = (j << 2);
  1724.             j4   = img->block_y + j;
  1725.             assert (pred_dir<=2);
  1726.             j6 = img->block_y_aff + j;
  1727.             //===== DIRECT PREDICTION =====
  1728.             if (l0_rFrame >=0)
  1729.             {
  1730.               if (!l0_rFrame  && ((!colocated->moving_block[j6][i4]) && (!listX[LIST_1 + list_offset][0]->is_long_term)))
  1731.               {
  1732.                 dec_picture->motion.mv  [LIST_0][j4][i4][0] = 0;
  1733.                 dec_picture->motion.mv  [LIST_0][j4][i4][1] = 0;
  1734.                 dec_picture->motion.ref_idx[LIST_0][j4][i4] = 0;
  1735.               }
  1736.               else
  1737.               {
  1738.                 dec_picture->motion.mv  [LIST_0][j4][i4][0] = pmvl0[0];
  1739.                 dec_picture->motion.mv  [LIST_0][j4][i4][1] = pmvl0[1];
  1740.                 dec_picture->motion.ref_idx[LIST_0][j4][i4] = l0_rFrame;
  1741.               }
  1742.             }
  1743.             else
  1744.             {
  1745.               dec_picture->motion.ref_idx[LIST_0][j4][i4] = -1;
  1746.               dec_picture->motion.mv  [LIST_0][j4][i4][0] = 0;
  1747.               dec_picture->motion.mv  [LIST_0][j4][i4][1] = 0;
  1748.             }
  1749.             if (l1_rFrame >=0)
  1750.             {
  1751.               if  (l1_rFrame==0 && ((!colocated->moving_block[j6][i4]) && (!listX[LIST_1 + list_offset][0]->is_long_term)))
  1752.               {
  1753.                 dec_picture->motion.mv  [LIST_1][j4][i4][0] = 0;
  1754.                 dec_picture->motion.mv  [LIST_1][j4][i4][1] = 0;
  1755.                 dec_picture->motion.ref_idx[LIST_1][j4][i4] = l1_rFrame;
  1756.               }
  1757.               else
  1758.               {
  1759.                 dec_picture->motion.mv  [LIST_1][j4][i4][0] = pmvl1[0];
  1760.                 dec_picture->motion.mv  [LIST_1][j4][i4][1] = pmvl1[1];
  1761.                 dec_picture->motion.ref_idx[LIST_1][j4][i4] = l1_rFrame;
  1762.               }
  1763.             }
  1764.             else
  1765.             {
  1766.               dec_picture->motion.mv  [LIST_1][j4][i4][0] = 0;
  1767.               dec_picture->motion.mv  [LIST_1][j4][i4][1] = 0;
  1768.               dec_picture->motion.ref_idx[LIST_1][j4][i4] = -1;
  1769.             }
  1770.             if (l0_rFrame < 0 && l1_rFrame < 0)
  1771.             {
  1772.               dec_picture->motion.ref_idx[LIST_0][j4][i4] = 0;
  1773.               dec_picture->motion.ref_idx[LIST_1][j4][i4] = 0;
  1774.             }
  1775.             if      (dec_picture->motion.ref_idx[LIST_1][j4][i4]==-1) 
  1776.             {
  1777.               direct_pdir = 0;
  1778.               ref_idx  = (dec_picture->motion.ref_idx[LIST_0][j4][i4] != -1) ? dec_picture->motion.ref_idx[LIST_0][j4][i4] : 0;
  1779.             }
  1780.             else if (dec_picture->motion.ref_idx[LIST_0][j4][i4]==-1) 
  1781.             {
  1782.               direct_pdir = 1;
  1783.               ref_idx  = (dec_picture->motion.ref_idx[LIST_1][j4][i4] != -1) ? dec_picture->motion.ref_idx[LIST_1][j4][i4] : 0;
  1784.             }
  1785.             else                                               
  1786.               direct_pdir = 2;
  1787.             pred_dir = direct_pdir;
  1788.             dec_picture->motion.ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][(short)dec_picture->motion.ref_idx[LIST_0][j4][i4]];
  1789.             dec_picture->motion.ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_1 + list_offset][(short)dec_picture->motion.ref_idx[LIST_1][j4][i4]];
  1790.           }
  1791.         }
  1792.         else
  1793.         {
  1794.           for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
  1795.           {
  1796.             i =  (decode_block_scan[k] & 3);
  1797.             j = ((decode_block_scan[k] >> 2) & 3);
  1798.             ioff = (i << 2);
  1799.             i4   = img->block_x + i;
  1800.             joff = (j << 2);
  1801.             j4   = img->block_y + j;
  1802.             assert (pred_dir<=2);
  1803.             j6   = img->block_y_aff + j;
  1804.             refList = (colocated->ref_idx[LIST_0][j6][i4]== -1 ? LIST_1 : LIST_0);
  1805.             ref_idx =  colocated->ref_idx[refList][j6][i4];
  1806.             if(ref_idx==-1) // co-located is intra mode
  1807.             {
  1808.               memset( &dec_picture->motion.mv  [LIST_0][j4][i4][0], 0, 2* sizeof(short));
  1809.               memset( &dec_picture->motion.mv  [LIST_1][j4][i4][0], 0, 2* sizeof(short));
  1810.               dec_picture->motion.ref_idx[LIST_0][j4][i4] = 0;
  1811.               dec_picture->motion.ref_idx[LIST_1][j4][i4] = 0;
  1812.             }
  1813.             else // co-located skip or inter mode
  1814.             {
  1815.               int mapped_idx=0;
  1816.               int iref;
  1817.               for (iref=0;iref<imin(img->num_ref_idx_l0_active,listXsize[LIST_0 + list_offset]);iref++)
  1818.               {
  1819.                 if(img->structure==0 && curr_mb_field==0)
  1820.                 {
  1821.                   // If the current MB is a frame MB and the colocated is from a field picture,
  1822.                   // then the colocated->ref_pic_id may have been generated from the wrong value of
  1823.                   // frame_poc if it references it's complementary field, so test both POC values
  1824.                   if(listX[0][iref]->top_poc*2 == colocated->ref_pic_id[refList][j6][i4] || listX[0][iref]->bottom_poc*2 == colocated->ref_pic_id[refList][j6][i4])
  1825.                   {
  1826.                     mapped_idx=iref;
  1827.                     break;
  1828.                   }
  1829.                   else //! invalid index. Default to zero even though this case should not happen
  1830.                     mapped_idx=INVALIDINDEX;
  1831.                   continue;
  1832.                 }
  1833.                 if (dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][iref]==colocated->ref_pic_id[refList][j6][i4])
  1834.                 {
  1835.                   mapped_idx=iref;
  1836.                   break;
  1837.                 }
  1838.                 else //! invalid index. Default to zero even though this case should not happen
  1839.                 {
  1840.                   mapped_idx=INVALIDINDEX;
  1841.                 }
  1842.               }
  1843.               if (INVALIDINDEX == mapped_idx)
  1844.               {
  1845.                 error("temporal direct errorncolocated block has ref that is unavailable",-1111);
  1846.               }              
  1847.               mv_scale = img->mvscale[LIST_0 + list_offset][mapped_idx];
  1848.               //! In such case, an array is needed for each different reference.
  1849.               if (mv_scale == 9999 || listX[LIST_0+list_offset][mapped_idx]->is_long_term)
  1850.               {
  1851.                 memcpy(&dec_picture->motion.mv  [LIST_0][j4][i4][0], &colocated->mv[refList][j6][i4][0], 2 * sizeof(short));
  1852.                 memset(&dec_picture->motion.mv  [LIST_1][j4][i4][0], 0, 2 * sizeof(short));
  1853.               }
  1854.               else
  1855.               {
  1856.                 dec_picture->motion.mv  [LIST_0][j4][i4][0]=(mv_scale * colocated->mv[refList][j6][i4][0] + 128 ) >> 8;
  1857.                 dec_picture->motion.mv  [LIST_0][j4][i4][1]=(mv_scale * colocated->mv[refList][j6][i4][1] + 128 ) >> 8;
  1858.                 dec_picture->motion.mv  [LIST_1][j4][i4][0]=dec_picture->motion.mv[LIST_0][j4][i4][0] - colocated->mv[refList][j6][i4][0] ;
  1859.                 dec_picture->motion.mv  [LIST_1][j4][i4][1]=dec_picture->motion.mv[LIST_0][j4][i4][1] - colocated->mv[refList][j6][i4][1] ;
  1860.               }
  1861.               dec_picture->motion.ref_idx[LIST_0][j4][i4] = (char) mapped_idx; //listX[1][0]->ref_idx[refList][j4][i4];
  1862.               dec_picture->motion.ref_idx[LIST_1][j4][i4] = 0;
  1863.             }
  1864.             // store reference picture ID determined by direct mode
  1865.             dec_picture->motion.ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][(short)dec_picture->motion.ref_idx[LIST_0][j4][i4]];
  1866.             dec_picture->motion.ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_1 + list_offset][(short)dec_picture->motion.ref_idx[LIST_1][j4][i4]];
  1867.           }
  1868.         }
  1869.         for (k = k_start; k < k_end; k ++)
  1870.         {
  1871.           i =  (decode_block_scan[k] & 3);
  1872.           j = ((decode_block_scan[k] >> 2) & 3);
  1873.           perform_mc(PLANE_Y, dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);           
  1874.         }
  1875.       }
  1876.     }
  1877.     iTransform(img, currMB, PLANE_Y, need_4x4_transform, smb); 
  1878.   }
  1879.   if ((active_sps->chroma_format_idc==YUV444)&&(!IS_INDEPENDENT(img)))  
  1880.   {
  1881.     for (uv = 0; uv < 2; uv++ )
  1882.     {
  1883.       if (IS_NEWINTRA (currMB))
  1884.       {
  1885.         // =============== 4x4 itrans ================
  1886.         // -------------------------------------------
  1887.         if (uv==0) 
  1888.         {
  1889.           intrapred_luma_16x16(img, currMB, PLANE_U, currMB->i16mode);
  1890.           iMBtrans4x4(PLANE_U, img, smb); 
  1891.         }
  1892.         else
  1893.         {
  1894.           intrapred_luma_16x16(img, currMB, PLANE_V, currMB->i16mode);
  1895.           iMBtrans4x4(PLANE_V, img, smb); 
  1896.         }
  1897.       }      
  1898.       else if (currMB->mb_type == I4MB)
  1899.       {
  1900.         itrans_4x4 = (!lossless_qpprime) ? itrans4x4 : Inv_Residual_trans_4x4;
  1901.         for (block8x8 = 0; block8x8 < 4; block8x8++)
  1902.         {
  1903.           for (k = block8x8 * 4; k < block8x8 * 4 + 4; k ++)
  1904.           {
  1905.             i =  (decode_block_scan[k] & 3);
  1906.             j = ((decode_block_scan[k] >> 2) & 3);
  1907.             ioff = (i << 2);
  1908.             joff = (j << 2);
  1909.             i4   = img->block_x + i;
  1910.             j4   = img->block_y + j;
  1911.             j_pos = j4 * BLOCK_SIZE;
  1912.             i_pos = i4 * BLOCK_SIZE;
  1913.             // PREDICTION
  1914.             //===== INTRA PREDICTION =====
  1915.             if (intrapred(img, currMB, (ColorPlane) (uv + 1), ioff, joff, i4,j4)==SEARCH_SYNC)  /* make 4x4 prediction block mpr from given prediction img->mb_mode */
  1916.               return SEARCH_SYNC;                   /* bit error */
  1917.             // =============== 4x4 itrans ================
  1918.             // -------------------------------------------
  1919.             itrans_4x4 (img, (ColorPlane) (uv + 1), ioff, joff);
  1920.             for(jj=0;jj<BLOCK_SIZE;jj++)
  1921.             {
  1922.               for(ii=0;ii<BLOCK_SIZE;ii++)
  1923.               { 
  1924.                 dec_picture->imgUV[uv][j_pos + jj][i_pos + ii] = (imgpel) img->mb_rres[uv+1][jj + joff][ii + ioff]; // construct picture from 4x4 blocks
  1925.               }
  1926.             }
  1927.           }
  1928.         }
  1929.       } //I4MB      
  1930.       else if (currMB->mb_type == I8MB) 
  1931.       {
  1932.         itrans_8x8 = (!lossless_qpprime) ? itrans8x8 : Inv_Residual_trans_8x8;
  1933.         for (block8x8 = 0; block8x8 < 4; block8x8++)
  1934.         {
  1935.           //=========== 8x8 BLOCK TYPE ============
  1936.           ioff = 8 * (block8x8 & 0x01);
  1937.           joff = 8 * (block8x8 >> 1);
  1938.           //PREDICTION
  1939.           intrapred8x8(img, currMB, (ColorPlane) (uv + 1), block8x8); 
  1940.           //For residual DPCM
  1941.           itrans_8x8(img, currMB, (ColorPlane) (uv + 1), ioff, joff);      // use DCT transform and make 8x8 block m7 from prediction block mpr
  1942.           for(jj = joff; jj < joff + 8;jj++)
  1943.           {
  1944.             for(ii = ioff; ii < ioff + 8;ii++)
  1945.             { 
  1946.               dec_picture->imgUV[uv][img->pix_y + jj][img->pix_x + ii] = (imgpel) img->mb_rres[uv+1][jj][ii]; // construct picture from 4x4 blocks
  1947.             }
  1948.           }
  1949.         }
  1950.       } //I8MB
  1951.       else if ((img->type == P_SLICE) && (currMB->mb_type == PSKIP))
  1952.       {   
  1953.         block_size_x = MB_BLOCK_SIZE;
  1954.         block_size_y = MB_BLOCK_SIZE;
  1955.         i = 0;
  1956.         j = 0;
  1957.         pred_dir = LIST_0;
  1958.         perform_mc((ColorPlane) (uv + 1), dec_picture, img,  pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1959.         mpr = img->mb_pred[uv+1];
  1960.         curComp = dec_picture->imgUV[uv];
  1961.         for(j = 0; j < img->mb_size[0][1]; j++)
  1962.         {                
  1963.           memcpy(&(curComp[img->pix_y + j][img->pix_x]), &(mpr[j][0]), img->mb_size[0][0] * sizeof(imgpel));
  1964.         }
  1965.       } //PSKIP
  1966.       else if (currMB->mb_type == P16x16)
  1967.       {
  1968.         block_size_x = MB_BLOCK_SIZE;
  1969.         block_size_y = MB_BLOCK_SIZE;
  1970.         i = 0;
  1971.         j = 0;
  1972.         pred_dir = currMB->b8pdir[0];  
  1973.         perform_mc((ColorPlane) (uv + 1), dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  1974.         iTransform(img, currMB, (ColorPlane) (uv + 1), need_4x4_transform, smb);   
  1975.       } //P16x16
  1976.       else if (currMB->mb_type == P16x8)
  1977.       {   
  1978.         block_size_x = MB_BLOCK_SIZE;
  1979.         block_size_y = 8;
  1980.         for (block8x8 = 0; block8x8 < 4; block8x8 += 2)
  1981.         {
  1982.           i = 0;
  1983.           j = block8x8;      
  1984.           pred_dir = currMB->b8pdir[block8x8];
  1985.           perform_mc((ColorPlane) (uv + 1), dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);          
  1986.         }
  1987.         iTransform(img, currMB, (ColorPlane) (uv + 1), need_4x4_transform, smb);
  1988.       } //P16x8      
  1989.       else if (currMB->mb_type == P8x16)
  1990.       {   
  1991.         block_size_x = 8;
  1992.         block_size_y = 16;
  1993.         for (block8x8 = 0; block8x8 < 2; block8x8 ++)
  1994.         {
  1995.           i = block8x8<<1;
  1996.           j = 0;      
  1997.           pred_dir = currMB->b8pdir[block8x8];
  1998.           assert (pred_dir<=2);
  1999.           perform_mc((ColorPlane) (uv + 1), dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  2000.         }
  2001.         iTransform(img, currMB, (ColorPlane) (uv + 1), need_4x4_transform, smb);
  2002.       } //P8x16
  2003.       else
  2004.       {
  2005.         // prepare direct modes
  2006.         if (img->type==B_SLICE && img->direct_spatial_mv_pred_flag && (IS_DIRECT (currMB) ||
  2007.           (IS_P8x8(currMB) && !(currMB->b8mode[0] && currMB->b8mode[1] && currMB->b8mode[2] && currMB->b8mode[3]))))
  2008.           prepare_direct_params(currMB, dec_picture, img, pmvl0, pmvl1, &l0_rFrame, &l1_rFrame);
  2009.         for (block8x8=0; block8x8<4; block8x8++)
  2010.         {
  2011.           mv_mode  = currMB->b8mode[block8x8];
  2012.           pred_dir = currMB->b8pdir[block8x8];
  2013.           //if ( mv_mode == SMB8x8 || mv_mode == SMB8x4 || mv_mode == SMB4x8 || mv_mode == SMB4x4 )
  2014.           if ( mv_mode != 0 )
  2015.           {
  2016.             int k_start = (block8x8 << 2);
  2017.             int k_inc = (mv_mode == SMB8x4) ? 2 : 1;
  2018.             int k_end = (mv_mode == SMB8x8) ? k_start + 1 : ((mv_mode == SMB4x4) ? k_start + 4 : k_start + k_inc + 1);
  2019.             block_size_x = ( mv_mode == SMB8x4 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
  2020.             block_size_y = ( mv_mode == SMB4x8 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
  2021.             for (k = k_start; k < k_end; k += k_inc)
  2022.             {
  2023.               i =  (decode_block_scan[k] & 3);
  2024.               j = ((decode_block_scan[k] >> 2) & 3);
  2025.               perform_mc((ColorPlane) (uv + 1), dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  2026.             }        
  2027.           }
  2028.           else
  2029.           {
  2030.             int k_start = (block8x8 << 2);
  2031.             int k_end = k_start;
  2032.             if (active_sps->direct_8x8_inference_flag)
  2033.             {
  2034.               block_size_x = SMB_BLOCK_SIZE;
  2035.               block_size_y = SMB_BLOCK_SIZE;
  2036.               k_end ++;
  2037.             }
  2038.             else
  2039.             {
  2040.               block_size_x = BLOCK_SIZE;
  2041.               block_size_y = BLOCK_SIZE;
  2042.               k_end += BLOCK_MULTIPLE;
  2043.             }
  2044.             // Prepare mvs (needed for deblocking and mv prediction
  2045.             for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
  2046.             {
  2047.               i =  (decode_block_scan[k] & 3);
  2048.               j = ((decode_block_scan[k] >> 2) & 3);
  2049.               ioff = (i << 2);
  2050.               i4   = img->block_x + i;
  2051.               joff = (j << 2);
  2052.               j4   = img->block_y + j;
  2053.               assert (pred_dir<=2);
  2054.               if (img->direct_spatial_mv_pred_flag)
  2055.               {
  2056.                 j6 = img->block_y_aff + j;
  2057.                 //===== DIRECT PREDICTION =====              
  2058.                 if (l0_rFrame >=0)
  2059.                 {
  2060.                   if (!l0_rFrame  && ((!colocated->moving_block[j6][i4]) && (!listX[LIST_1 + list_offset][0]->is_long_term)))
  2061.                   {
  2062.                     dec_picture->motion.mv  [LIST_0][j4][i4][0] = 0;
  2063.                     dec_picture->motion.mv  [LIST_0][j4][i4][1] = 0;
  2064.                     dec_picture->motion.ref_idx[LIST_0][j4][i4] = 0;
  2065.                   }
  2066.                   else
  2067.                   {
  2068.                     dec_picture->motion.mv  [LIST_0][j4][i4][0] = pmvl0[0];
  2069.                     dec_picture->motion.mv  [LIST_0][j4][i4][1] = pmvl0[1];
  2070.                     dec_picture->motion.ref_idx[LIST_0][j4][i4] = l0_rFrame;
  2071.                   }
  2072.                 }
  2073.                 else
  2074.                 {
  2075.                   dec_picture->motion.ref_idx[LIST_0][j4][i4] = -1;
  2076.                   dec_picture->motion.mv  [LIST_0][j4][i4][0] = 0;
  2077.                   dec_picture->motion.mv  [LIST_0][j4][i4][1] = 0;
  2078.                 }
  2079.                 if (l1_rFrame >=0)
  2080.                 {
  2081.                   if  (l1_rFrame==0 && ((!colocated->moving_block[j6][i4]) && (!listX[LIST_1 + list_offset][0]->is_long_term)))
  2082.                   {
  2083.                     dec_picture->motion.mv  [LIST_1][j4][i4][0] = 0;
  2084.                     dec_picture->motion.mv  [LIST_1][j4][i4][1] = 0;
  2085.                     dec_picture->motion.ref_idx[LIST_1][j4][i4] = l1_rFrame;
  2086.                   }
  2087.                   else
  2088.                   {
  2089.                     dec_picture->motion.mv  [LIST_1][j4][i4][0] = pmvl1[0];
  2090.                     dec_picture->motion.mv  [LIST_1][j4][i4][1] = pmvl1[1];
  2091.                     dec_picture->motion.ref_idx[LIST_1][j4][i4] = l1_rFrame;
  2092.                   }
  2093.                 }
  2094.                 else
  2095.                 {
  2096.                   dec_picture->motion.mv  [LIST_1][j4][i4][0] = 0;
  2097.                   dec_picture->motion.mv  [LIST_1][j4][i4][1] = 0;
  2098.                   dec_picture->motion.ref_idx[LIST_1][j4][i4] = -1;
  2099.                 }
  2100.                 if (l0_rFrame < 0 && l1_rFrame < 0)
  2101.                 {
  2102.                   dec_picture->motion.ref_idx[LIST_0][j4][i4] = 0;
  2103.                   dec_picture->motion.ref_idx[LIST_1][j4][i4] = 0;
  2104.                 }
  2105.                 if      (dec_picture->motion.ref_idx[LIST_1][j4][i4]==-1) 
  2106.                 {
  2107.                   direct_pdir = 0;
  2108.                   ref_idx  = (dec_picture->motion.ref_idx[LIST_0][j4][i4] != -1) ? dec_picture->motion.ref_idx[LIST_0][j4][i4] : 0;
  2109.                 }
  2110.                 else if (dec_picture->motion.ref_idx[LIST_0][j4][i4]==-1) 
  2111.                 {
  2112.                   direct_pdir = 1;
  2113.                   ref_idx  = (dec_picture->motion.ref_idx[LIST_1][j4][i4] != -1) ? dec_picture->motion.ref_idx[LIST_1][j4][i4] : 0;
  2114.                 }
  2115.                 else                                               
  2116.                   direct_pdir = 2;
  2117.                 pred_dir = direct_pdir;
  2118.               }
  2119.               else
  2120.               {
  2121.                 j6= img->block_y_aff + j;
  2122.                 refList = (colocated->ref_idx[LIST_0][j6][i4]== -1 ? LIST_1 : LIST_0);
  2123.                 ref_idx =  colocated->ref_idx[refList][j6][i4];
  2124.                 if(ref_idx==-1) // co-located is intra mode
  2125.                 {
  2126.                   for(hv=0; hv<2; hv++)
  2127.                   {
  2128.                     dec_picture->motion.mv  [LIST_0][j4][i4][hv]=0;
  2129.                     dec_picture->motion.mv  [LIST_1][j4][i4][hv]=0;
  2130.                   }
  2131.                   dec_picture->motion.ref_idx[LIST_0][j4][i4] = 0;
  2132.                   dec_picture->motion.ref_idx[LIST_1][j4][i4] = 0;
  2133.                 }
  2134.                 else // co-located skip or inter mode
  2135.                 {
  2136.                   int mapped_idx=0;
  2137.                   int iref;
  2138.                   {
  2139.                     for (iref=0;iref<imin(img->num_ref_idx_l0_active,listXsize[LIST_0 + list_offset]);iref++)
  2140.                     {
  2141.                       if(img->structure==0 && curr_mb_field==0)
  2142.                       {
  2143.                         // If the current MB is a frame MB and the colocated is from a field picture,
  2144.                         // then the colocated->ref_pic_id may have been generated from the wrong value of
  2145.                         // frame_poc if it references it's complementary field, so test both POC values
  2146.                         if(listX[0][iref]->top_poc*2 == colocated->ref_pic_id[refList][j6][i4] || listX[0][iref]->bottom_poc*2 == colocated->ref_pic_id[refList][j6][i4])
  2147.                         {
  2148.                           mapped_idx=iref;
  2149.                           break;
  2150.                         }
  2151.                         else //! invalid index. Default to zero even though this case should not happen
  2152.                           mapped_idx=INVALIDINDEX;
  2153.                         continue;
  2154.                       }
  2155.                       if (dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][iref]==colocated->ref_pic_id[refList][j6][i4])
  2156.                       {
  2157.                         mapped_idx=iref;
  2158.                         break;
  2159.                       }
  2160.                       else //! invalid index. Default to zero even though this case should not happen
  2161.                       {
  2162.                         mapped_idx=INVALIDINDEX; 
  2163.                       }
  2164.                     }
  2165.                     if (INVALIDINDEX == mapped_idx)
  2166.                     {
  2167.                       error("temporal direct errorncolocated block has ref that is unavailable",-1111);
  2168.                     }
  2169.                   }
  2170.                   mv_scale = img->mvscale[LIST_0 + list_offset][mapped_idx];
  2171.                   //! In such case, an array is needed for each different reference.
  2172.                   if (mv_scale == 9999 || listX[LIST_0+list_offset][mapped_idx]->is_long_term)
  2173.                   {
  2174.                     dec_picture->motion.mv  [LIST_0][j4][i4][0]=colocated->mv[refList][j6][i4][0];
  2175.                     dec_picture->motion.mv  [LIST_0][j4][i4][1]=colocated->mv[refList][j6][i4][1];
  2176.                     dec_picture->motion.mv  [LIST_1][j4][i4][0]=0;
  2177.                     dec_picture->motion.mv  [LIST_1][j4][i4][1]=0;
  2178.                   }
  2179.                   else
  2180.                   {
  2181.                     dec_picture->motion.mv  [LIST_0][j4][i4][0] = (short) ((mv_scale * colocated->mv[refList][j6][i4][0] + 128 ) >> 8);
  2182.                     dec_picture->motion.mv  [LIST_0][j4][i4][1] = (short) ((mv_scale * colocated->mv[refList][j6][i4][1] + 128 ) >> 8);
  2183.                     dec_picture->motion.mv  [LIST_1][j4][i4][0] = (short) (dec_picture->motion.mv[LIST_0][j4][i4][0] - colocated->mv[refList][j6][i4][0]);
  2184.                     dec_picture->motion.mv  [LIST_1][j4][i4][1] = (short) (dec_picture->motion.mv[LIST_0][j4][i4][1] - colocated->mv[refList][j6][i4][1]);
  2185.                   }
  2186.                   dec_picture->motion.ref_idx[LIST_0][j4][i4] = (char) mapped_idx; //listX[1][0]->ref_idx[refList][j4][i4];
  2187.                   dec_picture->motion.ref_idx[LIST_1][j4][i4] = 0;                  
  2188.                 }
  2189.               }
  2190.               // store reference picture ID determined by direct mode
  2191.               dec_picture->motion.ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][(short)dec_picture->motion.ref_idx[LIST_0][j4][i4]];
  2192.               dec_picture->motion.ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_1 + list_offset][(short)dec_picture->motion.ref_idx[LIST_1][j4][i4]];
  2193.             }
  2194.             for (k = k_start; k < k_end; k ++)
  2195.             {
  2196.               i =  (decode_block_scan[k] & 3);
  2197.               j = ((decode_block_scan[k] >> 2) & 3);
  2198.               perform_mc((ColorPlane) (uv + 1), dec_picture, img, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
  2199.             }
  2200.           }
  2201.         }
  2202.         iTransform(img, currMB, (ColorPlane) (uv + 1), need_4x4_transform, smb);
  2203.       }
  2204.     } 
  2205.   }  
  2206.   return 0;
  2207. /*!
  2208.  ************************************************************************
  2209.  * brief
  2210.  *    change target plane
  2211.  *    for 4:4:4 Independent mode
  2212.  ************************************************************************
  2213.  */
  2214. void change_plane_JV( int nplane )
  2215. {
  2216.     img->colour_plane_id = nplane;
  2217.     img->mb_data = img->mb_data_JV[nplane];
  2218.     dec_picture = dec_picture_JV[nplane];
  2219.     Co_located = Co_located_JV[nplane];
  2220. }
  2221. /*!
  2222.  ************************************************************************
  2223.  * brief
  2224.  *    make frame picture from each plane data
  2225.  *    for 4:4:4 Independent mode
  2226.  ************************************************************************
  2227.  */
  2228. void make_frame_picture_JV()
  2229. {
  2230.   int uv, line;
  2231.   int nsize;
  2232.   int nplane;
  2233.   dec_picture = dec_picture_JV[0];
  2234.   // Copy Storable Params
  2235.   for( nplane=0; nplane<MAX_PLANE; nplane++ )
  2236.   {
  2237.     copy_storable_param_JV( &dec_picture->JVmotion[nplane], &dec_picture_JV[nplane]->motion );
  2238.   }
  2239.   // This could be done with pointers and seems not necessary
  2240.   for( uv=0; uv<2; uv++ )
  2241.   {
  2242.     for( line=0; line<img->height; line++ )
  2243.     {
  2244.       nsize = sizeof(imgpel) * img->width;
  2245.       memcpy( dec_picture->imgUV[uv][line], dec_picture_JV[uv+1]->imgY[line], nsize );
  2246.     }
  2247.     free_storable_picture(dec_picture_JV[uv+1]);
  2248.   }
  2249. }