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

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. #include "config.h"
  24. #include "stdio.h"
  25. #include "T264.h"
  26. #include "bitstream.h"
  27. #include "utility.h"
  28. #ifndef CHIP_DM642
  29. #include "memory.h"
  30. #endif
  31. #include "assert.h"
  32. #ifndef CHIP_DM642
  33. #include "sse2.h"
  34. #endif
  35. #include "interpolate.h"
  36. #include "math.h"
  37. #include "dct.h"
  38. #include "dec_cavlc.h"
  39. #include "deblock.h"
  40. #include "block.h"
  41. //for dec CABAC
  42. #include "cabac_engine.h"
  43. #include "dec_cabac.h"
  44. #include "inter.h"
  45. #define NAL_BUFFER_LEN 1024 * 1024  /* big enough ? */
  46. void
  47. T264dec_load_ref(T264_t* t)
  48. {
  49.     int32_t i, j, k;
  50.     t->rec = &t->refn[0];
  51.     if (t->slice_type == SLICE_P)
  52.     {
  53.         for(i = 0, j = 0 ; i < t->refl0_num ; i ++)
  54.         {
  55.             if (t->refn[i + 1].poc >= 0)
  56.             {
  57.                 t->ref[0][j ++] = &t->refn[i + 1];
  58.             }
  59.         }
  60.     }
  61.     else if (t->slice_type == SLICE_B)
  62.     {
  63.         for(i = 0, j = 0, k = 0 ; i < t->refl0_num + t->refl1_num; i ++)
  64.         {
  65.             if (t->refn[i + 1].poc < t->slice.pic_order_cnt_lsb)
  66.             {
  67.                 if (t->refn[i + 1].poc >= 0)
  68.                     t->ref[0][j ++] = &t->refn[i + 1];
  69.             }
  70.             else
  71.             {
  72.                 // yes, t->refn[i].poc already > 0
  73.                 t->ref[1][k ++] = &t->refn[i + 1];
  74.             }
  75.         }
  76.     }
  77. }
  78. void
  79. T264dec_save_ref(T264_t* t)
  80. {
  81.     int32_t i;
  82.     T264_frame_t tmp;
  83.     /* deblock filter exec here */
  84.     if (t->need_deblock)
  85.         T264_deblock_frame(t, t->rec);
  86.     /* current only del with i,p */
  87.     T264_extend_border(t, t->rec);
  88.     T264_interpolate_halfpel(t, t->rec);
  89.     tmp = t->refn[t->ss.num_ref_frames];
  90.     t->refn[0].poc = t->slice.pic_order_cnt_lsb;
  91.     for(i = t->ss.num_ref_frames ; i >= 1 ; i --)
  92.     {
  93.         t->refn[i] = t->refn[i - 1];
  94.     }
  95.     t->refn[0] = tmp;
  96. }
  97. void
  98. T264dec_mb_load_context(T264_t* t, int32_t mb_y, int32_t mb_x)
  99. {
  100.     int32_t i, j;
  101.     /* nnz count will be set in read_cavlc, but in many cases, 
  102.         nnz will equal to 0 because cbp == 0
  103.      */
  104.     memset(t->mb.nnz, 0, sizeof(t->mb.nnz));
  105.     memset(t->mb.nnz_ref, 0, sizeof(t->mb.nnz_ref));
  106.     memset(t->mb.mode_i4x4, Intra_4x4_DC, 16 * sizeof(uint8_t));
  107.     memset(t->mb.submb_part, -1, sizeof(t->mb.submb_part));
  108.     memset(t->mb.dct_y_z, 0, sizeof(t->mb.dct_y_z));
  109.     memset(t->mb.dct_uv_z, 0, sizeof(t->mb.dct_uv_z));
  110.     memset(t->mb.dc4x4_z, 0, sizeof(t->mb.dc4x4_z));
  111.     memset(t->mb.dc2x2_z, 0, sizeof(t->mb.dc2x2_z));
  112.     t->mb.mb_part = -1;
  113. #define INITINVALIDVEC(vec) vec.refno = -1; vec.x = vec.y = 0;
  114.     for(i = 0 ; i < 2 ; i ++)
  115.     {
  116.         for(j = 0 ; j < 16 ; j ++)
  117.         {
  118.             INITINVALIDVEC(t->mb.vec[i][j]);
  119.         }
  120.     }
  121. #undef INITINVALIDVEC
  122.     T264_mb_load_context(t, mb_y, mb_x);
  123.     
  124.     t->mb.src_y = t->mb.dst_y;
  125.     t->mb.src_u = t->mb.dst_u;
  126.     t->mb.src_v = t->mb.dst_v;
  127. }
  128. void
  129. T264dec_mb_decode(T264_t* t)
  130. {
  131.     /* p skip decode as p mode */
  132.     if(t->mb.mb_mode == P_MODE)
  133.     {
  134.         T264dec_mb_decode_interp_y(t);
  135.         //
  136.         // Chroma
  137.         //
  138.         T264dec_mb_decode_interp_uv(t);
  139.         t->stat.p_block_num[t->mb.mb_part] ++;
  140.     }
  141.     else if (t->mb.mb_mode == B_MODE)
  142.     {
  143.         T264dec_mb_decode_interb_y(t);
  144.         //
  145.         // Chroma
  146.         //
  147.         T264dec_mb_decode_interb_uv(t);
  148.         t->stat.b_block_num[t->mb.mb_part] ++;
  149.     }
  150.     else if (t->mb.mb_mode == I_4x4 || t->mb.mb_mode == I_16x16)
  151.     {
  152.         T264dec_mb_decode_intra_y(t);
  153.         //
  154.         // Chroma
  155.         //
  156.         T264dec_mb_decode_intra_uv(t);
  157.         t->stat.i_block_num[t->mb.mb_mode] ++;
  158.     }
  159. }
  160. void
  161. T264dec_mb_save_context(T264_t* t, int32_t i, int32_t j)
  162. {
  163.     memcpy(t->mb.context, &t->mb, sizeof(*t->mb.context));
  164. }
  165. void
  166. T264dec_parse_slice_header(T264_t* t)
  167. {
  168.     t->slice.first_mb_in_slice = eg_read_ue(t->bs); 
  169.     assert(t->slice.first_mb_in_slice == 0);
  170.     t->slice_type = t->slice.slice_type = eg_read_ue(t->bs);
  171.     t->slice.pic_id = eg_read_ue(t->bs);    
  172.     t->slice.frame_num = eg_read_direct(t->bs, t->ss.log2_max_frame_num_minus4 + 4);
  173.     if (t->nal.nal_unit_type == NAL_SLICE_IDR)
  174.     {
  175.         t->slice.idr_pic_id = eg_read_ue(t->bs);
  176.     }
  177.     if (t->ss.pic_order_cnt_type == 0)
  178.     {
  179.         t->poc = t->slice.pic_order_cnt_lsb = eg_read_direct(t->bs, t->ss.max_pic_order + 4);
  180.     }
  181.     if (t->slice_type == SLICE_P)
  182.     {
  183.         t->refl1_num = 0;
  184.         t->refl0_num = t->ps.num_ref_idx_l0_active_minus1 + 1;
  185.         // num_ref_idx_active_override_flag
  186.         if (eg_read_direct(t->bs, 1))
  187.         {
  188.             t->refl0_num = eg_read_ue(t->bs) + 1;
  189.         }
  190.     }
  191.     else if (t->slice_type == SLICE_B)
  192.     {
  193.         // direct_spatial_mv_pred_flag
  194.         t->param.direct_flag = t->slice.direct_spatial_mv_pred_flag = eg_read_direct(t->bs, 1);
  195.         t->refl1_num = 1;
  196.         t->refl0_num = t->ps.num_ref_idx_l0_active_minus1 + 1;
  197.         // num_ref_idx_active_override_flag
  198.         if (eg_read_direct(t->bs, 1))
  199.         {
  200.             t->refl0_num = eg_read_ue(t->bs) + 1;
  201.             t->refl1_num = eg_read_ue(t->bs) + 1;
  202.         }
  203.     }
  204.     /* ref_pic_list_reordering() */
  205.     if(t->slice.slice_type != SLICE_I && t->slice.slice_type != SLICE_SI )
  206.     {
  207.         eg_read_skip(t->bs, 1);
  208.         if (t->slice.slice_type == SLICE_B)
  209.             eg_read_skip(t->bs, 1);
  210.     }
  211.     if (t->nal.nal_ref_idc != 0)
  212.     {
  213.         /* dec_ref_pic_marking() */
  214.         if (t->nal.nal_unit_type == NAL_SLICE_IDR)
  215.         {
  216.             eg_read_direct(t->bs, 1);
  217.             eg_read_direct(t->bs, 1);
  218.         }
  219.         else
  220.         {
  221.             eg_read_direct(t->bs, 1);
  222.         }
  223.     }
  224. //for dec CABAC
  225. if(t->ps.entroy_coding_mode_flag!=0 && t->slice_type!= SLICE_I)
  226. {
  227. t->slice.cabac_init_idc = eg_read_ue(t->bs);
  228. }
  229.     t->qp_y = eg_read_se(t->bs) + t->ps.pic_init_qp_minus26 + 26;
  230.     if (t->ps.deblocking_filter_control_present_flag)
  231.     {
  232.         t->need_deblock = !eg_read_ue(t->bs);
  233.     }
  234. }
  235. static void __inline
  236. get_output_frame(T264_t* t)
  237. {
  238.     /* we will reorder the output frame */
  239.     if (t->slice_type != SLICE_B)
  240.     {
  241.         t->output.poc = -2;
  242.         if (t->frame_num > 0)
  243.         {
  244.             //
  245.             // refn[0] = current reconstruct frame
  246.             // refn[1] = ref frame 1
  247.             // refn[2] = ref frame 2
  248.             //
  249.             t->output = t->refn[1];
  250.         }
  251.     }
  252.     else
  253.     {
  254.         // slice b always should send immediately
  255.         t->output = t->refn[0];
  256.     }
  257. }
  258. /* currently we _only_ support one slice */
  259. void
  260. T264dec_parse_slice(T264_t* t)
  261. {
  262.     int32_t i, j, skip, end_of_slice;
  263. //to be cleaned
  264. //extern int frame_cabac, mb_cabac, slice_type_cabac;
  265.     T264dec_parse_slice_header(t);
  266.     
  267.     T264dec_load_ref(t);
  268. //for dec CABAC
  269. if( t->ps.entroy_coding_mode_flag == 1 )
  270. {
  271. /* alignment needed */
  272. BitstreamByteAlign( t->bs );
  273. /* init cabac */
  274. T264_cabac_context_init( &t->cabac, t->slice_type, t->ps.pic_init_qp_minus26+26+t->slice.slice_qp_delta, t->slice.cabac_init_idc );
  275. T264_cabac_decode_init ( &t->cabac, t->bs );
  276. }
  277.     t->skip = -1;
  278.     for(i = 0 ; i < t->mb_height ; i ++)
  279.     {
  280.         for(j = 0 ; j < t->mb_width ; j ++)
  281.         {
  282.             T264dec_mb_load_context(t, i, j);
  283.             if( t->ps.entroy_coding_mode_flag == 1 )
  284. {
  285. //CABAC
  286. if(t->mb.mb_xy > 0)
  287. {
  288. end_of_slice = T264_cabac_decode_terminal(&t->cabac);
  289. assert(end_of_slice == 0);
  290. }
  291. skip = T264dec_mb_read_cabac(t);
  292. }
  293. else
  294. {
  295. //CAVLC
  296. T264dec_mb_read_cavlc(t);
  297. }
  298.             T264dec_mb_decode(t);
  299. if(t->ps.entroy_coding_mode_flag==1 && skip)
  300. {
  301. t->mb.mb_mode = (t->slice_type==SLICE_B)?B_SKIP:P_SKIP;
  302. }
  303.             T264dec_mb_save_context(t, i, j);
  304.         }
  305.     }
  306. if( t->ps.entroy_coding_mode_flag == 1 )
  307. {
  308. /* end of slice */
  309. end_of_slice = T264_cabac_decode_terminal(&t->cabac);
  310. assert(end_of_slice == 1);
  311. BitstreamByteAlign(t->bs);
  312. }
  313. //for dec CABAC
  314. if( t->ps.entroy_coding_mode_flag == 1 )
  315. {
  316. T264_cabac_model_update( &t->cabac, t->slice_type, t->ps.pic_init_qp_minus26+26 + t->slice.slice_qp_delta );
  317. }
  318.     get_output_frame(t);
  319.     if (t->slice_type != SLICE_B)
  320.     {
  321.         T264dec_save_ref(t);
  322.     }
  323.     else if (t->need_deblock)
  324.     {
  325.         T264_deblock_frame(t, t->rec);
  326.     }
  327.     t->emms();
  328. }
  329. void
  330. T264dec_parse_pic_header(T264_t* t)
  331. {
  332.     t->ps.pic_id = eg_read_ue(t->bs);
  333.     t->ps.seq_id = eg_read_ue(t->bs);
  334.     assert(t->ps.pic_id == 0 && t->ps.seq_id == 0);
  335.     t->ps.entroy_coding_mode_flag = eg_read_direct(t->bs, 1);
  336. //for dec CABAC, now support CABAC
  337.     //assert(t->ps.entroy_coding_mode_flag == 0);
  338.     t->ps.pic_order_present_flag = eg_read_direct(t->bs, 1);
  339.     assert(t->ps.pic_order_present_flag == 0);
  340.     t->ps.num_slice_groups_minus1 = eg_read_ue(t->bs);
  341.     assert(t->ps.num_slice_groups_minus1 == 0);
  342.     t->ps.num_ref_idx_l0_active_minus1 = eg_read_ue(t->bs);
  343.     t->ps.num_ref_idx_l1_active_minus1 = eg_read_ue(t->bs);
  344.     t->ps.weighted_pred_flag = eg_read_direct(t->bs, 1);
  345.     t->ps.weighted_bipred_idc = eg_read_direct(t->bs, 2);
  346.     t->ps.pic_init_qp_minus26 = eg_read_se(t->bs);
  347.     t->ps.pic_init_qs_minus26 = eg_read_se(t->bs);
  348.     t->ps.chroma_qp_index_offset = eg_read_se(t->bs);
  349.     t->ps.deblocking_filter_control_present_flag = eg_read_direct(t->bs, 1);
  350.     t->refl0_num = t->ps.num_ref_idx_l0_active_minus1 + 1;
  351.     t->refl1_num = t->ps.num_ref_idx_l1_active_minus1 + 1;
  352.     t->qp_y = t->ps.pic_init_qp_minus26 + 26;
  353. }
  354. void
  355. T264dec_parse_seq_header(T264_t* t)
  356. {
  357.     int32_t i;
  358.     int32_t prev_ref_num = t->ss.num_ref_frames;
  359.     t->ss.profile_idc = eg_read_direct(t->bs, 8);
  360.     eg_read_skip(t->bs, 8);
  361.     t->ss.level_idc = eg_read_direct(t->bs, 8);
  362.     t->ss.seq_id = eg_read_ue(t->bs);
  363.     assert(t->ss.seq_id == 0);
  364.     t->ss.log2_max_frame_num_minus4 = eg_read_ue(t->bs);
  365.     t->ss.pic_order_cnt_type = eg_read_ue(t->bs);
  366.     assert(t->ss.pic_order_cnt_type == 0);
  367.     if (t->ss.pic_order_cnt_type == 0)
  368.     {
  369.         t->ss.max_pic_order = eg_read_ue(t->bs);
  370.     }
  371.     t->ss.num_ref_frames = eg_read_ue(t->bs);
  372.     eg_read_skip(t->bs, 1);
  373.     t->ss.pic_width_in_mbs_minus1 = eg_read_ue(t->bs);
  374.     t->ss.pic_height_in_mbs_minus1 = eg_read_ue(t->bs);
  375.     t->ss.frame_mbs_only_flag = eg_read_direct(t->bs, 1);
  376.     assert(t->ss.frame_mbs_only_flag == 1);
  377.     eg_read_skip(t->bs, 2);
  378.     // vui_parameters_present_flag
  379.     if (eg_read_direct(t->bs, 1))
  380.     {
  381.         // aspect_ratio_info_present_flag
  382.         if (eg_read_direct(t->bs, 1) == 1)
  383.             t->aspect_ratio = eg_read_direct(t->bs, 8);
  384.         // overscan_info_present_flag
  385.         eg_read_skip(t->bs, 1);
  386.         // video_signal_type_present_flag
  387.         if (eg_read_direct(t->bs, 1) == 1)
  388.             t->video_format = eg_read_direct(t->bs, 3);
  389.         // others discard
  390.     }
  391.     t->mb_height = t->ss.pic_height_in_mbs_minus1 + 1;
  392.     t->mb_width = t->ss.pic_width_in_mbs_minus1 + 1;
  393.     t->width = t->mb_width * 16;
  394.     t->height = t->mb_height * 16;
  395.     t->edged_width = t->width + 2 * EDGED_WIDTH;
  396.     t->edged_height = t->height + 2 * EDGED_HEIGHT;
  397.     t->stride    = t->width;
  398.     t->stride_uv = t->width >> 1;
  399.     t->edged_stride = t->edged_width;
  400.     t->edged_stride_uv = t->edged_width >> 1;
  401.     t->mb_stride = t->mb_width;
  402.     /* malloc ref frame buffer */
  403.     /* first we malloc current decode buffer */
  404.     if (t->refn[0].Y[0] == 0)
  405.     {
  406.         uint8_t* p = T264_malloc(t->edged_width * t->edged_height + (t->edged_width * t->edged_height >> 1), CACHE_SIZE);
  407.         t->refn[0].Y[0] = p + EDGED_HEIGHT * t->edged_width + EDGED_WIDTH;
  408.         t->refn[0].U = p + t->edged_width * t->edged_height + (t->edged_width * EDGED_HEIGHT >> 2) + (EDGED_WIDTH >> 1);
  409.         t->refn[0].V = p + t->edged_width * t->edged_height + (t->edged_width * t->edged_height >> 2) + (t->edged_width * EDGED_HEIGHT >> 2) + (EDGED_WIDTH >> 1);
  410.         t->refn[0].mb = T264_malloc(t->mb_height * t->mb_width * sizeof(T264_mb_context_t), CACHE_SIZE);
  411.         p = T264_malloc(t->edged_width * t->edged_height * 3, CACHE_SIZE);
  412.         t->refn[0].Y[1] = p + EDGED_HEIGHT * t->edged_width + EDGED_WIDTH;
  413.         t->refn[0].Y[2] = t->refn[0].Y[1] + t->edged_width * t->edged_height;
  414.         t->refn[0].Y[3] = t->refn[0].Y[2] + t->edged_width * t->edged_height;
  415.     }
  416.     for (i = prev_ref_num ; i < t->ss.num_ref_frames ; i ++)
  417.     {
  418.         uint8_t* p = T264_malloc(t->edged_width * t->edged_height + (t->edged_width * t->edged_height >> 1), CACHE_SIZE);
  419.         t->refn[i + 1].Y[0] = p + EDGED_HEIGHT * t->edged_width + EDGED_WIDTH;
  420.         t->refn[i + 1].U = p + t->edged_width * t->edged_height + (t->edged_width * EDGED_HEIGHT >> 2) + (EDGED_WIDTH >> 1);
  421.         t->refn[i + 1].V = p + t->edged_width * t->edged_height + (t->edged_width * t->edged_height >> 2) + (t->edged_width * EDGED_HEIGHT >> 2) + (EDGED_WIDTH >> 1);
  422.         t->refn[i + 1].mb = T264_malloc(t->mb_height * t->mb_width * sizeof(T264_mb_context_t), CACHE_SIZE);
  423.         p = T264_malloc(t->edged_width * t->edged_height * 3, CACHE_SIZE);
  424.         t->refn[i + 1].Y[1] = p + EDGED_HEIGHT * t->edged_width + EDGED_WIDTH;
  425.         t->refn[i + 1].Y[2] = t->refn[i + 1].Y[1] + t->edged_width * t->edged_height;
  426.         t->refn[i + 1].Y[3] = t->refn[i + 1].Y[2] + t->edged_width * t->edged_height;
  427.     }
  428.     t->refl0_num = 0;
  429.     t->refl1_num = 0;
  430. }
  431. void
  432. T264dec_parse_custom_set(T264_t* t)
  433. {
  434.     int32_t encoder_ver = eg_read_direct(t->bs, 32);
  435.     int32_t flag = eg_read_direct(t->bs, 32);
  436.     if (flag & CUSTOM_FASTINTERPOLATE)
  437.         t->flags |= USE_FASTINTERPOLATE;
  438. }
  439. void
  440. T264dec_custom_buffer(T264_t* t, uint8_t* buf, int32_t stride)
  441. {
  442. }
  443. decoder_state_t
  444. T264dec_copy_nal(T264_t* t)
  445. {
  446.     uint8_t tmp;
  447.     uint32_t shift;
  448.     uint32_t shift1;
  449.     shift = t->shift;
  450.     shift1 = t->shift1;
  451.     while (t->src_buf < t->src_end)
  452.     {
  453.         tmp = *t->src_buf ++;
  454.         shift1 = (shift1 | tmp) << 8;
  455.         if (shift1 == 0x00000300)
  456.         {
  457.             shift1 = 0xffffff00;    /* reset state */
  458.             /* shift == x x 0 0 ==> shift == x x 0 ff
  459.                here we need to prevent the next few words become a wrong start code */
  460.             shift |= 0xff;
  461.             continue;
  462.         }
  463.         shift = (shift << 8) | tmp;
  464.         if (shift == 0x00000001)
  465.         {
  466.             t->shift1 = 0xffffff00;
  467.             t->shift = -1;
  468.             /* we do not copy the next start code */
  469.             t->nal_len -= 3;
  470.             return DEC_STATE_OK;
  471.         }
  472.         t->nal_buf[t->nal_len ++] = tmp;
  473.     }
  474.     t->shift = shift;
  475.     t->shift1 = shift1;
  476.     return DEC_STATE_BUFFER;
  477. }
  478. decoder_state_t
  479. T264dec_find_nal(T264_t* t)
  480. {
  481.     int32_t find = 0;
  482.     uint8_t tmp;
  483.     uint32_t shift;
  484.     shift = t->shift;
  485.     while (t->src_buf < t->src_end)
  486.     {
  487.         tmp = *t->src_buf ++;
  488.         shift = (shift << 8) | tmp;
  489.         
  490.         if (shift == 0x00000001)
  491.         {
  492.             t->shift = -1;
  493.             t->nal_len = 0;
  494.             find = 1;
  495.             break;
  496.         }
  497.     }
  498.     if (find)
  499.     {
  500.         t->action = T264dec_copy_nal;
  501.         return T264dec_parse(t);
  502.     }
  503.     t->shift = shift;
  504.     return DEC_STATE_BUFFER;
  505. }
  506. decoder_state_t
  507. T264dec_decode_nal(T264_t* t)
  508. {
  509.     eg_init(t->bs, t->nal_buf, t->nal_len);
  510.     /* ready for next nal */
  511.     t->action = T264dec_copy_nal;
  512.     t->nal_len = 0;
  513.     eg_read_skip(t->bs, 1);  /* discard forbidden + nal_ref_idc */
  514.     t->nal.nal_ref_idc = eg_read_direct(t->bs, 2);
  515.     t->nal.nal_unit_type = eg_read_direct(t->bs, 5);
  516.     switch (t->nal.nal_unit_type)
  517.     {
  518.     case NAL_SLICE_NOPART:
  519.         T264dec_parse_slice(t);
  520.         t->frame_num ++;
  521.         t->frame_id ++;
  522.         return DEC_STATE_SLICE;
  523.     case NAL_PIC_SET:
  524.         T264dec_parse_pic_header(t);
  525.         return DEC_STATE_PIC;
  526.     case NAL_SLICE_IDR:
  527.         T264dec_parse_slice(t);
  528.         t->frame_num ++;
  529.         t->frame_id ++;
  530.         return DEC_STATE_SLICE;
  531.     case NAL_SEQ_SET:
  532.         T264dec_parse_seq_header(t);
  533.         t->frame_num = 0;
  534.         return DEC_STATE_SEQ;
  535.     case NAL_CUSTOM_SET:
  536.         T264dec_parse_custom_set(t);
  537.         return DEC_STATE_CUSTOM_SET;
  538.     default:
  539.         assert(0);
  540.         break;
  541.     }
  542.     return DEC_STATE_UNKOWN;
  543. }
  544. T264_t*
  545. T264dec_open()
  546. {
  547.     T264_t* t = T264_malloc(sizeof(T264_t), CACHE_SIZE);
  548. int i, j;
  549.     memset(t, 0, sizeof(T264_t));
  550.     t->nal_buf = T264_malloc(NAL_BUFFER_LEN, CACHE_SIZE);
  551.     t->bs = T264_malloc(sizeof(bs_t), CACHE_SIZE);
  552. //for cabac
  553. for(j=0; j<4; j++)
  554. {
  555. for(i=5; i<8; i++)
  556. {
  557. t->mb.vec_ref[(j<<3)+i].vec[0].refno = -2;
  558. t->mb.vec_ref[(j<<3)+i].vec[0].x = 0;
  559. t->mb.vec_ref[(j<<3)+i].vec[0].y = 0;
  560. t->mb.vec_ref[(j<<3)+i].vec[1].refno = -2;
  561. t->mb.vec_ref[(j<<3)+i].vec[1].x = 0;
  562. t->mb.vec_ref[(j<<3)+i].vec[1].y = 0;
  563. }
  564. }
  565.     T264_init_cpu(t);
  566.     t->action = T264dec_find_nal;
  567.     t->shift = -1;
  568.     t->shift1 = 0xffffff00;
  569.     t->need_deblock = 1;
  570.     t->flags = USE_HALFPEL| USE_QUARTPEL;   /* interpolate func needs this flag */
  571.     return t;
  572. }
  573. void
  574. T264dec_close(T264_t* t)
  575. {
  576.     int32_t i;
  577.     for(i = 0 ; i < t->ss.num_ref_frames + 1 ; i ++)
  578.     {
  579.         T264_free(t->refn[i].Y[0] - (EDGED_HEIGHT * t->edged_width + EDGED_WIDTH));
  580.         T264_free(t->refn[i].mb);
  581.         T264_free(t->refn[i].Y[1] - (EDGED_HEIGHT * t->edged_width + EDGED_WIDTH));
  582.     }
  583.     T264_free(t->bs);
  584.     T264_free(t->nal_buf);
  585.     T264_free(t);
  586. }
  587. void
  588. T264dec_buffer(T264_t* t, uint8_t* buf, int32_t len)
  589. {
  590.     t->src_buf = buf;
  591.     t->src_end = buf + len;
  592. }
  593. decoder_state_t
  594. T264dec_parse(T264_t* t)
  595. {
  596.     if (t->action(t) == DEC_STATE_BUFFER)
  597.     {
  598.         return DEC_STATE_BUFFER;
  599.     }
  600.     return T264dec_decode_nal(t);
  601. }
  602. T264_frame_t* 
  603. T264dec_flush_frame(T264_t* t)
  604. {
  605.     /* the last i/p frame */
  606.     return &t->refn[1];
  607. }