b_frame.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:54k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*
  2. ***********************************************************************
  3. * COPYRIGHT AND WARRANTY INFORMATION
  4. *
  5. * Copyright 2001, International Telecommunications Union, Geneva
  6. *
  7. * DISCLAIMER OF WARRANTY
  8. *
  9. * These software programs are available to the user without any
  10. * license fee or royalty on an "as is" basis. The ITU disclaims
  11. * any and all warranties, whether express, implied, or
  12. * statutory, including any implied warranties of merchantability
  13. * or of fitness for a particular purpose.  In no event shall the
  14. * contributor or the ITU be liable for any incidental, punitive, or
  15. * consequential damages of any kind whatsoever arising from the
  16. * use of these programs.
  17. *
  18. * This disclaimer of warranty extends to the user of these programs
  19. * and user's customers, employees, agents, transferees, successors,
  20. * and assigns.
  21. *
  22. * The ITU does not represent or warrant that the programs furnished
  23. * hereunder are free of infringement of any third-party patents.
  24. * Commercial implementations of ITU-T Recommendations, including
  25. * shareware, may be subject to royalty fees to patent holders.
  26. * Information regarding the ITU-T patent policy is available from
  27. * the ITU Web site at http://www.itu.int.
  28. *
  29. * THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
  30. ************************************************************************
  31. */
  32. /*!
  33.  *************************************************************************************
  34.  * file b_frame.c
  35.  *
  36.  * brief
  37.  *    B picture coding
  38.  *
  39.  * author
  40.  *    Main contributors (see contributors.h for copyright, address and affiliation details)
  41.  *    - Byeong-Moon Jeon                <jeonbm@lge.com>
  42.  *    - Yoon-Seong Soh                  <yunsung@lge.com>
  43.  *    - Thomas Stockhammer              <stockhammer@ei.tum.de>
  44.  *    - Detlev Marpe                    <marpe@hhi.de>
  45.  *    - Guido Heising                   <heising@hhi.de>
  46.  *    - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
  47.  *************************************************************************************
  48.  */
  49. #include <stdlib.h>
  50. #include <math.h>
  51. #include <memory.h>
  52. #include "elements.h"
  53. #include "b_frame.h"
  54. #include "refbuf.h"
  55. #ifdef _ADAPT_LAST_GROUP_
  56. extern int *last_P_no;
  57. #endif
  58. /*!
  59.  ************************************************************************
  60.  * brief
  61.  *    Set reference frame information in global arrays
  62.  *    depending on mode decision. Used for motion vector prediction.
  63.  ************************************************************************
  64.  */
  65. void SetRefFrameInfo_B()
  66. {
  67.   int i,j;
  68.   const int fw_predframe_no = img->mb_data[img->current_mb_nr].ref_frame;
  69.   if(img->imod==B_Direct)
  70.   {
  71.     for (j = 0; j < 4;j++)
  72.     {
  73.       for (i = 0; i < 4;i++)
  74.       {
  75.         fw_refFrArr[img->block_y+j][img->block_x+i] =
  76.             bw_refFrArr[img->block_y+j][img->block_x+i] = -1;
  77.       }
  78.     }
  79.   }
  80.   else
  81.     if (img->imod == B_Forward)
  82.     {
  83.       for (j = 0;j < 4;j++)
  84.       {
  85.         for (i = 0;i < 4;i++)
  86.         {
  87.           fw_refFrArr[img->block_y+j][img->block_x+i] = fw_predframe_no;
  88.           bw_refFrArr[img->block_y+j][img->block_x+i] = -1;
  89.         }
  90.       }
  91.     }
  92.     else
  93.       if(img->imod == B_Backward)
  94.       {
  95.         for (j = 0;j < 4;j++)
  96.         {
  97.           for (i = 0;i < 4;i++)
  98.           {
  99.             fw_refFrArr[img->block_y+j][img->block_x+i] = -1;
  100.             bw_refFrArr[img->block_y+j][img->block_x+i] = 0;
  101.           }
  102.         }
  103.       }
  104.       else
  105.         if(img->imod == B_Bidirect)
  106.         {
  107.           for (j = 0;j < 4;j++)
  108.           {
  109.             for (i = 0;i < 4;i++)
  110.             {
  111.               fw_refFrArr[img->block_y+j][img->block_x+i] = fw_predframe_no;
  112.               bw_refFrArr[img->block_y+j][img->block_x+i] = 0;
  113.             }
  114.           }
  115.         }
  116.         else // 4x4-, 16x16-intra
  117.         {
  118.           for (j = 0;j < 4;j++)
  119.           {
  120.             for (i = 0;i < 4;i++)
  121.             {
  122.               fw_refFrArr[img->block_y+j][img->block_x+i] =
  123.                 bw_refFrArr[img->block_y+j][img->block_x+i] = -1;
  124.             }
  125.           }
  126.         }
  127. }
  128. /*!
  129.  ************************************************************************
  130.  * brief
  131.  *    Performs DCT, quantization, run/level pre-coding and IDCT
  132.  *    for the MC-compensated MB residue of a B-frame;
  133.  *    current cbp (for LUMA only) is affected
  134.  ************************************************************************
  135.  */
  136. void LumaResidualCoding_B()
  137. {
  138.   int cbp_mask, cbp_blk_mask, sum_cnt_nonz, coeff_cost, nonzero;
  139.   int mb_y, mb_x, block_y, block_x, i, j, pic_pix_y, pic_pix_x, pic_block_x, pic_block_y;
  140.   int ii4, jj4, iii4, jjj4, i2, j2, fw_pred, bw_pred, ref_inx, df_pred, db_pred;
  141.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  142.   switch(img->imod)
  143.   {
  144.     case B_Forward :
  145.       currMB->cbp     = 0 ;
  146.       currMB->cbp_blk = 0 ;
  147.       sum_cnt_nonz    = 0 ;
  148.       for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  149.       {
  150.         for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  151.         {
  152.           cbp_mask=(int)pow(2,(mb_x/8+mb_y/4));
  153.           coeff_cost=0;
  154.           for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  155.           {
  156.             pic_pix_y=img->pix_y+block_y;
  157.             pic_block_y=pic_pix_y/BLOCK_SIZE;
  158.             for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  159.             {
  160.               cbp_blk_mask = (block_x>>2)+ block_y ;
  161.               pic_pix_x=img->pix_x+block_x;
  162.               pic_block_x=pic_pix_x/BLOCK_SIZE;
  163.               img->ipredmode[pic_block_x+1][pic_block_y+1]=0;
  164.               if(input->mv_res)
  165.               {
  166.                 ii4=(img->pix_x+block_x)*8+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  167.                 jj4=(img->pix_y+block_y)*8+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  168.                 for (j=0;j<4;j++)
  169.                 {
  170.                   j2=j*8;
  171.                   for (i=0;i<4;i++)
  172.                   {
  173.                     i2=i*8;
  174.                     img->mpr[i+block_x][j+block_y]=UMVPelY_18 (mref[img->fw_multframe_no], jj4+j2, ii4+i2); // refbuf
  175.                   }
  176.                 }
  177.               }
  178.               else
  179.               {
  180.                 ii4=(img->pix_x+block_x)*4+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  181.                 jj4=(img->pix_y+block_y)*4+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  182.                 for (j=0;j<4;j++)
  183.                 {
  184.                   j2=j*4;
  185.                   for (i=0;i<4;i++)
  186.                   {
  187.                     i2=i*4;
  188.                     img->mpr[i+block_x][j+block_y]=UMVPelY_14 (mref[img->fw_multframe_no], jj4+j2, ii4+i2); // refbuf
  189.                   }
  190.                 }
  191.               }
  192.               for (j=0; j < BLOCK_SIZE; j++)
  193.               {
  194.                 for (i=0; i < BLOCK_SIZE; i++)
  195.                 {
  196.                   img->m7[i][j]=
  197.                     imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-img->mpr[i+block_x][j+block_y];
  198.                 }
  199.               }
  200.               nonzero=dct_luma(block_x,block_y,&coeff_cost);
  201.               if (nonzero)
  202.               {
  203.                 currMB->cbp_blk |= 1 << cbp_blk_mask ;            // one bit for every 4x4 block
  204.                 currMB->cbp     |= cbp_mask;
  205.               }
  206.             } // block_x
  207.           } // block_y
  208.           if (coeff_cost > 3)
  209.           {
  210.             sum_cnt_nonz += coeff_cost;
  211.           }
  212.           else //discard
  213.           {
  214.             currMB->cbp     &=  (63-cbp_mask);
  215.             currMB->cbp_blk &= ~(51 << (mb_y + (mb_x>>2) )) ;
  216.             for (i=mb_x; i < mb_x+BLOCK_SIZE*2; i++)
  217.             {
  218.               for (j=mb_y; j < mb_y+BLOCK_SIZE*2; j++)
  219.               {
  220.                 imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  221.               }
  222.             }
  223.           }
  224.         } // mb_x
  225.       } // mb_y
  226.       if (sum_cnt_nonz <= 5 )
  227.       {
  228.         currMB->cbp     &= 0xfffff0 ;
  229.         currMB->cbp_blk &= 0xff0000 ;
  230.         for (i=0; i < MB_BLOCK_SIZE; i++)
  231.         {
  232.           for (j=0; j < MB_BLOCK_SIZE; j++)
  233.           {
  234.             imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  235.           }
  236.         }
  237.       }
  238.       break;
  239.     case B_Backward :
  240.       currMB->cbp     = 0 ;
  241.       currMB->cbp_blk = 0 ;
  242.       sum_cnt_nonz    = 0 ;
  243.       for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  244.       {
  245.         for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  246.         {
  247.           cbp_mask=(int)pow(2,(mb_x/8+mb_y/4));
  248.           coeff_cost=0;
  249.           for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  250.           {
  251.             pic_pix_y=img->pix_y+block_y;
  252.             pic_block_y=pic_pix_y/BLOCK_SIZE;
  253.             for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  254.             {
  255.               cbp_blk_mask = (block_x>>2)+ block_y ;
  256.               pic_pix_x=img->pix_x+block_x;
  257.               pic_block_x=pic_pix_x/BLOCK_SIZE;
  258.               img->ipredmode[pic_block_x+1][pic_block_y+1]=0;
  259.               if(input->mv_res)
  260.               {
  261.                 iii4=(img->pix_x+block_x)*8+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  262.                 jjj4=(img->pix_y+block_y)*8+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  263.                 for (j=0;j<4;j++)
  264.                 {
  265.                   j2=j*8;
  266.                   for (i=0;i<4;i++)
  267.                   {
  268.                     i2=i*8;
  269.                     img->mpr[i+block_x][j+block_y]=UMVPelY_18 (mref_P, jjj4+j2, iii4+i2); // refbuf
  270.                   }
  271.                 }
  272.               }
  273.               else
  274.               {
  275.                 iii4=(img->pix_x+block_x)*4+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  276.                 jjj4=(img->pix_y+block_y)*4+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  277.                 for (j=0;j<4;j++)
  278.                 {
  279.                   j2=j*4;
  280.                   for (i=0;i<4;i++)
  281.                   {
  282.                     i2=i*4;
  283.                     img->mpr[i+block_x][j+block_y]=UMVPelY_14 (mref_P, jjj4+j2, iii4+i2); // refbuf
  284.                   }
  285.                 }
  286.               }
  287.               for (j=0; j < BLOCK_SIZE; j++)
  288.               {
  289.                 for (i=0; i < BLOCK_SIZE; i++)
  290.                 {
  291.                   img->m7[i][j]=
  292.                     imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-img->mpr[i+block_x][j+block_y];
  293.                 }
  294.               }
  295.               nonzero=dct_luma(block_x,block_y,&coeff_cost);
  296.               if (nonzero)
  297.               {
  298.                 currMB->cbp_blk |= 1 << cbp_blk_mask ;            // one bit for every 4x4 block
  299.                 currMB->cbp     |= cbp_mask;
  300.               }
  301.             } // block_x
  302.           } // block_y
  303.           if (coeff_cost > 3)
  304.           {
  305.             sum_cnt_nonz += coeff_cost;
  306.           }
  307.           else //discard
  308.           {
  309.             currMB->cbp     &= (63-cbp_mask);
  310.             currMB->cbp_blk &= ~(51 << (mb_y + (mb_x>>2) )) ;
  311.             for (i=mb_x; i < mb_x+BLOCK_SIZE*2; i++)
  312.             {
  313.               for (j=mb_y; j < mb_y+BLOCK_SIZE*2; j++)
  314.               {
  315.                 imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  316.               }
  317.             }
  318.           }
  319.         } // mb_x
  320.       } // mb_y
  321.       if (sum_cnt_nonz <= 5 )
  322.       {
  323.         currMB->cbp     &= 0xfffff0 ;
  324.         currMB->cbp_blk &= 0xff0000 ;
  325.         for (i=0; i < MB_BLOCK_SIZE; i++)
  326.         {
  327.           for (j=0; j < MB_BLOCK_SIZE; j++)
  328.           {
  329.             imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  330.           }
  331.         }
  332.       }
  333.       break;
  334.     case B_Bidirect :
  335.       currMB->cbp=0;
  336.       currMB->cbp_blk=0;
  337.       sum_cnt_nonz=0;
  338.       for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  339.       {
  340.         for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  341.         {
  342.           cbp_mask=(int)pow(2,(mb_x/8+mb_y/4));
  343.           coeff_cost=0;
  344.           for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  345.           {
  346.             pic_pix_y=img->pix_y+block_y;
  347.             pic_block_y=pic_pix_y/BLOCK_SIZE;
  348.             for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  349.             {
  350.               cbp_blk_mask = (block_x>>2)+ block_y ;
  351.               pic_pix_x=img->pix_x+block_x;
  352.               pic_block_x=pic_pix_x/BLOCK_SIZE;
  353.               img->ipredmode[pic_block_x+1][pic_block_y+1]=0;
  354.               if(input->mv_res)
  355.               {
  356.                 ii4=(img->pix_x+block_x)*8+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  357.                 jj4=(img->pix_y+block_y)*8+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  358.                 iii4=(img->pix_x+block_x)*8+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  359.                 jjj4=(img->pix_y+block_y)*8+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  360.                 for (j=0;j<4;j++)
  361.                 {
  362.                   j2=j*8;
  363.                   for (i=0;i<4;i++)
  364.                   {
  365.                     i2=i*8;
  366.                     fw_pred=UMVPelY_18 (mref[img->fw_multframe_no], jj4+j2, ii4+i2);  // refbuf
  367.                     bw_pred=UMVPelY_18 (mref_P, jjj4+j2, iii4+i2);  // refbuf
  368.                     img->mpr[i+block_x][j+block_y]=(int)((fw_pred+bw_pred)/2.0+0.5);
  369.                   }
  370.                 }
  371.               }
  372.               else
  373.               {
  374.                 ii4=(img->pix_x+block_x)*4+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  375.                 jj4=(img->pix_y+block_y)*4+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  376.                 iii4=(img->pix_x+block_x)*4+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  377.                 jjj4=(img->pix_y+block_y)*4+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  378.                 for (j=0;j<4;j++)
  379.                 {
  380.                   j2=j*4;
  381.                   for (i=0;i<4;i++)
  382.                   {
  383.                     i2=i*4;
  384.                     fw_pred=UMVPelY_14 (mref[img->fw_multframe_no], jj4+j2, ii4+i2);  // refbuf
  385.                     bw_pred=UMVPelY_14 (mref_P, jjj4+j2, iii4+i2);  // refbuf
  386.                     img->mpr[i+block_x][j+block_y]=(int)((fw_pred+bw_pred)/2.0+0.5);
  387.                   }
  388.                 }
  389.               }
  390.               for (j=0; j < BLOCK_SIZE; j++)
  391.               {
  392.                 for (i=0; i < BLOCK_SIZE; i++)
  393.                 {
  394.                   img->m7[i][j]=
  395.                     imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-img->mpr[i+block_x][j+block_y];
  396.                 }
  397.               }
  398.               nonzero=dct_luma(block_x,block_y,&coeff_cost);
  399.               if (nonzero)
  400.               {
  401.                 currMB->cbp_blk |= 1 << cbp_blk_mask ;            // one bit for every 4x4 block
  402.                 currMB->cbp     |= cbp_mask;
  403.               }
  404.             } // block_x
  405.           } // block_y
  406.           if (coeff_cost > 3)
  407.           {
  408.             sum_cnt_nonz += coeff_cost;
  409.           }
  410.           else //discard
  411.           {
  412.             currMB->cbp     &= (63-cbp_mask);
  413.             currMB->cbp_blk &= ~(51 << (mb_y + (mb_x>>2) )) ;
  414.             for (i=mb_x; i < mb_x+BLOCK_SIZE*2; i++)
  415.             {
  416.               for (j=mb_y; j < mb_y+BLOCK_SIZE*2; j++)
  417.               {
  418.                 imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  419.               }
  420.             }
  421.           }
  422.         } // mb_x
  423.       } // mb_y
  424.       if (sum_cnt_nonz <= 5 )
  425.       {
  426.         currMB->cbp     &= 0xfffff0 ;
  427.         currMB->cbp_blk &= 0xff0000 ;
  428.         for (i=0; i < MB_BLOCK_SIZE; i++)
  429.         {
  430.           for (j=0; j < MB_BLOCK_SIZE; j++)
  431.           {
  432.             imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  433.           }
  434.         }
  435.       }
  436.       break;
  437.     case B_Direct :
  438.       currMB->cbp=0;
  439.       currMB->cbp_blk=0;
  440.       sum_cnt_nonz=0;
  441.       for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  442.       {
  443.         for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  444.         {
  445.           cbp_mask=(int)pow(2,(mb_x/8+mb_y/4));
  446.           coeff_cost=0;
  447.           for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  448.           {
  449.             pic_pix_y=img->pix_y+block_y;
  450.             pic_block_y=pic_pix_y/BLOCK_SIZE;
  451.             for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  452.             {
  453.               cbp_blk_mask = (block_x>>2)+ block_y ;
  454.               pic_pix_x=img->pix_x+block_x;
  455.               pic_block_x=pic_pix_x/BLOCK_SIZE;
  456.               img->ipredmode[pic_block_x+1][pic_block_y+1]=0;
  457.               if(input->mv_res)
  458.               {
  459.                 ii4 =(img->pix_x+block_x)*8+dfMV[0][pic_block_y][pic_block_x+4];
  460.                 jj4 =(img->pix_y+block_y)*8+dfMV[1][pic_block_y][pic_block_x+4];
  461.                 iii4=(img->pix_x+block_x)*8+dbMV[0][pic_block_y][pic_block_x+4];
  462.                 jjj4=(img->pix_y+block_y)*8+dbMV[1][pic_block_y][pic_block_x+4];
  463.                 // next P is intra mode
  464.                 if(refFrArr[pic_block_y][pic_block_x]==-1)
  465.                   ref_inx=(img->number-1)%img->buf_cycle;
  466.                 // next P is skip or inter mode
  467.                 else
  468.                   ref_inx=(img->number-refFrArr[pic_block_y][pic_block_x]-1)%img->buf_cycle;
  469.                 for (j=0;j<4;j++)
  470.                 {
  471.                   j2=j*8;
  472.                   for (i=0;i<4;i++)
  473.                   {
  474.                     i2=i*8;
  475.                     df_pred=UMVPelY_18 (mref[ref_inx], jj4+j2, ii4+i2);
  476.                     db_pred=UMVPelY_18 (mref_P, jjj4+j2, iii4+i2);
  477.                     img->mpr[i+block_x][j+block_y]=(int)((df_pred+db_pred)/2.0+0.5);
  478.                   }
  479.                 }
  480.               }
  481.               else
  482.               {
  483.                 ii4 =(img->pix_x+block_x)*4+dfMV[0][pic_block_y][pic_block_x+4];
  484.                 jj4 =(img->pix_y+block_y)*4+dfMV[1][pic_block_y][pic_block_x+4];
  485.                 iii4=(img->pix_x+block_x)*4+dbMV[0][pic_block_y][pic_block_x+4];
  486.                 jjj4=(img->pix_y+block_y)*4+dbMV[1][pic_block_y][pic_block_x+4];
  487.                 // next P is intra mode
  488.                 if(refFrArr[pic_block_y][pic_block_x]==-1)
  489.                   ref_inx=(img->number-1)%img->buf_cycle;
  490.                 // next P is skip or inter mode
  491.                 else
  492.                   ref_inx=(img->number-refFrArr[pic_block_y][pic_block_x]-1)%img->buf_cycle;
  493.                 for (j=0;j<4;j++)
  494.                 {
  495.                   j2=j*4;
  496.                   for (i=0;i<4;i++)
  497.                   {
  498.                     i2=i*4;
  499.                     df_pred=UMVPelY_14 (mref[ref_inx], jj4+j2, ii4+i2);
  500.                     db_pred=UMVPelY_14 (mref_P, jjj4+j2, iii4+i2);
  501.                     img->mpr[i+block_x][j+block_y]=(int)((df_pred+db_pred)/2.0+0.5);
  502.                   }
  503.                 }
  504.               }
  505.               // LG : direct residual coding
  506.               for (j=0; j < BLOCK_SIZE; j++)
  507.               {
  508.                 for (i=0; i < BLOCK_SIZE; i++)
  509.                 {
  510.                   img->m7[i][j]=
  511.                     imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-img->mpr[i+block_x][j+block_y];
  512.                 }
  513.               }
  514.               nonzero=dct_luma(block_x,block_y,&coeff_cost);
  515.               if (nonzero)
  516.               {
  517.                 currMB->cbp_blk |= 1 << cbp_blk_mask ;            // one bit for every 4x4 block
  518.                 currMB->cbp     |= cbp_mask;
  519.               }
  520.             } // block_x
  521.           } // block_y
  522.           // LG : direct residual coding
  523.           if (coeff_cost > 3)
  524.           {
  525.             sum_cnt_nonz += coeff_cost;
  526.           }
  527.           else //discard
  528.           {
  529.             currMB->cbp     &= (63-cbp_mask);
  530.             currMB->cbp_blk &= ~(51 << (mb_y + (mb_x>>2) )) ;
  531.             for (i=mb_x; i < mb_x+BLOCK_SIZE*2; i++)
  532.             {
  533.               for (j=mb_y; j < mb_y+BLOCK_SIZE*2; j++)
  534.               {
  535.                 imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  536.               }
  537.             }
  538.           }
  539.         } // mb_x
  540.       } // mb_y
  541.       // LG : direct residual coding
  542.       if (sum_cnt_nonz <= 5 )
  543.       {
  544.         currMB->cbp     &= 0xfffff0 ;
  545.         currMB->cbp_blk &= 0xff0000 ;
  546.         for (i=0; i < MB_BLOCK_SIZE; i++)
  547.         {
  548.           for (j=0; j < MB_BLOCK_SIZE; j++)
  549.           {
  550.             imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
  551.           }
  552.         }
  553.       }
  554.       break;
  555.     default:
  556.       break;
  557.   } // switch()
  558. }
  559. /*!
  560.  ************************************************************************
  561.  * brief
  562.  *    Performs DCT, quantization, run/level pre-coding and IDCT
  563.  *    for the chrominance of a B-frame macroblock;
  564.  *    current cbp and cr_cbp are affected
  565.  ************************************************************************
  566.  */
  567. void ChromaCoding_B(int *cr_cbp)
  568. {
  569.   int i, j;
  570.   int uv, ii,jj,ii0,jj0,ii1,jj1,if1,jf1,if0,jf0,f1,f2,f3,f4;
  571.   int pic_block_y, pic_block_x, ref_inx, fw_pred, bw_pred;
  572.   Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  573.   if(input->mv_res)
  574.   {
  575.     f1=16;
  576.     f2=15;
  577.   }
  578.   else
  579.   {
  580.     f1=8;
  581.     f2=7;
  582.   }
  583.   f3=f1*f1;
  584.   f4=f3/2;
  585.   *cr_cbp=0;
  586.   for (uv=0; uv < 2; uv++)
  587.   {
  588.     if (img->imod == INTRA_MB_OLD || img->imod == INTRA_MB_NEW)
  589.     {
  590.       intrapred_chroma(img->pix_c_x,img->pix_c_y,uv);
  591.     }
  592.     else if(img->imod == B_Forward)
  593.     {
  594.       for (j=0; j < MB_BLOCK_SIZE/2; j++)
  595.       {
  596.         pic_block_y=(img->pix_c_y+j)/2;
  597.         for (i=0; i < MB_BLOCK_SIZE/2; i++)
  598.         {
  599.           pic_block_x=(img->pix_c_x+i)/2;
  600.           ii=(img->pix_c_x+i)*f1+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  601.           jj=(img->pix_c_y+j)*f1+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  602.           ii0= max (0, min (img->width_cr-1, ii/f1));
  603.           jj0= max (0, min (img->height_cr-1, jj/f1));
  604.           ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
  605.           jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
  606.           if1=(ii & f2);
  607.           jf1=(jj & f2);
  608.           if0=f1-if1;
  609.           jf0=f1-jf1;
  610.           img->mpr[i][j]=(if0*jf0*mcef[img->fw_multframe_no][uv][jj0][ii0]+
  611.                   if1*jf0*mcef[img->fw_multframe_no][uv][jj0][ii1]+
  612.                   if0*jf1*mcef[img->fw_multframe_no][uv][jj1][ii0]+
  613.                   if1*jf1*mcef[img->fw_multframe_no][uv][jj1][ii1]+f4)/f3;
  614.           img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j];
  615.         }
  616.       }
  617.     }
  618.     else if(img->imod == B_Backward)
  619.     {
  620.       for (j=0; j < MB_BLOCK_SIZE/2; j++)
  621.       {
  622.         pic_block_y=(img->pix_c_y+j)/2;
  623.         for (i=0; i < MB_BLOCK_SIZE/2; i++)
  624.         {
  625.           pic_block_x=(img->pix_c_x+i)/2;
  626.           ii=(img->pix_c_x+i)*f1+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  627.           jj=(img->pix_c_y+j)*f1+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  628.           ii0= max (0, min (img->width_cr-1, ii/f1));
  629.           jj0= max (0, min (img->height_cr-1, jj/f1));
  630.           ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
  631.           jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
  632.           if1=(ii & f2);
  633.           jf1=(jj & f2);
  634.           if0=f1-if1;
  635.           jf0=f1-jf1;
  636.           img->mpr[i][j]=(if0*jf0*mcef_P[uv][jj0][ii0]+if1*jf0*mcef_P[uv][jj0][ii1]+
  637.                     if0*jf1*mcef_P[uv][jj1][ii0]+if1*jf1*mcef_P[uv][jj1][ii1]+f4)/f3;
  638.           img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j];
  639.         }
  640.       }
  641.     }
  642.     else if(img->imod == B_Bidirect)
  643.     {
  644.       for (j=0; j < MB_BLOCK_SIZE/2; j++)
  645.       {
  646.         pic_block_y=(img->pix_c_y+j)/2;
  647.         for (i=0; i < MB_BLOCK_SIZE/2; i++)
  648.         {
  649.           pic_block_x=(img->pix_c_x+i)/2;
  650.           ii=(img->pix_c_x+i)*f1+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  651.           jj=(img->pix_c_y+j)*f1+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  652.           ii0= max (0, min (img->width_cr-1, ii/f1));
  653.           jj0= max (0, min (img->height_cr-1, jj/f1));
  654.           ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
  655.           jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
  656.           if1=(ii & f2);
  657.           jf1=(jj & f2);
  658.           if0=f1-if1;
  659.           jf0=f1-jf1;
  660.           fw_pred=(if0*jf0*mcef[img->fw_multframe_no][uv][jj0][ii0]+
  661.                if1*jf0*mcef[img->fw_multframe_no][uv][jj0][ii1]+
  662.                if0*jf1*mcef[img->fw_multframe_no][uv][jj1][ii0]+
  663.                if1*jf1*mcef[img->fw_multframe_no][uv][jj1][ii1]+f4)/f3;
  664.           ii=(img->pix_c_x+i)*f1+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  665.           jj=(img->pix_c_y+j)*f1+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  666.           ii0= max (0, min (img->width_cr-1, ii/f1));
  667.           jj0= max (0, min (img->height_cr-1, jj/f1));
  668.           ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
  669.           jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
  670.           if1=(ii & f2);
  671.           jf1=(jj & f2);
  672.           if0=f1-if1;
  673.           jf0=f1-jf1;
  674.           bw_pred=(if0*jf0*mcef_P[uv][jj0][ii0]+if1*jf0*mcef_P[uv][jj0][ii1]+
  675.                if0*jf1*mcef_P[uv][jj1][ii0]+if1*jf1*mcef_P[uv][jj1][ii1]+f4)/f3;
  676.           img->mpr[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);
  677.           img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j];
  678.         }
  679.       }
  680.     }
  681.     else // img->imod == B_Direct
  682.     {
  683.       for (j=0; j < MB_BLOCK_SIZE/2; j++)
  684.       {
  685.         pic_block_y=(img->pix_c_y+j)/2;
  686.         for (i=0; i < MB_BLOCK_SIZE/2; i++)
  687.         {
  688.           pic_block_x=(img->pix_c_x+i)/2;
  689.           ii=(img->pix_c_x+i)*f1+dfMV[0][pic_block_y][pic_block_x+4];
  690.           jj=(img->pix_c_y+j)*f1+dfMV[1][pic_block_y][pic_block_x+4];
  691.           ii0= max (0, min (img->width_cr-1, ii/f1));
  692.           jj0= max (0, min (img->height_cr-1, jj/f1));
  693.           ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
  694.           jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
  695.           if1=(ii & f2);
  696.           jf1=(jj & f2);
  697.           if0=f1-if1;
  698.           jf0=f1-jf1;
  699.           // next P is intra mode
  700.           if(refFrArr[pic_block_y][pic_block_x]==-1)
  701.             ref_inx=(img->number-1)%img->buf_cycle;
  702.           // next P is skip or inter mode
  703.           else
  704.             ref_inx=(img->number-refFrArr[pic_block_y][pic_block_x]-1)%img->buf_cycle;
  705.           fw_pred=(if0*jf0*mcef[ref_inx][uv][jj0][ii0]+
  706.                if1*jf0*mcef[ref_inx][uv][jj0][ii1]+
  707.                if0*jf1*mcef[ref_inx][uv][jj1][ii0]+
  708.                if1*jf1*mcef[ref_inx][uv][jj1][ii1]+f4)/f3;
  709.           ii=(img->pix_c_x+i)*f1+dbMV[0][pic_block_y][pic_block_x+4];
  710.           jj=(img->pix_c_y+j)*f1+dbMV[1][pic_block_y][pic_block_x+4];
  711.           ii0= max (0, min (img->width_cr-1, ii/f1));
  712.           jj0= max (0, min (img->height_cr-1, jj/f1));
  713.           ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
  714.           jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
  715.           if1=(ii & f2);
  716.           jf1=(jj & f2);
  717.           if0=f1-if1;
  718.           jf0=f1-jf1;
  719.           bw_pred=(if0*jf0*mcef_P[uv][jj0][ii0]+if1*jf0*mcef_P[uv][jj0][ii1]+
  720.                if0*jf1*mcef_P[uv][jj1][ii0]+if1*jf1*mcef_P[uv][jj1][ii1]+f4)/f3;
  721.           img->mpr[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);
  722.           // LG : direct residual coding
  723.           img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j];
  724.         }
  725.       }
  726.     }
  727.     *cr_cbp=dct_chroma(uv, *cr_cbp);
  728.   }
  729.   // LG : direct residual coding
  730.   currMB->cbp += *cr_cbp*16;
  731. }
  732. /*!
  733.  ************************************************************************
  734.  * brief
  735.  *    Passes for a given MB of a B picture the reference frame
  736.  *    parameter and motion parameters to the NAL
  737.  ************************************************************************
  738.  */
  739. int writeMotionInfo2NAL_Bframe()
  740. {
  741.   int i,j,k,l,m;
  742.   int step_h,step_v;
  743.   int curr_mvd=0, fw_blk_shape=0, bw_blk_shape=0;
  744.   int mb_nr = img->current_mb_nr;
  745.   Macroblock *currMB = &img->mb_data[mb_nr];
  746.   const int fw_predframe_no=currMB->ref_frame;
  747.   SyntaxElement *currSE = &img->MB_SyntaxElements[currMB->currSEnr];
  748.   int *bitCount = currMB->bitcounter;
  749.   Slice *currSlice = img->currentSlice;
  750.   DataPartition *dataPart;
  751.   int *partMap = assignSE2partition[input->partition_mode];
  752.   int no_bits = 0;
  753.   // Write fw_predframe_no(Forward, Bidirect)
  754.   if(img->imod==B_Forward || img->imod==B_Bidirect)
  755.   {
  756. #ifdef _ADDITIONAL_REFERENCE_FRAME_
  757.     if (img->no_multpred > 1 || input->add_ref_frame > 0)
  758. #else
  759.       if (img->no_multpred > 1)
  760. #endif
  761.     {
  762.       currSE->value1 = currMB->ref_frame;
  763.       currSE->type = SE_REFFRAME;
  764.       if (input->symbol_mode == UVLC)
  765.         currSE->mapping = n_linfo2;
  766.       else
  767.         currSE->writing = writeRefFrame2Buffer_CABAC;
  768.       dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
  769.       dataPart->writeSyntaxElement( currSE, dataPart);
  770.       bitCount[BITS_INTER_MB]+=currSE->len;
  771.       no_bits += currSE->len;
  772. #if TRACE
  773.       snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_fw_Reference frame no %3d ", currMB->ref_frame);
  774. #endif
  775.       // proceed to next SE
  776.       currSE++;
  777.       currMB->currSEnr++;
  778.     }
  779.   }
  780.   // Write Blk_size(Bidirect)
  781.   if(img->imod==B_Bidirect)
  782.   {
  783.     // Write blockshape for forward pred
  784.     fw_blk_shape = BlkSize2CodeNumber(img->fw_blc_size_h, img->fw_blc_size_v);
  785.     currSE->value1 = fw_blk_shape;
  786.     currSE->type = SE_BFRAME;
  787.     if (input->symbol_mode == UVLC)
  788.       currSE->mapping = n_linfo2;
  789.     else
  790.       currSE->writing = writeBiDirBlkSize2Buffer_CABAC;
  791.     dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
  792.     dataPart->writeSyntaxElement( currSE, dataPart);
  793.     bitCount[BITS_INTER_MB]+=currSE->len;
  794.     no_bits += currSE->len;
  795. #if TRACE
  796.     snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_bidiret_fw_blk %3d x %3d ", img->fw_blc_size_h, img->fw_blc_size_v);
  797. #endif
  798.     // proceed to next SE
  799.     currSE++;
  800.     currMB->currSEnr++;
  801.     // Write blockshape for backward pred
  802.     bw_blk_shape = BlkSize2CodeNumber(img->bw_blc_size_h, img->bw_blc_size_v);
  803.     currSE->value1 = bw_blk_shape;
  804.     currSE->type = SE_BFRAME;
  805.     if (input->symbol_mode == UVLC)
  806.       currSE->mapping = n_linfo2;
  807.     else
  808.       currSE->writing = writeBiDirBlkSize2Buffer_CABAC;
  809.     dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
  810.     dataPart->writeSyntaxElement( currSE, dataPart);
  811.     bitCount[BITS_INTER_MB]+=currSE->len;
  812.     no_bits += currSE->len;
  813. #if TRACE
  814.     snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_bidiret_bw_blk %3d x %3d ", img->bw_blc_size_h, img->bw_blc_size_v);
  815. #endif
  816.     // proceed to next SE
  817.     currSE++;
  818.     currMB->currSEnr++;
  819.   }
  820.   // Write MVDFW(Forward, Bidirect)
  821.   if(img->imod==B_Forward || img->imod==B_Bidirect)
  822.   {
  823.     step_h=img->fw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize
  824.     step_v=img->fw_blc_size_v/BLOCK_SIZE;  // vertical stepsize
  825.     for (j=0; j < BLOCK_SIZE; j += step_v)
  826.     {
  827.       for (i=0;i < BLOCK_SIZE; i += step_h)
  828.       {
  829.         for (k=0; k < 2; k++)
  830.         {
  831.           if(img->mb_mode==1) // fw 16x16
  832.             curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][k]);
  833.           else
  834.             if(img->mb_mode==3) // bidirectinal
  835.             {
  836.               switch(fw_blk_shape)
  837.               {
  838.                 case 0:
  839.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][k]);
  840.                   break;
  841.                 case 1:
  842.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][2][k]);
  843.                   break;
  844.                 case 2:
  845.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][3][k]);
  846.                   break;
  847.                 case 3:
  848.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][4][k]);
  849.                   break;
  850.                 case 4:
  851.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][5][k]);
  852.                   break;
  853.                 case 5:
  854.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][6][k]);
  855.                   break;
  856.                 case 6:
  857.                   curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][7][k]);
  858.                   break;
  859.                 default:
  860.                   break;
  861.               }
  862.             }
  863.             else
  864.               curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][(img->mb_mode)/2][k]);
  865.           // store (oversampled) mvd
  866.           for (l=0; l < step_v; l++)
  867.             for (m=0; m < step_h; m++)
  868.               currMB->mvd[0][j+l][i+m][k] = curr_mvd;
  869.           currSE->value1 = curr_mvd;
  870.           currSE->type = SE_MVD;
  871.           if (input->symbol_mode == UVLC)
  872.             currSE->mapping = mvd_linfo2;
  873.           else
  874.           {
  875.             img->subblock_x = i; // position used for context determination
  876.             img->subblock_y = j; // position used for context determination
  877.             currSE->value2 = 2*k; // identifies the component and the direction; only used for context determination
  878.             currSE->writing = writeBiMVD2Buffer_CABAC;
  879.           }
  880.           dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
  881.           dataPart->writeSyntaxElement( currSE, dataPart);
  882.           bitCount[BITS_INTER_MB]+=currSE->len;
  883.           no_bits += currSE->len;
  884. #if TRACE
  885.           snprintf(currSE->tracestring, TRACESTRING_SIZE, " MVD(%d) = %3d",k, curr_mvd);
  886. #endif
  887.           // proceed to next SE
  888.           currSE++;
  889.           currMB->currSEnr++;
  890.         }
  891.       }
  892.     }
  893.   }
  894.   // Write MVDBW(Backward, Bidirect)
  895.   if(img->imod==B_Backward || img->imod==B_Bidirect)
  896.   {
  897.     step_h=img->bw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize
  898.     step_v=img->bw_blc_size_v/BLOCK_SIZE;  // vertical stepsize
  899.     for (j=0; j < BLOCK_SIZE; j += step_v)
  900.     {
  901.       for (i=0;i < BLOCK_SIZE; i += step_h)
  902.       {
  903.         for (k=0; k < 2; k++)
  904.         {
  905.           if(img->mb_mode==2) // bw 16x16
  906.             curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][k]);
  907.           else
  908.             if(img->mb_mode==3) // bidirectional
  909.             {
  910.               switch(bw_blk_shape)
  911.               {
  912.                 case 0:
  913.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][k]);
  914.                   break;
  915.                 case 1:
  916.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][2][k]);
  917.                   break;
  918.                 case 2:
  919.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][3][k]);
  920.                   break;
  921.                 case 3:
  922.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][4][k]);
  923.                   break;
  924.                 case 4:
  925.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][5][k]);
  926.                   break;
  927.                 case 5:
  928.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][6][k]);
  929.                   break;
  930.                 case 6:
  931.                   curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][7][k]);
  932.                   break;
  933.                 default:
  934.                   break;
  935.               }
  936.             }
  937.             else // other bw
  938.               curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][(img->mb_mode-1)/2][k]);
  939.           // store (oversampled) mvd
  940.           for (l=0; l < step_v; l++)
  941.             for (m=0; m < step_h; m++)
  942.               currMB->mvd[1][j+l][i+m][k] = curr_mvd;
  943.           currSE->value1 = curr_mvd;
  944.           currSE->type = SE_MVD;
  945.           if (input->symbol_mode == UVLC)
  946.             currSE->mapping = mvd_linfo2;
  947.           else
  948.           {
  949.             img->subblock_x = i; // position used for context determination
  950.             img->subblock_y = j; // position used for context determination
  951.             currSE->value2 = 2*k+1; // identifies the component and the direction; only used for context determination
  952.             currSE->writing = writeBiMVD2Buffer_CABAC;
  953.           }
  954.           dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
  955.           dataPart->writeSyntaxElement( currSE, dataPart);
  956.           bitCount[BITS_INTER_MB]+=currSE->len;
  957.           no_bits += currSE->len;
  958. #if TRACE
  959.           snprintf(currSE->tracestring, TRACESTRING_SIZE, " MVD(%d) = %3d",k, curr_mvd);
  960. #endif
  961.           // proceed to next SE
  962.           currSE++;
  963.           currMB->currSEnr++;
  964.         }
  965.       }
  966.     }
  967.   }
  968.   return no_bits;
  969. }
  970. /*!
  971.  ************************************************************************
  972.  * brief
  973.  *    Passes back the code number given the blocksize width and
  974.  *    height (should be replaced by an appropriate table lookup)
  975.  ************************************************************************
  976.  */
  977. int BlkSize2CodeNumber(int blc_size_h, int blc_size_v)
  978. {
  979.   if(blc_size_h==16 && blc_size_v==16)  // 16x16 : code_number 0
  980.     return 0;
  981.   else
  982.     if(blc_size_h==16 && blc_size_v==8)  // 16x8 : code_number 1
  983.       return 1;
  984.     else
  985.       if(blc_size_h==8 && blc_size_v==16) // 8x16 : code_number 2
  986.         return 2;
  987.       else
  988.         if(blc_size_h==8 && blc_size_v==8) // 8x8 : code_number 3
  989.           return 3;
  990.         else
  991.           if(blc_size_h==8 && blc_size_v==4)  // 8x4 : code_number 4
  992.             return 4;
  993.           else
  994.             if(blc_size_h==4 && blc_size_v==8) // 4x8 : code_number 5
  995.               return 5;
  996.             else  // 4x4 : code_number 6
  997.               return 6;
  998. }
  999. /*!
  1000.  ************************************************************************
  1001.  * brief
  1002.  *    select intra, forward, backward, bidirectional, direct mode
  1003.  ************************************************************************
  1004.  */
  1005. int motion_search_Bframe(int tot_intra_sad)
  1006. {
  1007.   int fw_sad, bw_sad, bid_sad, dir_sad;
  1008.   int fw_predframe_no;
  1009.   fw_predframe_no=get_fwMV(&fw_sad, tot_intra_sad);
  1010.   get_bwMV(&bw_sad);
  1011.   get_bid(&bid_sad, fw_predframe_no);
  1012.   get_dir(&dir_sad);
  1013.   compare_sad(tot_intra_sad, fw_sad, bw_sad, bid_sad, dir_sad);
  1014.   return fw_predframe_no;
  1015. }
  1016. void get_bid(int *bid_sad, int fw_predframe_no)
  1017. {
  1018.   int mb_y,mb_x, block_y, block_x, pic_pix_y, pic_pix_x, pic_block_y, pic_block_x;
  1019.   int i, j, ii4, jj4, iii4, jjj4, i2, j2;
  1020.   int fw_pred, bw_pred, bid_pred[4][4];
  1021.   int code_num, step_h, step_v, mvd_x, mvd_y;
  1022.   // consider code number of fw_predframe_no
  1023.   *bid_sad = QP2QUANT[img->qp]*min(fw_predframe_no,1)*2;
  1024.   // consider bits of fw_blk_size
  1025.   if(img->fw_blc_size_h==16 && img->fw_blc_size_v==16)      // 16x16 : blocktype 1
  1026.     code_num=0;
  1027.   else if(img->fw_blc_size_h==16 && img->fw_blc_size_v==8)   // 16x8 : blocktype 2
  1028.     code_num=1;
  1029.   else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==16)  // 8x16 : blocktype 3
  1030.     code_num=2;
  1031.   else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==8)  // 8x8 : blocktype 4
  1032.     code_num=3;
  1033.   else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==4)   // 8x4 : blocktype 5
  1034.     code_num=4;
  1035.   else if(img->fw_blc_size_h==4 && img->fw_blc_size_v==8)   // 4x8 : blocktype 6
  1036.     code_num=5;
  1037.   else        // 4x4 : blocktype 7
  1038.     code_num=6;
  1039.   *bid_sad += QP2QUANT[img->qp]*img->blk_bituse[code_num];
  1040.   // consider bits of bw_blk_size
  1041.   if(img->bw_blc_size_h==16 && img->bw_blc_size_v==16)      // 16x16 : blocktype 1
  1042.     code_num=0;
  1043.   else if(img->bw_blc_size_h==16 && img->bw_blc_size_v==8)   // 16x8 : blocktype 2
  1044.     code_num=1;
  1045.   else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==16)  // 8x16 : blocktype 3
  1046.     code_num=2;
  1047.   else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==8)  // 8x8 : blocktype 4
  1048.     code_num=3;
  1049.   else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==4)   // 8x4 : blocktype 5
  1050.     code_num=4;
  1051.   else if(img->bw_blc_size_h==4 && img->bw_blc_size_v==8)   // 4x8 : blocktype 6
  1052.     code_num=5;
  1053.   else        // 4x4 : blocktype 7
  1054.     code_num=6;
  1055.   *bid_sad += QP2QUANT[img->qp]*img->blk_bituse[code_num];
  1056.   // consider bits of mvdfw
  1057.   step_h=img->fw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize
  1058.   step_v=img->fw_blc_size_v/BLOCK_SIZE;  // vertical stepsize
  1059.   for (j=0; j < BLOCK_SIZE; j += step_v)
  1060.   {
  1061.     for (i=0;i < BLOCK_SIZE; i += step_h)
  1062.     {
  1063.       if(img->fw_blc_size_h==16 && img->fw_blc_size_v==16)
  1064.       {      // 16x16 : blocktype 1
  1065.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][0];
  1066.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][1];
  1067.       }
  1068.       else if(img->fw_blc_size_h==16 && img->fw_blc_size_v==8)
  1069.       {  // 16x8 : blocktype 2
  1070.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][2][0];
  1071.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][2][1];
  1072.       }
  1073.       else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==16)
  1074.       { // 8x16 : blocktype 3
  1075.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][3][0];
  1076.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][3][1];
  1077.       }
  1078.       else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==8)
  1079.       { // 8x8 : blocktype 4
  1080.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][4][0];
  1081.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][4][1];
  1082.       }
  1083.       else if(img->fw_blc_size_h==8 && img->fw_blc_size_v==4)
  1084.       {  // 8x4 : blocktype 5
  1085.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][5][0];
  1086.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][5][1];
  1087.       }
  1088.       else if(img->fw_blc_size_h==4 && img->fw_blc_size_v==8)
  1089.       {  // 4x8 : blocktype 6
  1090.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][6][0];
  1091.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][6][1];
  1092.       }
  1093.       else
  1094.       {                                                      // 4x4 : blocktype 7
  1095.         mvd_x=tmp_fwMV[0][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][7][0];
  1096.         mvd_y=tmp_fwMV[1][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][7][1];
  1097.       }
  1098.       *bid_sad += QP2QUANT[img->qp]*(img->mv_bituse[absm(mvd_x)]+img->mv_bituse[absm(mvd_y)]);
  1099.     }
  1100.   }
  1101.   // consider bits of mvdbw
  1102.   step_h=img->bw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize
  1103.   step_v=img->bw_blc_size_v/BLOCK_SIZE;  // vertical stepsize
  1104.   for (j=0; j < BLOCK_SIZE; j += step_v)
  1105.   {
  1106.     for (i=0;i < BLOCK_SIZE; i += step_h)
  1107.     {
  1108.       if(img->bw_blc_size_h==16 && img->bw_blc_size_v==16)
  1109.       {      // 16x16 : blocktype 1
  1110.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][0];
  1111.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][1];
  1112.       }
  1113.       else if(img->bw_blc_size_h==16 && img->bw_blc_size_v==8)
  1114.       {  // 16x8 : blocktype 2
  1115.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][2][0];
  1116.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][2][1];
  1117.       }
  1118.       else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==16)
  1119.       { // 8x16 : blocktype 3
  1120.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][3][0];
  1121.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][3][1];
  1122.       }
  1123.       else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==8)
  1124.       { // 8x8 : blocktype 4
  1125.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][4][0];
  1126.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][4][1];
  1127.       }
  1128.       else if(img->bw_blc_size_h==8 && img->bw_blc_size_v==4)
  1129.       {  // 8x4 : blocktype 5
  1130.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][5][0];
  1131.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][5][1];
  1132.       }
  1133.       else if(img->bw_blc_size_h==4 && img->bw_blc_size_v==8)
  1134.       {  // 4x8 : blocktype 6
  1135.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][6][0];
  1136.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][6][1];
  1137.       }
  1138.       else
  1139.       {                                                      // 4x4 : blocktype 7
  1140.         mvd_x=tmp_bwMV[0][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][7][0];
  1141.         mvd_y=tmp_bwMV[1][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][7][1];
  1142.       }
  1143.       *bid_sad += QP2QUANT[img->qp]*(img->mv_bituse[absm(mvd_x)]+img->mv_bituse[absm(mvd_y)]);
  1144.     }
  1145.   }
  1146.   for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  1147.   {
  1148.     for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  1149.     {
  1150.       for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  1151.       {
  1152.         pic_pix_y=img->pix_y+block_y;
  1153.         pic_block_y=pic_pix_y/BLOCK_SIZE;
  1154.         for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  1155.         {
  1156.           pic_pix_x=img->pix_x+block_x;
  1157.           pic_block_x=pic_pix_x/BLOCK_SIZE;
  1158.           if(input->mv_res)
  1159.           {
  1160.             ii4=(img->pix_x+block_x)*8+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  1161.             jj4=(img->pix_y+block_y)*8+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  1162.             iii4=(img->pix_x+block_x)*8+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  1163.             jjj4=(img->pix_y+block_y)*8+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  1164.             for (j=0;j<4;j++)
  1165.             {
  1166.               j2=j*8;
  1167.               for (i=0;i<4;i++)
  1168.               {
  1169.                 i2=i*8;
  1170.                 fw_pred = UMVPelY_18 (mref[img->fw_multframe_no],jj4+j2,ii4+i2);
  1171.                 bw_pred = UMVPelY_18 (mref_P, jjj4+j2, iii4+i2);
  1172.                 bid_pred[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);
  1173.               }
  1174.             }
  1175.           }
  1176.           else
  1177.           {
  1178.             ii4=(img->pix_x+block_x)*4+tmp_fwMV[0][pic_block_y][pic_block_x+4];
  1179.             jj4=(img->pix_y+block_y)*4+tmp_fwMV[1][pic_block_y][pic_block_x+4];
  1180.             iii4=(img->pix_x+block_x)*4+tmp_bwMV[0][pic_block_y][pic_block_x+4];
  1181.             jjj4=(img->pix_y+block_y)*4+tmp_bwMV[1][pic_block_y][pic_block_x+4];
  1182.             for (j=0;j<4;j++)
  1183.             {
  1184.               j2=j*4;
  1185.               for (i=0;i<4;i++)
  1186.               {
  1187.                 i2=i*4;
  1188.                 fw_pred=UMVPelY_14 (mref[img->fw_multframe_no], jj4+j2, ii4+i2);
  1189.                 bw_pred=UMVPelY_14 (mref_P, jjj4+j2, iii4+i2);
  1190.                 bid_pred[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);
  1191.               }
  1192.             }
  1193.           }
  1194.           for (j=0; j < BLOCK_SIZE; j++)
  1195.           {
  1196.             for (i=0; i < BLOCK_SIZE; i++)
  1197.             {
  1198.               img->m7[i][j]=imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-bid_pred[i][j];
  1199.             }
  1200.           }
  1201.           *bid_sad += find_sad(input->hadamard, img->m7);
  1202.         }
  1203.       }
  1204.     }
  1205.   }
  1206. }
  1207. void get_dir(int *dir_sad)
  1208. {
  1209.   int mb_y,mb_x, block_y, block_x, pic_pix_y, pic_pix_x, pic_block_y, pic_block_x;
  1210.   int i, j, ii4, jj4, iii4, jjj4, i2, j2, hv;
  1211.   int ref_inx, df_pred, db_pred, dir_pred[4][4];
  1212.   int refP_tr, TRb, TRp;
  1213.   // initialize with bias value
  1214.   *dir_sad=-QP2QUANT[img->qp] * 16;
  1215.   // create dfMV, dbMV
  1216.   for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  1217.   {
  1218.     for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  1219.     {
  1220.       for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  1221.       {
  1222.         pic_pix_y=img->pix_y+block_y;
  1223.         pic_block_y=pic_pix_y/BLOCK_SIZE;
  1224.         for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  1225.         {
  1226.           pic_pix_x=img->pix_x+block_x;
  1227.           pic_block_x=pic_pix_x/BLOCK_SIZE;
  1228.           // next P is intra mode
  1229.           if(refFrArr[pic_block_y][pic_block_x]==-1)
  1230.           {
  1231.             for(hv=0; hv<2; hv++)
  1232.             {
  1233.               dfMV[hv][pic_block_y][pic_block_x+4]=dbMV[hv][pic_block_y][pic_block_x+4]=0;
  1234.             }
  1235.           }
  1236.           // next P is skip or inter mode
  1237.           else
  1238.           {
  1239. #ifdef _ADAPT_LAST_GROUP_
  1240.             refP_tr=last_P_no [refFrArr[pic_block_y][pic_block_x]];
  1241. #else
  1242.             refP_tr=nextP_tr - ((refFrArr[pic_block_y][pic_block_x]+1)*img->p_interval);
  1243. #endif
  1244.             TRb=img->tr-refP_tr;
  1245.             TRp=nextP_tr-refP_tr;
  1246.             for(hv=0; hv<2; hv++)
  1247.             {
  1248.               dfMV[hv][pic_block_y][pic_block_x+4]=TRb*tmp_mv[hv][pic_block_y][pic_block_x+4]/TRp;
  1249.               dbMV[hv][pic_block_y][pic_block_x+4]=(TRb-TRp)*tmp_mv[hv][pic_block_y][pic_block_x+4]/TRp;
  1250.             }
  1251.           }
  1252.         }
  1253.       }
  1254.     }
  1255.   }
  1256.   // prediction
  1257.   for (mb_y=0; mb_y < MB_BLOCK_SIZE; mb_y += BLOCK_SIZE*2)
  1258.   {
  1259.     for (mb_x=0; mb_x < MB_BLOCK_SIZE; mb_x += BLOCK_SIZE*2)
  1260.     {
  1261.       for (block_y=mb_y; block_y < mb_y+BLOCK_SIZE*2; block_y += BLOCK_SIZE)
  1262.       {
  1263.         pic_pix_y=img->pix_y+block_y;
  1264.         pic_block_y=pic_pix_y/BLOCK_SIZE;
  1265.         for (block_x=mb_x; block_x < mb_x+BLOCK_SIZE*2; block_x += BLOCK_SIZE)
  1266.         {
  1267.           pic_pix_x=img->pix_x+block_x;
  1268.           pic_block_x=pic_pix_x/BLOCK_SIZE;
  1269.           if(input->mv_res)
  1270.           {
  1271.             ii4=(img->pix_x+block_x)*8+dfMV[0][pic_block_y][pic_block_x+4];
  1272.             jj4=(img->pix_y+block_y)*8+dfMV[1][pic_block_y][pic_block_x+4];
  1273.             iii4=(img->pix_x+block_x)*8+dbMV[0][pic_block_y][pic_block_x+4];
  1274.             jjj4=(img->pix_y+block_y)*8+dbMV[1][pic_block_y][pic_block_x+4];
  1275.             {
  1276.               // next P is intra mode
  1277.               if(refFrArr[pic_block_y][pic_block_x]==-1)
  1278.                 ref_inx=(img->number-1)%img->buf_cycle;
  1279.               // next P is skip or inter mode
  1280.               else
  1281.                 ref_inx=(img->number-refFrArr[pic_block_y][pic_block_x]-1)%img->buf_cycle;
  1282.               for (j=0;j<4;j++)
  1283.               {
  1284.                 j2=j*8;
  1285.                 for (i=0;i<4;i++)
  1286.                 {
  1287.                   i2=i*8;
  1288.                   df_pred = UMVPelY_18 (mref[ref_inx], jj4 +j2,  ii4+i2);
  1289.                   db_pred = UMVPelY_18 (mref_P,        jjj4+j2, iii4+i2);
  1290.                   dir_pred[i][j]=(int)((df_pred+db_pred)/2.0+0.5);
  1291.                 }
  1292.               }
  1293.               for (j=0; j < BLOCK_SIZE; j++)
  1294.               {
  1295.                 for (i=0; i < BLOCK_SIZE; i++)
  1296.                 {
  1297.                   img->m7[i][j]=imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-dir_pred[i][j];
  1298.                 }
  1299.               }
  1300.               *dir_sad += find_sad(input->hadamard, img->m7);
  1301.             }
  1302.           }
  1303.           else
  1304.           {
  1305.             ii4=(img->pix_x+block_x)*4+dfMV[0][pic_block_y][pic_block_x+4];
  1306.             jj4=(img->pix_y+block_y)*4+dfMV[1][pic_block_y][pic_block_x+4];
  1307.             iii4=(img->pix_x+block_x)*4+dbMV[0][pic_block_y][pic_block_x+4];
  1308.             jjj4=(img->pix_y+block_y)*4+dbMV[1][pic_block_y][pic_block_x+4];
  1309.             {
  1310.               // next P is intra mode
  1311.               if(refFrArr[pic_block_y][pic_block_x]==-1)
  1312.                 ref_inx=(img->number-1)%img->buf_cycle;
  1313.               // next P is skip or inter mode
  1314.               else
  1315.                 ref_inx=(img->number-refFrArr[pic_block_y][pic_block_x]-1)%img->buf_cycle;
  1316.               for (j=0;j<4;j++)
  1317.               {
  1318.                 j2=j*4;
  1319.                 for (i=0;i<4;i++)
  1320.                 {
  1321.                   i2=i*4;
  1322.                   df_pred=UMVPelY_14 (mref[ref_inx], jj4+j2, ii4+i2);
  1323.                   db_pred=UMVPelY_14 (mref_P, jjj4+j2, iii4+i2);
  1324.                   dir_pred[i][j]=(int)((df_pred+db_pred)/2.0+0.5);
  1325.                 }
  1326.               }
  1327.               for (j=0; j < BLOCK_SIZE; j++)
  1328.               {
  1329.                 for (i=0; i < BLOCK_SIZE; i++)
  1330.                 {
  1331.                   img->m7[i][j]=imgY_org[img->pix_y+block_y+j][img->pix_x+block_x+i]-dir_pred[i][j];
  1332.                 }
  1333.               }
  1334.               *dir_sad += find_sad(input->hadamard, img->m7);
  1335.             } // else
  1336.           } // else
  1337.         } // block_x
  1338.       } // block_y
  1339.     } // mb_x
  1340.   } // mb_y
  1341. }
  1342. void compare_sad(int tot_intra_sad, int fw_sad, int bw_sad, int bid_sad, int dir_sad)
  1343. {
  1344.   int hv, i, j;
  1345.   int InterIntraSave ;
  1346.   InterIntraSave = img->mb_data[img->current_mb_nr].intraOrInter ;
  1347.   img->mb_data[img->current_mb_nr].intraOrInter = INTER_MB ;
  1348.   // LG : dfMV, dbMV reset
  1349.   if( (dir_sad<=tot_intra_sad) && (dir_sad<=fw_sad) && (dir_sad<=bw_sad) && (dir_sad<=bid_sad) )
  1350.   {
  1351.     img->imod = B_Direct;
  1352.     img->mb_mode = 0;
  1353.     for(hv=0; hv<2; hv++)
  1354.       for(i=0; i<4; i++)
  1355.         for(j=0; j<4; j++)
  1356.           tmp_fwMV[hv][img->block_y+j][img->block_x+i+4]=
  1357.           tmp_bwMV[hv][img->block_y+j][img->block_x+i+4]=0;
  1358.   }
  1359.   else if( (bw_sad<=tot_intra_sad) && (bw_sad<=fw_sad) && (bw_sad<=bid_sad) && (bw_sad<=dir_sad) )
  1360.   {
  1361.     img->imod = B_Backward;
  1362.     img->mb_mode = img->bw_mb_mode;
  1363.     for(hv=0; hv<2; hv++)
  1364.       for(i=0; i<4; i++)
  1365.         for(j=0; j<4; j++)
  1366.           tmp_fwMV[hv][img->block_y+j][img->block_x+i+4]=
  1367.           dfMV[hv][img->block_y+j][img->block_x+i+4]=
  1368.           dbMV[hv][img->block_y+j][img->block_x+i+4]=0;
  1369.   }
  1370.   else if( (fw_sad<=tot_intra_sad) && (fw_sad<=bw_sad) && (fw_sad<=bid_sad) && (fw_sad<=dir_sad) )
  1371.   {
  1372.     img->imod = B_Forward;
  1373.     img->mb_mode = img->fw_mb_mode;
  1374.     for(hv=0; hv<2; hv++)
  1375.       for(i=0; i<4; i++)
  1376.         for(j=0; j<4; j++)
  1377.           tmp_bwMV[hv][img->block_y+j][img->block_x+i+4]=
  1378.           dfMV[hv][img->block_y+j][img->block_x+i+4]=
  1379.           dbMV[hv][img->block_y+j][img->block_x+i+4]=0;
  1380.   }
  1381.   else if( (bid_sad<=tot_intra_sad) && (bid_sad<=fw_sad) && (bid_sad<=bw_sad) && (bid_sad<=dir_sad) )
  1382.   {
  1383.     img->imod = B_Bidirect;
  1384.     img->mb_mode = 3;
  1385.     for(hv=0; hv<2; hv++)
  1386.       for(i=0; i<4; i++)
  1387.         for(j=0; j<4; j++)
  1388.           dfMV[hv][img->block_y+j][img->block_x+i+4]=
  1389.           dbMV[hv][img->block_y+j][img->block_x+i+4]=0;
  1390.   }
  1391.   else if( (tot_intra_sad<=dir_sad) && (tot_intra_sad<=bw_sad) && (tot_intra_sad<=fw_sad) && (tot_intra_sad<=bid_sad) )
  1392.   {
  1393.     img->mb_mode=img->imod+8*img->type; // img->type=2
  1394.     img->mb_data[img->current_mb_nr].intraOrInter  =  InterIntraSave;
  1395.     for(hv=0; hv<2; hv++)
  1396.       for(i=0; i<4; i++)
  1397.         for(j=0; j<4; j++)
  1398.           tmp_fwMV[hv][img->block_y+j][img->block_x+i+4]=
  1399.           tmp_bwMV[hv][img->block_y+j][img->block_x+i+4]=
  1400.           dfMV[hv][img->block_y+j][img->block_x+i+4]=
  1401.           dbMV[hv][img->block_y+j][img->block_x+i+4]=0;
  1402.   }
  1403. }