cavlc.c
上传用户:sunbaby
上传日期:2013-05-31
资源大小:242k
文件大小:34k
源码类别:

mpeg/mp3

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  *
  3.  *  T264 AVC CODEC
  4.  *
  5.  *  Copyright(C) 2004-2005 llcc <lcgate1@yahoo.com.cn>
  6.  *               2004-2005 visionany <visionany@yahoo.com.cn>
  7.  *
  8.  *  This program is free software ; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation ; either version 2 of the License, or
  11.  *  (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program ; if not, write to the Free Software
  20.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  21.  *
  22.  ****************************************************************************/
  23.  /*****************************************************************************
  24.  * cavlc.c: from x 264
  25.  *****************************************************************************/
  26. //#include <stdlib.h>
  27. //#include <string.h>
  28. //#include <stdint.h>
  29. #include <stdio.h>
  30. #include "T264.h"
  31. #include "vlc.h"
  32. #include "bitstream.h"
  33. #include "inter.h"
  34. #include <assert.h>
  35. static const uint8_t intra4x4_cbp_to_golomb[48]=
  36. {
  37.   3, 29, 30, 17, 31, 18, 37,  8, 32, 38, 19,  9, 20, 10, 11,  2,
  38.  16, 33, 34, 21, 35, 22, 39,  4, 36, 40, 23,  5, 24,  6,  7,  1,
  39.  41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15,  0
  40. };
  41. static const uint8_t inter_cbp_to_golomb[48]=
  42. {
  43.   0,  2,  3,  7,  4,  8, 17, 13,  5, 18,  9, 14, 10, 15, 16, 11,
  44.   1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
  45.   6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
  46. };
  47. /*
  48. static const uint8_t block_idx_x[16] =
  49. {
  50.     0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  51. };
  52. static const uint8_t block_idx_y[16] =
  53. {
  54.     0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  55. };
  56. static const uint8_t block_idx_xy[4][4] =
  57. {
  58.     { 0, 2, 8,  10},
  59.     { 1, 3, 9,  11},
  60.     { 4, 6, 12, 14},
  61.     { 5, 7, 13, 15}
  62. };
  63. */
  64. #define IS_DIRECT(x) (x == B_DIRECT_16x16 || x == B_DIRECT_8x8)
  65. #define BLOCK_INDEX_CHROMA_DC   (-1)
  66. #define BLOCK_INDEX_LUMA_DC     (-2)
  67. static __inline void eg_write_vlc(bs_t* bs, vlc_t v)
  68. {
  69. eg_write_direct(bs, v.i_bits & ((uint32_t)~0 >> (uint32_t)(32 - v.i_size)), v.i_size);
  70. }
  71. int8_t
  72. T264_mb_predict_intra4x4_mode(T264_t *t, int32_t idx)
  73. {
  74. int32_t x, y;
  75. int8_t nA, nB, pred_blk;
  76.     x = luma_inverse_x[idx];
  77.     y = luma_inverse_y[idx];
  78.     nA = t->mb.i4x4_pred_mode_ref[IPM_LUMA + x + y * 8 - 1];
  79.     nB = t->mb.i4x4_pred_mode_ref[IPM_LUMA + x + y * 8 - 8];
  80.     pred_blk  = T264_MIN(nA, nB);
  81.     if( pred_blk < 0 )
  82.         return Intra_4x4_DC;
  83.     return pred_blk;
  84. }
  85. uint8_t 
  86. T264_mb_predict_non_zero_code(T264_t* t, int idx)
  87. {
  88.     int32_t x, y;
  89.     int32_t nA, nB;
  90. int16_t pred_blk;
  91. if(idx >= 0 && idx < 16)
  92. {
  93.         x = luma_inverse_x[idx];
  94.         y = luma_inverse_y[idx];
  95.         nA = t->mb.nnz_ref[NNZ_LUMA + x + y * 8 - 1];
  96.         nB = t->mb.nnz_ref[NNZ_LUMA + x + y * 8 - 8];
  97. }
  98. else if(idx < 20)
  99. {
  100. y = (idx - 16) / 2;
  101. x = (idx - 16) % 2;
  102.         nA = t->mb.nnz_ref[NNZ_CHROMA0 + x + y * 8 - 1];
  103.         nB = t->mb.nnz_ref[NNZ_CHROMA0 + x + y * 8 - 8];
  104. }
  105. else if(idx < 24)
  106. {
  107.         y = (idx - 20) / 2;
  108.         x = (idx - 20) % 2;
  109.         nA = t->mb.nnz_ref[NNZ_CHROMA1 + x + y * 8 - 1];
  110.         nB = t->mb.nnz_ref[NNZ_CHROMA1 + x + y * 8 - 8];
  111. }
  112.     pred_blk = nA + nB;
  113.     if( pred_blk < 0x80 )
  114.     {
  115.         pred_blk = ( pred_blk + 1 ) >> 1;
  116.     }
  117.     return pred_blk & 0x7f;
  118. }
  119. /****************************************************************************
  120.  * block_residual_write_cavlc
  121.  ****************************************************************************/
  122. static void block_residual_write_cavlc( T264_t *h, int32_t i_idx, int16_t *l, int32_t i_count )
  123. {
  124.     int level[16], run[16];
  125.     int i_total, i_trailing;
  126.     int i_total_zero;
  127.     int i_last;
  128.     unsigned int i_sign;
  129.     int i;
  130.     int i_zero_left;
  131.     int i_suffix_length;
  132.     /* first find i_last */
  133.     i_last = i_count - 1;
  134.     while( i_last >= 0 && l[i_last] == 0 )
  135.     {
  136.         i_last--;
  137.     }
  138.     i_sign = 0;
  139.     i_total = 0;
  140.     i_trailing = 0;
  141.     i_total_zero = 0;
  142.     if( i_last >= 0 )
  143.     {
  144.         int b_trailing = 1;
  145.         int idx = 0;
  146.         /* level and run and total */
  147.         while( i_last >= 0 )
  148.         {
  149.             level[idx] = l[i_last--];
  150.             run[idx] = 0;
  151.             while( i_last >= 0 && l[i_last] == 0 )
  152.             {
  153.                 run[idx]++;
  154.                 i_last--;
  155.             }
  156.             i_total++;
  157.             i_total_zero += run[idx];
  158.             if( b_trailing && ABS( level[idx] ) == 1 && i_trailing < 3 )
  159.             {
  160.                 i_sign <<= 1;
  161.                 if( level[idx] < 0 )
  162.                 {
  163.                     i_sign |= 0x01;
  164.                 }
  165.                 i_trailing++;
  166.             }
  167.             else
  168.             {
  169.                 b_trailing = 0;
  170.             }
  171.             idx++;
  172.         }
  173.     }
  174.     /* total/trailing represented by Coeff_token (5 tables)*/
  175.     if( i_idx == BLOCK_INDEX_CHROMA_DC )
  176.     {
  177. eg_write_vlc(h->bs, x264_coeff_token[4][i_total*4+i_trailing]);
  178.     }
  179.     else
  180.     {
  181.         /* T264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
  182.         static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
  183.         int nC = 0;
  184.         if( i_idx == BLOCK_INDEX_LUMA_DC )
  185.         {
  186. // predict nC = (nA + nB) / 2;
  187.             nC = T264_mb_predict_non_zero_code(h, 0);
  188.         }
  189.         else
  190.         {
  191. // predict nC = (nA + nB) / 2;
  192.             nC = T264_mb_predict_non_zero_code(h, i_idx);
  193.         }
  194. eg_write_vlc(h->bs, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing]);
  195.     }
  196.     if( i_total <= 0 )
  197.     {
  198.         return;
  199.     }
  200. //encoding trailing '1's (max = 3)
  201.     i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
  202.     if( i_trailing > 0 )
  203.     {
  204. eg_write_direct(h->bs, i_sign & ((uint32_t)~0 >> (uint32_t)(32 - i_trailing)), i_trailing);
  205.     }
  206. //encoding remain levels of non-zero coefficients (prefix + suffix)
  207.     for( i = i_trailing; i < i_total; i++ )
  208.     {
  209.         int i_level_code;
  210.         /* calculate level code */
  211.         if( level[i] < 0 )
  212.         {
  213.             i_level_code = -2*level[i] - 1;
  214.         }
  215.         else /* if( level[i] > 0 ) */
  216.         {
  217.             i_level_code = 2 * level[i] - 2;
  218.         }
  219.         if( i == i_trailing && i_trailing < 3 )
  220.         {
  221.             i_level_code -=2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
  222.         }
  223.         if( ( i_level_code >> i_suffix_length ) < 14 )
  224.         {
  225.             eg_write_vlc(h->bs, x264_level_prefix[i_level_code >> i_suffix_length]);
  226.             if( i_suffix_length > 0 )
  227.             {
  228. eg_write_direct(h->bs, (uint32_t)i_level_code & ((uint32_t)~0 >> (uint32_t)(32 - i_suffix_length)), i_suffix_length);
  229.             }
  230.         }
  231.         else if( i_suffix_length == 0 && i_level_code < 30 )
  232.         {
  233.             eg_write_vlc(h->bs, x264_level_prefix[14]);
  234.             eg_write_direct(h->bs, (i_level_code - 14) & ((uint32_t)~0 >> (uint32_t)(32 - 4)), 4);
  235.         }
  236.         else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
  237.         {
  238.             eg_write_vlc(h->bs, x264_level_prefix[14]);
  239.             eg_write_direct(h->bs, i_level_code & ((uint32_t)~0 >> (uint32_t)(32 - i_suffix_length)), i_suffix_length);
  240.         }
  241.         else
  242.         {
  243.             eg_write_vlc(h->bs, x264_level_prefix[15]);
  244.             i_level_code -= 15 << i_suffix_length;
  245.             if( i_suffix_length == 0 )
  246.             {
  247.                 i_level_code -= 15;
  248.             }
  249.             if( i_level_code >= ( 1 << 12 ) || i_level_code < 0 )
  250.             {
  251.                 fprintf( stderr, "OVERFLOW levelcode=%dn", i_level_code );
  252.             }
  253. eg_write_direct(h->bs, i_level_code & ((uint32_t)~0 >> (uint32_t)(32 - 12)), 12);
  254.         }
  255.         if( i_suffix_length == 0 )
  256.         {
  257.             i_suffix_length++;
  258.         }
  259.         if( ABS( level[i] ) > ( 3 << ( i_suffix_length - 1 ) ) && i_suffix_length < 6 )
  260.         {
  261.             i_suffix_length++;
  262.         }
  263.     }
  264. //encode total zeros [i_total-1][i_total_zero]
  265.     if( i_total < i_count )
  266.     {
  267.         if( i_idx == BLOCK_INDEX_CHROMA_DC )
  268.         {
  269.             eg_write_vlc(h->bs, x264_total_zeros_dc[i_total-1][i_total_zero]);
  270.         }
  271.         else
  272.         {
  273.             eg_write_vlc(h->bs, x264_total_zeros[i_total-1][i_total_zero]);
  274.         }
  275.     }
  276. //encode each run of zeros
  277.     for( i = 0, i_zero_left = i_total_zero; i < i_total - 1; i++ )
  278.     {
  279.         int i_zl;
  280.         if( i_zero_left <= 0 )
  281.         {
  282.             break;
  283.         }
  284.         i_zl = T264_MIN( i_zero_left - 1, 6 );
  285. eg_write_vlc(h->bs, x264_run_before[i_zl][run[i]]);
  286.         i_zero_left -= run[i];
  287.     }
  288. }
  289. void 
  290. T264_macroblock_write_cavlc(T264_t *t)
  291. {
  292.     int32_t mb_mode = t->mb.mb_mode;
  293.     int32_t i;
  294.     int32_t offset;
  295.     if (t->slice_type == SLICE_I)
  296.     {
  297.         offset = 0;
  298.     }
  299.     else if (t->slice_type == SLICE_P)
  300.     {
  301.         offset = 5;
  302.     }
  303.     else if (t->slice_type == SLICE_B)
  304.     {
  305.         offset = 23;
  306.     }
  307.     if(t->slice_type != SLICE_I)
  308.     {
  309.         eg_write_ue(t->bs, t->skip);  /* skip run */
  310.         t->skip = 0;
  311.     }
  312.     if (mb_mode == I_4x4)
  313.     {
  314.         // mb_type
  315.         eg_write_ue(t->bs, offset + 0);
  316.         /* Prediction: Luma */
  317.         for (i = 0 ; i < 16; i ++) /* i : Inverse raster scan */
  318.         {
  319.             int8_t pred = T264_mb_predict_intra4x4_mode(t, i);
  320.             int8_t mode = t->mb.mode_i4x4[i];
  321.             if( pred == mode)
  322.             {
  323.                 eg_write_direct1(t->bs, 1); /* b_prev_intra4x4_pred_mode */
  324.             }
  325.             else
  326.             {
  327.                 eg_write_direct1(t->bs, 0); /* b_prev_intra4x4_pred_mode */
  328.                 if( mode < pred )
  329.                 {
  330.                     eg_write_direct(t->bs, mode, 3);  /* rem_intra4x4_pred_mode */
  331.                 }
  332.                 else
  333.                 {
  334.                     eg_write_direct(t->bs, mode - 1, 3);  /* rem_intra4x4_pred_mode */
  335.                 }
  336.             }
  337.         }
  338.         //intra_chroma_pred_mode
  339.         eg_write_ue(t->bs, t->mb.mb_mode_uv);
  340.         // coded_block_pattern
  341.         eg_write_ue(t->bs, intra4x4_cbp_to_golomb[( t->mb.cbp_c << 4 ) | t->mb.cbp_y]);
  342.         //delta_qp
  343.         if (t->mb.cbp_y > 0 || t->mb.cbp_c > 0)
  344.         {
  345.             eg_write_se(t->bs, t->mb.mb_qp_delta); /* 0 = no change on qp */
  346.             for (i = 0; i < 16 ; i ++)
  347.             {
  348.                 if(t->mb.cbp_y & (1 << ( i / 4 )))
  349.                 {
  350.                     block_residual_write_cavlc(t, i, t->mb.dct_y_z[i], 16);
  351.                 }
  352.             }
  353.         }
  354.     }
  355.     else if (mb_mode == I_16x16)
  356.     {
  357.         // mb_type
  358.         eg_write_ue(t->bs, offset + 1 + t->mb.mode_i16x16 + 
  359.             t->mb.cbp_c * 4 + (t->mb.cbp_y == 0 ? 0 : 12));
  360.         // intra chroma pred mode
  361.         eg_write_ue(t->bs, t->mb.mb_mode_uv);
  362.         // delta qp
  363.         eg_write_se(t->bs, t->mb.mb_qp_delta);
  364.         // dc luma
  365.         block_residual_write_cavlc(t, BLOCK_INDEX_LUMA_DC, t->mb.dc4x4_z, 16);
  366.         if (t->mb.cbp_y != 0)
  367.         {
  368.             for(i = 0 ; i < 16 ; i ++)
  369.             {
  370.                 if (t->mb.cbp_y & (1 << (i / 4)))
  371.                 {
  372.                     block_residual_write_cavlc(t, i, &(t->mb.dct_y_z[i][1]), 15);
  373.                 }
  374.             }
  375.         }
  376.     }
  377.     else
  378.     {
  379.         T264_vector_t vec;
  380.         if (t->slice_type == SLICE_P)
  381.         {
  382.             switch (t->mb.mb_part) 
  383.             {
  384.             case MB_16x16:
  385.                 eg_write_ue(t->bs, MB_16x16);
  386.                 if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  387.                 {
  388.                     eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  389.                 }
  390.                 vec = t->mb.vec[0][0];
  391.                 T264_predict_mv(t, 0, 0, 4, &vec);
  392.                 eg_write_se(t->bs, t->mb.vec[0][0].x - vec.x);
  393.                 eg_write_se(t->bs, t->mb.vec[0][0].y - vec.y);
  394.              break;
  395.             case MB_16x8:
  396.                 eg_write_ue(t->bs, MB_16x8);
  397.                 if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  398.                 {
  399.                     eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  400.                     eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][8].refno);
  401.                 }
  402.                 vec = t->mb.vec[0][0];
  403.                 T264_predict_mv(t, 0, 0, 4, &vec);
  404.                 eg_write_se(t->bs, t->mb.vec[0][0].x - vec.x);
  405.                 eg_write_se(t->bs, t->mb.vec[0][0].y - vec.y);
  406.                 vec = t->mb.vec[0][8];
  407.                 T264_predict_mv(t, 0, 8, 4, &vec);
  408.                 eg_write_se(t->bs, t->mb.vec[0][8].x - vec.x);
  409.                 eg_write_se(t->bs, t->mb.vec[0][8].y - vec.y);
  410.              break;
  411.             case MB_8x16:
  412.                 eg_write_ue(t->bs, MB_8x16);
  413.                 if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  414.                 {
  415.                     eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  416.                     eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][luma_index[4]].refno);
  417.                 }
  418.                 vec = t->mb.vec[0][0];
  419.                 T264_predict_mv(t, 0, 0, 2, &vec);
  420.                 eg_write_se(t->bs, t->mb.vec[0][0].x - vec.x);
  421.                 eg_write_se(t->bs, t->mb.vec[0][0].y - vec.y);
  422.                 vec = t->mb.vec[0][luma_index[4]];
  423.                 T264_predict_mv(t, 0, luma_index[4], 2, &vec);
  424.                 eg_write_se(t->bs, t->mb.vec[0][luma_index[4]].x - vec.x);
  425.                 eg_write_se(t->bs, t->mb.vec[0][luma_index[4]].y - vec.y);
  426.                 break;
  427.             case MB_8x8:
  428.             case MB_8x8ref0:
  429.                 if (t->mb.vec[0][luma_index[0]].refno == 0 &&
  430.                     t->mb.vec[0][luma_index[4]].refno == 0 &&
  431.                     t->mb.vec[0][luma_index[8]].refno == 0 &&
  432.                     t->mb.vec[0][luma_index[12]].refno == 0)
  433.                 {
  434.                     eg_write_ue(t->bs, MB_8x8ref0);
  435.                     for (i = 0 ; i < 4 ; i ++)
  436.                     {
  437.                         switch (t->mb.submb_part[luma_index[4 * i]]) 
  438.                         {
  439.                         case MB_8x8:
  440.                             eg_write_ue(t->bs, 0);
  441.                          break;
  442.                         case MB_8x4:
  443.                             eg_write_ue(t->bs, 1);
  444.                             break;
  445.                         case MB_4x8:
  446.                             eg_write_ue(t->bs, 2);
  447.                             break;
  448.                         case MB_4x4:
  449.                             eg_write_ue(t->bs, 3);
  450.                             break;
  451.                         default:
  452.                             break;
  453.                         }
  454.                     }
  455.                 }
  456.                 else
  457.                 {
  458.                     eg_write_ue(t->bs, MB_8x8);
  459.                     for (i = 0 ; i < 4 ; i ++)
  460.                     {
  461.                         switch (t->mb.submb_part[luma_index[4 * i]]) 
  462.                         {
  463.                         case MB_8x8:
  464.                             eg_write_ue(t->bs, 0);
  465.                             break;
  466.                         case MB_8x4:
  467.                             eg_write_ue(t->bs, 1);
  468.                             break;
  469.                         case MB_4x8:
  470.                             eg_write_ue(t->bs, 2);
  471.                             break;
  472.                         case MB_4x4:
  473.                             eg_write_ue(t->bs, 3);
  474.                             break;
  475.                         default:
  476.                             break;
  477.                         }
  478.                     }
  479.                     
  480.                     if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  481.                     {
  482.                         eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  483.                         eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][luma_index[4]].refno);
  484.                         eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][luma_index[8]].refno);
  485.                         eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][luma_index[12]].refno);
  486.                     }
  487.                 }
  488.                 for(i = 0 ; i < 4 ; i ++)
  489.                 {
  490.                     switch(t->mb.submb_part[luma_index[4 * i]]) 
  491.                     {
  492.                     case MB_8x8:
  493.                         vec = t->mb.vec[0][luma_index[4 * i]];
  494.                         T264_predict_mv(t, 0, luma_index[4 * i], 2, &vec);
  495.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].x - vec.x);
  496.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].y - vec.y);
  497.                      break;
  498.                     case MB_8x4:
  499.                         vec = t->mb.vec[0][luma_index[4 * i]];
  500.                         T264_predict_mv(t, 0, luma_index[4 * i], 2, &vec);
  501.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].x - vec.x);
  502.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].y - vec.y);
  503.                         vec = t->mb.vec[0][luma_index[4 * i + 2]];
  504.                         T264_predict_mv(t, 0, luma_index[4 * i + 2], 2, &vec);
  505.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 2]].x - vec.x);
  506.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 2]].y - vec.y);
  507.                      break;
  508.                     case MB_4x8:
  509.                         vec = t->mb.vec[0][luma_index[4 * i]];
  510.                         T264_predict_mv(t, 0, luma_index[4 * i], 1, &vec);
  511.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].x - vec.x);
  512.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].y - vec.y);
  513.                         vec = t->mb.vec[0][luma_index[4 * i + 1]];
  514.                         T264_predict_mv(t, 0, luma_index[4 * i + 1], 1, &vec);
  515.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 1]].x - vec.x);
  516.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 1]].y - vec.y);
  517.                         break;
  518.                     case MB_4x4:
  519.                         vec = t->mb.vec[0][luma_index[4 * i]];
  520.                         T264_predict_mv(t, 0, luma_index[4 * i], 1, &vec);
  521.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].x - vec.x);
  522.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].y - vec.y);
  523.                         vec = t->mb.vec[0][luma_index[4 * i + 1]];
  524.                         T264_predict_mv(t, 0, luma_index[4 * i + 1], 1, &vec);
  525.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 1]].x - vec.x);
  526.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 1]].y - vec.y);
  527.                         vec = t->mb.vec[0][luma_index[4 * i + 2]];
  528.                         T264_predict_mv(t, 0, luma_index[4 * i + 2], 1, &vec);
  529.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 2]].x - vec.x);
  530.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 2]].y - vec.y);
  531.                         vec = t->mb.vec[0][luma_index[4 * i + 3]];
  532.                         T264_predict_mv(t, 0, luma_index[4 * i + 3], 1, &vec);
  533.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 3]].x - vec.x);
  534.                         eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i + 3]].y - vec.y);
  535.                         break;
  536.                     }
  537.                 }
  538.                 break;
  539.             default:
  540.                 break;
  541.             }
  542.         }
  543.         else
  544.         {
  545.             switch (t->mb.mb_part)
  546.             {
  547.             case MB_16x16:
  548.                  if (t->mb.is_copy != 1)
  549.                  {
  550.                     switch (t->mb.mb_part2[0])
  551.                     {
  552.                     case B_L0_16x16:
  553.                         eg_write_ue(t->bs, 1);
  554.                         if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  555.                         {
  556.                             eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  557.                         }
  558.                         vec = t->mb.vec[0][0];
  559.                         T264_predict_mv(t, 0, 0, 4, &vec);
  560.                         eg_write_se(t->bs, t->mb.vec[0][0].x - vec.x);
  561.                         eg_write_se(t->bs, t->mb.vec[0][0].y - vec.y);
  562.                         break;
  563.                     case B_L1_16x16:
  564.                         eg_write_ue(t->bs, 2);
  565.                         if (t->ps.num_ref_idx_l1_active_minus1 > 0)
  566.                         {
  567.                             eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][0].refno);
  568.                         }
  569.                         vec = t->mb.vec[1][0];
  570.                         T264_predict_mv(t, 1, 0, 4, &vec);
  571.                         eg_write_se(t->bs, t->mb.vec[1][0].x - vec.x);
  572.                         eg_write_se(t->bs, t->mb.vec[1][0].y - vec.y);
  573.                         break;
  574.                     case B_Bi_16x16:
  575.                         eg_write_ue(t->bs, 3);
  576.                         if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  577.                         {
  578.                             eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  579.                         }
  580.                         if (t->ps.num_ref_idx_l1_active_minus1 > 0)
  581.                         {
  582.                             eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][0].refno);
  583.                         }
  584.                         vec = t->mb.vec[0][0];
  585.                         T264_predict_mv(t, 0, 0, 4, &vec);
  586.                         eg_write_se(t->bs, t->mb.vec[0][0].x - vec.x);
  587.                         eg_write_se(t->bs, t->mb.vec[0][0].y - vec.y);
  588.                         vec = t->mb.vec[1][0];
  589.                         T264_predict_mv(t, 1, 0, 4, &vec);
  590.                         eg_write_se(t->bs, t->mb.vec[1][0].x - vec.x);
  591.                         eg_write_se(t->bs, t->mb.vec[1][0].y - vec.y);
  592.                         break;
  593.                     }
  594.                 }
  595.                 else
  596.                 {
  597.                     eg_write_ue(t->bs, 0);
  598.                 }
  599.                 break;
  600.             case MB_16x8:
  601.                 {
  602.                     static int8_t mode_16x8[3][3] = 
  603.                     {
  604.                         B_L0_L0_16x8, B_L0_L1_16x8, B_L0_Bi_16x8,
  605.                         B_L1_L0_16x8, B_L1_L1_16x8, B_L1_Bi_16x8,
  606.                         B_Bi_L0_16x8, B_Bi_L1_16x8, B_Bi_Bi_16x8
  607.                     };
  608.                     eg_write_ue(t->bs, mode_16x8[t->mb.mb_part2[0] - B_L0_16x8][t->mb.mb_part2[1] - B_L0_16x8]);
  609.                     if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  610.                     {
  611.                         if (t->mb.mb_part2[0] != B_L1_16x8)
  612.                             eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  613.                         if (t->mb.mb_part2[1] != B_L1_16x8)
  614.                             eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][8].refno);
  615.                     }
  616.                     if (t->ps.num_ref_idx_l1_active_minus1 > 0)
  617.                     {
  618.                         if (t->mb.mb_part2[0] != B_L0_16x8)
  619.                             eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][0].refno);
  620.                         if (t->mb.mb_part2[1] != B_L0_16x8)
  621.                             eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][8].refno);
  622.                     }
  623.                     // l0
  624.                     for (i = 0 ; i < 2 ; i ++)
  625.                     {
  626.                         switch(t->mb.mb_part2[i]) 
  627.                         {
  628.                         case B_L0_16x8:
  629.                             vec = t->mb.vec[0][8 * i];
  630.                             T264_predict_mv(t, 0, luma_index[8 * i], 4, &vec);
  631.                             eg_write_se(t->bs, t->mb.vec[0][luma_index[8 * i]].x - vec.x);
  632.                             eg_write_se(t->bs, t->mb.vec[0][luma_index[8 * i]].y - vec.y);
  633.                             break;
  634.                         case B_Bi_16x8:
  635.                             vec = t->mb.vec[0][luma_index[8 * i]];
  636.                             T264_predict_mv(t, 0, luma_index[8 * i], 4, &vec);
  637.                             eg_write_se(t->bs, t->mb.vec[0][luma_index[8 * i]].x - vec.x);
  638.                             eg_write_se(t->bs, t->mb.vec[0][luma_index[8 * i]].y - vec.y);
  639.                         }
  640.                     }
  641.                     for (i = 0 ; i < 2 ; i ++)
  642.                     {
  643.                         switch(t->mb.mb_part2[i]) 
  644.                         {
  645.                         case B_L1_16x8:
  646.                             vec = t->mb.vec[1][luma_index[8 * i]];
  647.                             T264_predict_mv(t, 1, luma_index[8 * i], 4, &vec);
  648.                             eg_write_se(t->bs, t->mb.vec[1][luma_index[8 * i]].x - vec.x);
  649.                             eg_write_se(t->bs, t->mb.vec[1][luma_index[8 * i]].y - vec.y);
  650.                             break;
  651.                         case B_Bi_16x8:
  652.                             vec = t->mb.vec[1][luma_index[8 * i]];
  653.                             T264_predict_mv(t, 1, luma_index[8 * i], 4, &vec);
  654.                             eg_write_se(t->bs, t->mb.vec[1][luma_index[8 * i]].x - vec.x);
  655.                             eg_write_se(t->bs, t->mb.vec[1][luma_index[8 * i]].y - vec.y);
  656.                             break;
  657.                         }
  658.                     }
  659.                 }
  660.                 break;
  661.             case MB_8x16:
  662.                 {
  663.                     static int8_t mode_8x16[3][3] = 
  664.                     {
  665.                         B_L0_L0_8x16, B_L0_L1_8x16, B_L0_Bi_8x16,
  666.                         B_L1_L0_8x16, B_L1_L1_8x16, B_L1_Bi_8x16,
  667.                         B_Bi_L0_8x16, B_Bi_L1_8x16, B_Bi_Bi_8x16
  668.                     };
  669.                     eg_write_ue(t->bs, mode_8x16[t->mb.mb_part2[0] - B_L0_8x16][t->mb.mb_part2[1] - B_L0_8x16]);
  670.                     if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  671.                     {
  672.                         if (t->mb.mb_part2[0] != B_L1_8x16)
  673.                             eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][0].refno);
  674.                         if (t->mb.mb_part2[1] != B_L1_8x16)
  675.                             eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][2].refno);
  676.                     }
  677.                     if (t->ps.num_ref_idx_l1_active_minus1 > 0)
  678.                     {
  679.                         if (t->mb.mb_part2[0] != B_L0_8x16)
  680.                             eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][0].refno);
  681.                         if (t->mb.mb_part2[1] != B_L0_8x16)
  682.                             eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][2].refno);
  683.                     }
  684.                     // l0
  685.                     for (i = 0 ; i < 2 ; i ++)
  686.                     {
  687.                         switch(t->mb.mb_part2[i]) 
  688.                         {
  689.                         case B_L0_8x16:
  690.                             vec = t->mb.vec[0][2 * i];
  691.                             T264_predict_mv(t, 0, luma_index[4 * i], 2, &vec);
  692.                             eg_write_se(t->bs, t->mb.vec[0][2 * i].x - vec.x);
  693.                             eg_write_se(t->bs, t->mb.vec[0][2 * i].y - vec.y);
  694.                             break;
  695.                         case B_Bi_8x16:
  696.                             vec = t->mb.vec[0][2 * i];
  697.                             T264_predict_mv(t, 0, luma_index[4 * i], 2, &vec);
  698.                             eg_write_se(t->bs, t->mb.vec[0][2 * i].x - vec.x);
  699.                             eg_write_se(t->bs, t->mb.vec[0][2 * i].y - vec.y);
  700.                         }
  701.                     }
  702.                     for (i = 0 ; i < 2 ; i ++)
  703.                     {
  704.                         switch(t->mb.mb_part2[i]) 
  705.                         {
  706.                         case B_L1_8x16:
  707.                             vec = t->mb.vec[1][2 * i];
  708.                             T264_predict_mv(t, 1, luma_index[4 * i], 2, &vec);
  709.                             eg_write_se(t->bs, t->mb.vec[1][2 * i].x - vec.x);
  710.                             eg_write_se(t->bs, t->mb.vec[1][2 * i].y - vec.y);
  711.                             break;
  712.                         case B_Bi_8x16:
  713.                             vec = t->mb.vec[1][2 * i];
  714.                             T264_predict_mv(t, 1, luma_index[4 * i], 2, &vec);
  715.                             eg_write_se(t->bs, t->mb.vec[1][2 * i].x - vec.x);
  716.                             eg_write_se(t->bs, t->mb.vec[1][2 * i].y - vec.y);
  717.                             break;
  718.                         }
  719.                     }
  720.                 }
  721.                 break;
  722.             case MB_8x8:
  723.                 {
  724.                     eg_write_ue(t->bs, 22);
  725.                     for (i = 0 ; i < 4 ; i ++)
  726.                     {
  727.                         if (t->mb.sub_is_copy[i] != 1)
  728.                         {
  729.                             switch (t->mb.submb_part[luma_index[4 * i]]) 
  730.                             {
  731.                             case B_L0_8x8:
  732.                                 eg_write_ue(t->bs, 1);
  733.                                 break;
  734.                             case B_L1_8x8:
  735.                                 eg_write_ue(t->bs, 2);
  736.                                 break;
  737.                             case B_Bi_8x8:
  738.                                 eg_write_ue(t->bs, 3);
  739.                                 break;
  740.                             default:
  741.                                 assert(0);
  742.                                 break;
  743.                             }
  744.                         }
  745.                         else
  746.                         {
  747.                             eg_write_ue(t->bs, 0);
  748.                             break;
  749.                         }
  750.                     }
  751.                     if (t->ps.num_ref_idx_l0_active_minus1 > 0)
  752.                     {
  753.                         for (i = 0 ; i < 4 ; i ++)
  754.                         {
  755.                             if (t->mb.submb_part[luma_index[4 * i]] != 2 && t->mb.submb_part[luma_index[4 * i]] != 0)
  756.                                 eg_write_te(t->bs, t->ps.num_ref_idx_l0_active_minus1, t->mb.vec[0][4 * i].refno);
  757.                         }
  758.                     }
  759.                     if (t->ps.num_ref_idx_l1_active_minus1 > 0)
  760.                     {
  761.                         for (i = 0 ; i < 4 ; i ++)
  762.                         {
  763.                             if (t->mb.submb_part[luma_index[4 * i]] != 1 && t->mb.submb_part[luma_index[4 * i]] != 0)
  764.                                 eg_write_te(t->bs, t->ps.num_ref_idx_l1_active_minus1, t->mb.vec[1][4 * i].refno);
  765.                         }
  766.                     }
  767.                     // l0
  768.                     for (i = 0 ; i < 4 ; i ++)
  769.                     {
  770.                         switch(t->mb.submb_part[luma_index[4 * i]]) 
  771.                         {
  772.                         case B_L0_8x8:
  773.                         case B_Bi_8x8:
  774.                             vec = t->mb.vec[0][luma_index[4 * i]];
  775.                             T264_predict_mv(t, 0, luma_index[4 * i], 2, &vec);
  776.                             eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].x - vec.x);
  777.                             eg_write_se(t->bs, t->mb.vec[0][luma_index[4 * i]].y - vec.y);
  778.                             break;
  779.                         }
  780.                     }
  781.                     for (i = 0 ; i < 4 ; i ++)
  782.                     {
  783.                         switch(t->mb.submb_part[luma_index[4 * i]]) 
  784.                         {
  785.                         case B_L1_8x8:
  786.                         case B_Bi_8x8:
  787.                             vec = t->mb.vec[1][luma_index[4 * i]];
  788.                             T264_predict_mv(t, 1, luma_index[4 * i], 2, &vec);
  789.                             eg_write_se(t->bs, t->mb.vec[1][luma_index[4 * i]].x - vec.x);
  790.                             eg_write_se(t->bs, t->mb.vec[1][luma_index[4 * i]].y - vec.y);
  791.                             break;
  792.                         }
  793.                     }
  794.                 }
  795.                 break;
  796.             }
  797.         }
  798.         eg_write_ue(t->bs, inter_cbp_to_golomb[(t->mb.cbp_c << 4)| t->mb.cbp_y]);
  799.         //delta_qp
  800.         if (t->mb.cbp_y > 0 || t->mb.cbp_c > 0)
  801.         {
  802.             eg_write_se(t->bs, t->mb.mb_qp_delta); /* 0 = no change on qp */
  803.             for (i = 0; i < 16 ; i ++)
  804.             {
  805.                 if(t->mb.cbp_y & (1 << ( i / 4 )))
  806.                 {
  807.                     block_residual_write_cavlc(t, i, t->mb.dct_y_z[i], 16);
  808.                 }
  809.             }
  810.         }
  811.     }
  812.     if (t->mb.cbp_c != 0)
  813.     {
  814.         block_residual_write_cavlc(t, BLOCK_INDEX_CHROMA_DC, t->mb.dc2x2_z[0], 4);
  815.         block_residual_write_cavlc(t, BLOCK_INDEX_CHROMA_DC, t->mb.dc2x2_z[1], 4);
  816.         if (t->mb.cbp_c & 0x2)
  817.         {
  818.             for(i = 0 ; i < 8 ; i ++)
  819.             {
  820.                 block_residual_write_cavlc(t, 16 + i, &(t->mb.dct_uv_z[i / 4][i % 4][1]), 15);
  821.             }
  822.         }
  823.     }
  824. }