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

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 "stdio.h"
  24. #include "T264.h"
  25. #include "bitstream.h"
  26. #include "portab.h"
  27. #include "rbsp.h"
  28. #include "cabac_engine.h"
  29. void 
  30. nal_unit_init(_RW T264_nal_t* nal, int32_t nal_ref_idc, int32_t nal_unit_type)
  31. {
  32.     nal->nal_ref_idc   = nal_ref_idc;
  33.     nal->nal_unit_type = nal_unit_type;
  34. }
  35. void 
  36. nal_unit_write(_R T264_t* t, _R T264_nal_t* nal)
  37. {
  38.     eg_write_direct(t->bs, 1, 32);
  39.     eg_write_direct(t->bs, ((uint32_t)nal->nal_ref_idc) << 5 | nal->nal_unit_type, 8);
  40. }
  41. void 
  42. seq_set_init(_R T264_t* t, _RW T264_seq_set_t* seq)
  43. {
  44.     //
  45.     // NOTE: FIXME
  46.     //
  47. //for CABAC
  48. if( t->param.cabac || t->param.b_num > 0 )
  49. seq->profile_idc               = 77;
  50. else
  51. seq->profile_idc      = 66;
  52.   
  53.     seq->level_idc                 = 30;
  54.     seq->seq_id                    = 0;
  55.     seq->log2_max_frame_num_minus4 = 12;
  56.     seq->pic_order_cnt_type        = 0;
  57.     seq->max_pic_order             = 12;
  58.     seq->num_ref_frames            = t->param.ref_num;
  59.     seq->pic_width_in_mbs_minus1   = t->width / 16 - 1;
  60.     seq->pic_height_in_mbs_minus1  = t->height / 16 - 1;
  61.     seq->frame_mbs_only_flag       = 1;
  62. }
  63. void 
  64. seq_set_write(_R T264_t* t, _RW T264_seq_set_t* seq)
  65. {
  66.     eg_write_direct(t->bs, ((uint32_t)seq->profile_idc << 16) |seq->level_idc, 24);
  67.     eg_write_ue(t->bs, seq->seq_id);
  68.     eg_write_ue(t->bs, seq->log2_max_frame_num_minus4);
  69.     eg_write_ue(t->bs, seq->pic_order_cnt_type);
  70.     if (seq->pic_order_cnt_type == 0)
  71.     {
  72.         eg_write_ue(t->bs, seq->max_pic_order);
  73.     }
  74.     else
  75.     {
  76.         //
  77.         // ...
  78.         //
  79.     }
  80.     eg_write_ue(t->bs, seq->num_ref_frames);
  81.     eg_write_direct1(t->bs, 0);
  82.     eg_write_ue(t->bs, seq->pic_width_in_mbs_minus1);
  83.     eg_write_ue(t->bs, seq->pic_height_in_mbs_minus1);
  84.     eg_write_direct1(t->bs, seq->frame_mbs_only_flag);
  85.     eg_write_direct1(t->bs, 0);
  86.     eg_write_direct1(t->bs, 0);
  87.     // vui_parameters_present_flag
  88.     eg_write_direct1(t->bs, 1);
  89.     // aspect_ratio_info_present_flag
  90.     eg_write_direct1(t->bs, 1);
  91.     eg_write_direct(t->bs, t->param.aspect_ratio, 8);
  92.     // overscan_info_present_flag
  93.     eg_write_direct1(t->bs, 0);
  94.     // video_signal_type_present_flag
  95.     eg_write_direct1(t->bs, 1);
  96.     eg_write_direct(t->bs, t->param.video_format, 3);
  97.     // video_full_range_flag
  98.     eg_write_direct1(t->bs, 0);
  99.     // colour_description_present_flag
  100.     eg_write_direct1(t->bs, 0);
  101.     // chroma_loc_info_present_flag
  102.     eg_write_direct1(t->bs, 0);
  103.     // timing_info_present_flag
  104.     // nal_hrd_parameters_present_flag
  105.     // vcl_hrd_parameters_present_flag
  106.     // pic_struct_present_flag 
  107.     // bitstream_restriction_flag
  108.     eg_write_direct(t->bs, 0, 5);
  109.     rbsp_trailing_bits(t);
  110. }
  111. void 
  112. pic_set_init(_R T264_t* t, _RW T264_pic_set_t* pic)
  113. {
  114.     pic->seq_id = 0;
  115.     pic->pic_id = 0;
  116. //for CABAC
  117. if(t->param.cabac == 0)
  118.      pic->entroy_coding_mode_flag = 0;
  119. else
  120. pic->entroy_coding_mode_flag = 1;
  121.     pic->pic_order_present_flag = 0;
  122.     pic->num_slice_groups_minus1 = 0;
  123.     pic->num_ref_idx_l0_active_minus1 = t->param.ref_num - 1;
  124.     pic->num_ref_idx_l1_active_minus1 = 0;
  125.     pic->weighted_pred_flag = 0;
  126.     pic->weighted_bipred_idc = 0;
  127.     pic->pic_init_qp_minus26 = t->param.qp - 26;
  128.     pic->pic_init_qs_minus26 = t->param.qp - 26;
  129.     pic->chroma_qp_index_offset = 0;
  130.     if (t->param.disable_filter)
  131.         pic->deblocking_filter_control_present_flag = 1;
  132.     else
  133.         pic->deblocking_filter_control_present_flag = 0;
  134. }
  135. void 
  136. pic_set_write(_R T264_t* t, _RW T264_pic_set_t* pic)
  137. {
  138.     eg_write_ue(t->bs, pic->pic_id);
  139.     eg_write_ue(t->bs, pic->seq_id);
  140.     eg_write_direct1(t->bs, pic->entroy_coding_mode_flag);
  141.     eg_write_direct1(t->bs, pic->pic_order_present_flag);
  142.     eg_write_ue(t->bs, pic->num_slice_groups_minus1);
  143.     eg_write_ue(t->bs, pic->num_ref_idx_l0_active_minus1);
  144.     eg_write_ue(t->bs, pic->num_ref_idx_l1_active_minus1);
  145.     eg_write_direct1(t->bs, pic->weighted_pred_flag);
  146. //  eg_write_direct1(t->bs, pic->weighted_bipred_idc); /*error bits*/
  147. eg_write_direct(t->bs, pic->weighted_bipred_idc, 2);
  148.     eg_write_se(t->bs, pic->pic_init_qp_minus26);
  149.     eg_write_se(t->bs, pic->pic_init_qs_minus26);
  150.     eg_write_se(t->bs, pic->chroma_qp_index_offset);
  151.     eg_write_direct1(t->bs, pic->deblocking_filter_control_present_flag);
  152.     eg_write_direct1(t->bs, 0);
  153.     eg_write_direct1(t->bs, 0);
  154.     rbsp_trailing_bits(t);
  155. }
  156. void 
  157. slice_header_init(_R T264_t* t, _RW T264_slice_t* slice)
  158. {
  159.     slice->first_mb_in_slice = 0;
  160. slice->slice_type = t->slice_type; /* ^^^ */
  161.     slice->pic_id = 0;
  162.     slice->idr_pic_id = t->idr_pic_id;
  163.     slice->pic_order_cnt_lsb = t->poc % (1 << (t->ss.max_pic_order + 4));
  164.     slice->frame_num = t->frame_num;
  165.     slice->direct_spatial_mv_pred_flag = t->param.direct_flag;
  166.     slice->ref_pic_list_reordering_flag_l0 = 0;
  167.     slice->no_output_of_prior_pics_flag = 0;
  168.     slice->long_term_reference_flag = 0;
  169.     slice->adaptive_ref_pic_marking_mode_flag = 0;
  170.     slice->slice_qp_delta = 0;
  171. //for CABAC
  172. /* get adapative cabac model if needed */
  173. if( t->param.cabac )
  174. {
  175. t->slice.cabac_init_idc = T264_cabac_model_get( &t->cabac, t->slice_type );
  176. }
  177. }
  178. void 
  179. slice_header_write(_R T264_t* t, _RW T264_slice_t* slice)
  180. {
  181.     eg_write_ue(t->bs, slice->first_mb_in_slice);
  182.     eg_write_ue(t->bs, slice->slice_type);
  183.     eg_write_ue(t->bs, slice->pic_id);
  184.     eg_write_direct(t->bs, slice->frame_num, t->ss.log2_max_frame_num_minus4 + 4);
  185.     if (t->nal.nal_unit_type == NAL_SLICE_IDR)
  186.     {
  187.         eg_write_ue(t->bs, slice->idr_pic_id);
  188.     }
  189.     if (t->ss.pic_order_cnt_type == 0)
  190.     {
  191.         eg_write_direct(t->bs, slice->pic_order_cnt_lsb, t->ss.max_pic_order + 4);
  192.     }
  193.     if (t->slice_type == SLICE_P)
  194.     {
  195.         // num_ref_idx_active_override_flag
  196.         if (t->refl0_num == t->param.ref_num)
  197.         {
  198.             eg_write_direct1(t->bs, 0);
  199.         }
  200.         else
  201.         {
  202.             eg_write_direct1(t->bs, 1);
  203.             eg_write_ue(t->bs, t->refl0_num - 1);
  204.         }
  205.         t->ps.num_ref_idx_l0_active_minus1 = t->refl0_num - 1;
  206.         t->ps.num_ref_idx_l1_active_minus1 = 0;
  207.     }
  208.     else if (t->slice_type == SLICE_B)
  209.     {
  210.         // direct_spatial_mv_pred_flag
  211.         eg_write_direct1(t->bs, slice->direct_spatial_mv_pred_flag);
  212.         // num_ref_idx_active_override_flag
  213.         {
  214.             eg_write_direct1(t->bs, 1);
  215.             eg_write_ue(t->bs, t->refl0_num - 1);
  216.             eg_write_ue(t->bs, t->refl1_num - 1);
  217.         }
  218.         t->ps.num_ref_idx_l0_active_minus1 = t->refl0_num - 1;
  219.         t->ps.num_ref_idx_l1_active_minus1 = t->refl1_num - 1;
  220.     }
  221.     /* ref_pic_list_reordering() */
  222. /*
  223.  * ^^^ ccc
  224.  */
  225. if(slice->slice_type != SLICE_I && slice->slice_type != SLICE_SI )
  226. {
  227. eg_write_direct1(t->bs, slice->ref_pic_list_reordering_flag_l0);
  228.         if (slice->slice_type == SLICE_B)
  229.             eg_write_direct1(t->bs, slice->ref_pic_list_reordering_flag_l0);
  230. }
  231.     if (t->nal.nal_ref_idc != 0)
  232.     {
  233.         /* dec_ref_pic_marking() */
  234.         if (t->nal.nal_unit_type == NAL_SLICE_IDR)
  235.         {
  236.             eg_write_direct1(t->bs, slice->no_output_of_prior_pics_flag);
  237.             eg_write_direct1(t->bs, slice->long_term_reference_flag);
  238.         }
  239.         else
  240.         {
  241.             eg_write_direct1(t->bs, slice->adaptive_ref_pic_marking_mode_flag);
  242.         }
  243.     }
  244. //for CABAC
  245. if(t->ps.entroy_coding_mode_flag!=0 && t->slice_type!= SLICE_I)
  246. {
  247. eg_write_ue(t->bs, t->slice.cabac_init_idc);
  248. }
  249.     eg_write_se(t->bs, t->qp_y - t->param.qp);
  250.     if (t->ps.deblocking_filter_control_present_flag)
  251.     {
  252.         eg_write_ue(t->bs, t->param.disable_filter);
  253.     }
  254. }
  255. void 
  256. custom_set_init(_R T264_t* t, _RW T264_custom_set_t* set)
  257. {
  258.     set->ver = T264_VER;
  259.     set->flags = 0;
  260.     if (t->flags & USE_FASTINTERPOLATE)
  261.         set->flags |= CUSTOM_FASTINTERPOLATE;
  262. }
  263. void 
  264. custom_set_write(_R T264_t* t, _RW T264_custom_set_t* set)
  265. {
  266.     eg_write_direct(t->bs, set->ver, 32);
  267.     eg_write_direct(t->bs, set->flags, 32);
  268. }
  269. void rbsp_trailing_bits(_R T264_t* t)
  270. {
  271.     eg_write_direct1(t->bs, 1);
  272.     eg_align(t->bs);
  273. }