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

Audio

开发平台:

Visual C++

  1. /*!
  2.  *************************************************************************************
  3.  * file header.c
  4.  *
  5.  * brief
  6.  *    H.264 Slice headers
  7.  *
  8.  *************************************************************************************
  9.  */
  10. #include "global.h"
  11. #include "elements.h"
  12. #include "defines.h"
  13. #include "fmo.h"
  14. #include "vlc.h"
  15. #include "mbuffer.h"
  16. #include "header.h"
  17. #include "ctx_tables.h"
  18. extern StorablePicture *dec_picture;
  19. #if TRACE
  20. #define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
  21. #else
  22. #define SYMTRACESTRING(s) // do nothing
  23. #endif
  24. extern int UsedBits;
  25. static void ref_pic_list_reordering(void);
  26. static void pred_weight_table(void);
  27. /*!
  28.  ************************************************************************
  29.  * brief
  30.  *    calculate Ceil(Log2(uiVal))
  31.  ************************************************************************
  32.  */
  33. unsigned CeilLog2( unsigned uiVal)
  34. {
  35.   unsigned uiTmp = uiVal-1;
  36.   unsigned uiRet = 0;
  37.   while( uiTmp != 0 )
  38.   {
  39.     uiTmp >>= 1;
  40.     uiRet++;
  41.   }
  42.   return uiRet;
  43. }
  44. unsigned CeilLog2_sf( unsigned uiVal)
  45. {
  46.   unsigned uiTmp = uiVal-1;
  47.   unsigned uiRet = 0;
  48.   while( uiTmp > 0 )
  49.   {
  50.     uiTmp >>= 1;
  51.     uiRet++;
  52.   }
  53.   return uiRet;
  54. }
  55. /*!
  56.  ************************************************************************
  57.  * brief
  58.  *    read the first part of the header (only the pic_parameter_set_id)
  59.  * return
  60.  *    Length of the first part of the slice header (in bits)
  61.  ************************************************************************
  62.  */
  63. int FirstPartOfSliceHeader(void)
  64. {
  65.   Slice *currSlice = img->currentSlice;
  66.   int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
  67.   DataPartition *partition = &(currSlice->partArr[dP_nr]);
  68.   Bitstream *currStream = partition->bitstream;
  69.   int tmp;
  70.   UsedBits= partition->bitstream->frame_bitoffset; // was hardcoded to 31 for previous start-code. This is better.
  71.   // Get first_mb_in_slice
  72.   currSlice->start_mb_nr = ue_v ("SH: first_mb_in_slice", currStream);
  73.   tmp = ue_v ("SH: slice_type", currStream);
  74.   if (tmp>4) tmp -=5;
  75.   img->type = currSlice->picture_type = (SliceType) tmp;
  76.   currSlice->pic_parameter_set_id = ue_v ("SH: pic_parameter_set_id", currStream);
  77.   if( img->separate_colour_plane_flag )
  78.   {
  79.     img->colour_plane_id = u_v (2, "SH: colour_plane_id", currStream);
  80.   }
  81.   return UsedBits;
  82. }
  83. /*!
  84.  ************************************************************************
  85.  * brief
  86.  *    read the scond part of the header (without the pic_parameter_set_id
  87.  * return
  88.  *    Length of the second part of the Slice header in bits
  89.  ************************************************************************
  90.  */
  91. int RestOfSliceHeader(void)
  92. {
  93.   Slice *currSlice = img->currentSlice;
  94.   int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
  95.   DataPartition *partition = &(currSlice->partArr[dP_nr]);
  96.   Bitstream *currStream = partition->bitstream;
  97.   int val, len;
  98.   img->frame_num = u_v (active_sps->log2_max_frame_num_minus4 + 4, "SH: frame_num", currStream);
  99.   /* Tian Dong: frame_num gap processing, if found */
  100.   if (img->idr_flag)
  101.   {
  102.     img->pre_frame_num = img->frame_num;
  103.     // picture error concealment
  104.     img->last_ref_pic_poc = 0;
  105.     assert(img->frame_num == 0);
  106.   }
  107.   if (active_sps->frame_mbs_only_flag)
  108.   {
  109.     img->structure = FRAME;
  110.     img->field_pic_flag=0;
  111.   }
  112.   else
  113.   {
  114.     // field_pic_flag   u(1)
  115.     img->field_pic_flag = u_1("SH: field_pic_flag", currStream);
  116.     if (img->field_pic_flag)
  117.     {
  118.       // bottom_field_flag  u(1)
  119.       img->bottom_field_flag = u_1("SH: bottom_field_flag", currStream);
  120.       img->structure = img->bottom_field_flag ? BOTTOM_FIELD : TOP_FIELD;
  121.     }
  122.     else
  123.     {
  124.       img->structure = FRAME;
  125.       img->bottom_field_flag = FALSE;
  126.     }
  127.   }
  128.   currSlice->structure = (PictureStructure) img->structure;
  129.   img->MbaffFrameFlag=(active_sps->mb_adaptive_frame_field_flag && (img->field_pic_flag==0));
  130.   if (img->structure == FRAME       ) 
  131.     assert (img->field_pic_flag == 0);
  132.   if (img->structure == TOP_FIELD   ) 
  133.     assert (img->field_pic_flag == 1 && (img->bottom_field_flag == FALSE));
  134.   if (img->structure == BOTTOM_FIELD) 
  135.     assert (img->field_pic_flag == 1 && (img->bottom_field_flag == TRUE ));
  136.   if (img->idr_flag)
  137.   {
  138.     img->idr_pic_id = ue_v("SH: idr_pic_id", currStream);
  139.   }
  140.   if (active_sps->pic_order_cnt_type == 0)
  141.   {
  142.     img->pic_order_cnt_lsb = u_v(active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4, "SH: pic_order_cnt_lsb", currStream);
  143.     if( active_pps->pic_order_present_flag  ==  1 &&  !img->field_pic_flag )
  144.       img->delta_pic_order_cnt_bottom = se_v("SH: delta_pic_order_cnt_bottom", currStream);
  145.     else
  146.       img->delta_pic_order_cnt_bottom = 0;
  147.   }
  148.   if( active_sps->pic_order_cnt_type == 1 && !active_sps->delta_pic_order_always_zero_flag )
  149.   {
  150.     img->delta_pic_order_cnt[ 0 ] = se_v("SH: delta_pic_order_cnt[0]", currStream);
  151.     if( active_pps->pic_order_present_flag  ==  1  &&  !img->field_pic_flag )
  152.       img->delta_pic_order_cnt[ 1 ] = se_v("SH: delta_pic_order_cnt[1]", currStream);
  153.   }else
  154.   {
  155.     if (active_sps->pic_order_cnt_type == 1)
  156.     {
  157.       img->delta_pic_order_cnt[ 0 ] = 0;
  158.       img->delta_pic_order_cnt[ 1 ] = 0;
  159.     }
  160.   }
  161.   //! redundant_pic_cnt is missing here
  162.   if (active_pps->redundant_pic_cnt_present_flag)
  163.   {
  164.     img->redundant_pic_cnt = ue_v ("SH: redundant_pic_cnt", currStream);
  165.   }
  166.   if(img->type==B_SLICE)
  167.   {
  168.     img->direct_spatial_mv_pred_flag = u_1 ("SH: direct_spatial_mv_pred_flag", currStream);
  169.   }
  170.   img->num_ref_idx_l0_active = active_pps->num_ref_idx_l0_active_minus1 + 1;
  171.   img->num_ref_idx_l1_active = active_pps->num_ref_idx_l1_active_minus1 + 1;
  172.   if(img->type==P_SLICE || img->type == SP_SLICE || img->type==B_SLICE)
  173.   {
  174.     val = u_1 ("SH: num_ref_idx_override_flag", currStream);
  175.     if (val)
  176.     {
  177.       img->num_ref_idx_l0_active = 1 + ue_v ("SH: num_ref_idx_l0_active_minus1", currStream);
  178.       if(img->type==B_SLICE)
  179.       {
  180.         img->num_ref_idx_l1_active = 1 + ue_v ("SH: num_ref_idx_l1_active_minus1", currStream);
  181.       }
  182.     }
  183.   }
  184.   if (img->type!=B_SLICE)
  185.   {
  186.     img->num_ref_idx_l1_active = 0;
  187.   }
  188.   ref_pic_list_reordering();
  189.   img->apply_weights = ((active_pps->weighted_pred_flag && (currSlice->picture_type == P_SLICE || currSlice->picture_type == SP_SLICE) )
  190.           || ((active_pps->weighted_bipred_idc > 0 ) && (currSlice->picture_type == B_SLICE)));
  191.   if ((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
  192.       (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE)))
  193.   {
  194.     pred_weight_table();
  195.   }
  196.   if (img->nal_reference_idc)
  197.     dec_ref_pic_marking(currStream);
  198.   if (active_pps->entropy_coding_mode_flag && img->type!=I_SLICE && img->type!=SI_SLICE)
  199.   {
  200.     img->model_number = ue_v("SH: cabac_init_idc", currStream);
  201.   }
  202.   else
  203.   {
  204.     img->model_number = 0;
  205.   }
  206.   currSlice->slice_qp_delta = val = se_v("SH: slice_qp_delta", currStream);
  207.   currSlice->qp = img->qp = 26 + active_pps->pic_init_qp_minus26 + val;
  208.   if ((img->qp < -img->bitdepth_luma_qp_scale) || (img->qp > 51))
  209.     error ("slice_qp_delta makes slice_qp_y out of range", 500);
  210.   if(img->type==SP_SLICE || img->type == SI_SLICE)
  211.   {
  212.     if(img->type==SP_SLICE)
  213.     {
  214.       img->sp_switch = u_1 ("SH: sp_for_switch_flag", currStream);
  215.     }
  216.     currSlice->slice_qs_delta = val = se_v("SH: slice_qs_delta", currStream);
  217.     currSlice->qs = img->qpsp = 26 + active_pps->pic_init_qs_minus26 + val;    
  218.     if ((img->qpsp < 0) || (img->qpsp > 51))
  219.       error ("slice_qs_delta makes slice_qs_y out of range", 500);
  220.   }
  221.   if ( !HI_INTRA_ONLY_PROFILE || (HI_INTRA_ONLY_PROFILE && (params->intra_profile_deblocking == 1) ))
  222.   //then read flags and parameters from bistream
  223.   {
  224.     if (active_pps->deblocking_filter_control_present_flag)
  225.     {
  226.       currSlice->DFDisableIdc = ue_v ("SH: disable_deblocking_filter_idc", currStream);
  227.       if (currSlice->DFDisableIdc!=1)
  228.       {
  229.         currSlice->DFAlphaC0Offset = 2 * se_v("SH: slice_alpha_c0_offset_div2", currStream);
  230.         currSlice->DFBetaOffset = 2 * se_v("SH: slice_beta_offset_div2", currStream);
  231.       }
  232.       else
  233.       {
  234.         currSlice->DFAlphaC0Offset = currSlice->DFBetaOffset = 0;
  235.       }
  236.     }
  237.     else
  238.     {
  239.       currSlice->DFDisableIdc = currSlice->DFAlphaC0Offset = currSlice->DFBetaOffset = 0;
  240.     }
  241.   }
  242.   else //By default the Loop Filter is Off
  243.   { //444_TEMP_NOTE: change made below. 08/07/07
  244.     //still need to parse the SEs (read flags and parameters from bistream) but will ignore
  245.     if (active_pps->deblocking_filter_control_present_flag)
  246.     {
  247.       currSlice->DFDisableIdc = ue_v ("SH: disable_deblocking_filter_idc", currStream);
  248.       if (currSlice->DFDisableIdc!=1)
  249.       {
  250.         currSlice->DFAlphaC0Offset = 2 * se_v("SH: slice_alpha_c0_offset_div2", currStream);
  251.         currSlice->DFBetaOffset = 2 * se_v("SH: slice_beta_offset_div2", currStream);
  252.       }
  253.     }//444_TEMP_NOTE. the end of change. 08/07/07
  254.     //Ignore the SEs, by default the Loop Filter is Off
  255.     currSlice->DFDisableIdc =1;
  256.     currSlice->DFAlphaC0Offset = currSlice->DFBetaOffset = 0;
  257.   }
  258.   if (active_pps->num_slice_groups_minus1>0 && active_pps->slice_group_map_type>=3 &&
  259.       active_pps->slice_group_map_type<=5)
  260.   {
  261.     len = (active_sps->pic_height_in_map_units_minus1+1)*(active_sps->pic_width_in_mbs_minus1+1)/
  262.           (active_pps->slice_group_change_rate_minus1+1);
  263.     if (((active_sps->pic_height_in_map_units_minus1+1)*(active_sps->pic_width_in_mbs_minus1+1))%
  264.           (active_pps->slice_group_change_rate_minus1+1))
  265.           len +=1;
  266.     len = CeilLog2(len+1);
  267.     img->slice_group_change_cycle = u_v (len, "SH: slice_group_change_cycle", currStream);
  268.   }
  269.   img->PicHeightInMbs = img->FrameHeightInMbs / ( 1 + img->field_pic_flag );
  270.   img->PicSizeInMbs   = img->PicWidthInMbs * img->PicHeightInMbs;
  271.   img->FrameSizeInMbs = img->PicWidthInMbs * img->FrameHeightInMbs;
  272.   return UsedBits;
  273. }
  274. /*!
  275.  ************************************************************************
  276.  * brief
  277.  *    read the reference picture reordering information
  278.  ************************************************************************
  279.  */
  280. static void ref_pic_list_reordering(void)
  281. {
  282.   Slice *currSlice = img->currentSlice;
  283.   int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
  284.   DataPartition *partition = &(currSlice->partArr[dP_nr]);
  285.   Bitstream *currStream = partition->bitstream;
  286.   int i, val;
  287.   alloc_ref_pic_list_reordering_buffer(currSlice);
  288.   if (img->type!=I_SLICE && img->type!=SI_SLICE)
  289.   {
  290.     val = currSlice->ref_pic_list_reordering_flag_l0 = u_1 ("SH: ref_pic_list_reordering_flag_l0", currStream);
  291.     if (val)
  292.     {
  293.       i=0;
  294.       do
  295.       {
  296.         val = currSlice->reordering_of_pic_nums_idc_l0[i] = ue_v("SH: reordering_of_pic_nums_idc_l0", currStream);
  297.         if (val==0 || val==1)
  298.         {
  299.           currSlice->abs_diff_pic_num_minus1_l0[i] = ue_v("SH: abs_diff_pic_num_minus1_l0", currStream);
  300.         }
  301.         else
  302.         {
  303.           if (val==2)
  304.           {
  305.             currSlice->long_term_pic_idx_l0[i] = ue_v("SH: long_term_pic_idx_l0", currStream);
  306.           }
  307.         }
  308.         i++;
  309.         // assert (i>img->num_ref_idx_l0_active);
  310.       } while (val != 3);
  311.     }
  312.   }
  313.   if (img->type==B_SLICE)
  314.   {
  315.     val = currSlice->ref_pic_list_reordering_flag_l1 = u_1 ("SH: ref_pic_list_reordering_flag_l1", currStream);
  316.     if (val)
  317.     {
  318.       i=0;
  319.       do
  320.       {
  321.         val = currSlice->reordering_of_pic_nums_idc_l1[i] = ue_v("SH: reordering_of_pic_nums_idc_l1", currStream);
  322.         if (val==0 || val==1)
  323.         {
  324.           currSlice->abs_diff_pic_num_minus1_l1[i] = ue_v("SH: abs_diff_pic_num_minus1_l1", currStream);
  325.         }
  326.         else
  327.         {
  328.           if (val==2)
  329.           {
  330.             currSlice->long_term_pic_idx_l1[i] = ue_v("SH: long_term_pic_idx_l1", currStream);
  331.           }
  332.         }
  333.         i++;
  334.         // assert (i>img->num_ref_idx_l1_active);
  335.       } while (val != 3);
  336.     }
  337.   }
  338.   // set reference index of redundant slices.
  339.   if(img->redundant_pic_cnt)
  340.   {
  341.     redundant_slice_ref_idx = currSlice->abs_diff_pic_num_minus1_l0[0] + 1;
  342.   }
  343. }
  344. /*!
  345.  ************************************************************************
  346.  * brief
  347.  *    read the weighted prediction tables
  348.  ************************************************************************
  349.  */
  350. static void pred_weight_table(void)
  351. {
  352.   Slice *currSlice = img->currentSlice;
  353.   int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
  354.   DataPartition *partition = &(currSlice->partArr[dP_nr]);
  355.   Bitstream *currStream = partition->bitstream;
  356.   int luma_weight_flag_l0, luma_weight_flag_l1, chroma_weight_flag_l0, chroma_weight_flag_l1;
  357.   int i,j;
  358.   img->luma_log2_weight_denom = ue_v ("SH: luma_log2_weight_denom", currStream);
  359.   img->wp_round_luma = img->luma_log2_weight_denom ? 1<<(img->luma_log2_weight_denom - 1): 0;
  360.   if ( 0 != active_sps->chroma_format_idc)
  361.   {
  362.     img->chroma_log2_weight_denom = ue_v ("SH: chroma_log2_weight_denom", currStream);
  363.     img->wp_round_chroma = img->chroma_log2_weight_denom ? 1<<(img->chroma_log2_weight_denom - 1): 0;
  364.   }
  365.   reset_wp_params(img);
  366.   for (i=0; i<img->num_ref_idx_l0_active; i++)
  367.   {
  368.     luma_weight_flag_l0 = u_1("SH: luma_weight_flag_l0", currStream);
  369.     if (luma_weight_flag_l0)
  370.     {
  371.       img->wp_weight[0][i][0] = se_v ("SH: luma_weight_l0", currStream);
  372.       img->wp_offset[0][i][0] = se_v ("SH: luma_offset_l0", currStream);
  373.       img->wp_offset[0][i][0] = img->wp_offset[0][i][0]<<(img->bitdepth_luma-8);
  374.     }
  375.     else
  376.     {
  377.       img->wp_weight[0][i][0] = 1<<img->luma_log2_weight_denom;
  378.       img->wp_offset[0][i][0] = 0;
  379.     }
  380.     if (active_sps->chroma_format_idc != 0)
  381.     {
  382.       chroma_weight_flag_l0 = u_1 ("SH: chroma_weight_flag_l0", currStream);
  383.       for (j=1; j<3; j++)
  384.       {
  385.         if (chroma_weight_flag_l0)
  386.         {
  387.           img->wp_weight[0][i][j] = se_v("SH: chroma_weight_l0", currStream);
  388.           img->wp_offset[0][i][j] = se_v("SH: chroma_offset_l0", currStream);
  389.           img->wp_offset[0][i][j] = img->wp_offset[0][i][j]<<(img->bitdepth_chroma-8);
  390.         }
  391.         else
  392.         {
  393.           img->wp_weight[0][i][j] = 1<<img->chroma_log2_weight_denom;
  394.           img->wp_offset[0][i][j] = 0;
  395.         }
  396.       }
  397.     }
  398.   }
  399.   if ((img->type == B_SLICE) && active_pps->weighted_bipred_idc == 1)
  400.   {
  401.     for (i=0; i<img->num_ref_idx_l1_active; i++)
  402.     {
  403.       luma_weight_flag_l1 = u_1("SH: luma_weight_flag_l1", currStream);
  404.       if (luma_weight_flag_l1)
  405.       {
  406.         img->wp_weight[1][i][0] = se_v ("SH: luma_weight_l1", currStream);
  407.         img->wp_offset[1][i][0] = se_v ("SH: luma_offset_l1", currStream);
  408.         img->wp_offset[1][i][0] = img->wp_offset[1][i][0]<<(img->bitdepth_luma-8);
  409.       }
  410.       else
  411.       {
  412.         img->wp_weight[1][i][0] = 1<<img->luma_log2_weight_denom;
  413.         img->wp_offset[1][i][0] = 0;
  414.       }
  415.       if (active_sps->chroma_format_idc != 0)
  416.       {
  417.         chroma_weight_flag_l1 = u_1 ("SH: chroma_weight_flag_l1", currStream);
  418.         for (j=1; j<3; j++)
  419.         {
  420.           if (chroma_weight_flag_l1)
  421.           {
  422.             img->wp_weight[1][i][j] = se_v("SH: chroma_weight_l1", currStream);
  423.             img->wp_offset[1][i][j] = se_v("SH: chroma_offset_l1", currStream);
  424.             img->wp_offset[1][i][j] = img->wp_offset[1][i][j]<<(img->bitdepth_chroma-8);
  425.           }
  426.           else
  427.           {
  428.             img->wp_weight[1][i][j] = 1<<img->chroma_log2_weight_denom;
  429.             img->wp_offset[1][i][j] = 0;
  430.           }
  431.         }
  432.       }
  433.     }
  434.   }
  435. }
  436. /*!
  437.  ************************************************************************
  438.  * brief
  439.  *    read the memory control operations
  440.  ************************************************************************
  441.  */
  442. void dec_ref_pic_marking(Bitstream *currStream)
  443. {
  444.   int val;
  445.   DecRefPicMarking_t *tmp_drpm,*tmp_drpm2;
  446.   // free old buffer content
  447.   while (img->dec_ref_pic_marking_buffer)
  448.   {
  449.     tmp_drpm=img->dec_ref_pic_marking_buffer;
  450.     img->dec_ref_pic_marking_buffer=tmp_drpm->Next;
  451.     free (tmp_drpm);
  452.   }
  453.   if (img->idr_flag)
  454.   {
  455.     img->no_output_of_prior_pics_flag = u_1("SH: no_output_of_prior_pics_flag", currStream);
  456.     img->long_term_reference_flag = u_1("SH: long_term_reference_flag", currStream);
  457.   }
  458.   else
  459.   {
  460.     img->adaptive_ref_pic_buffering_flag = u_1("SH: adaptive_ref_pic_buffering_flag", currStream);
  461.     if (img->adaptive_ref_pic_buffering_flag)
  462.     {
  463.       // read Memory Management Control Operation
  464.       do
  465.       {
  466.         tmp_drpm=(DecRefPicMarking_t*)calloc (1,sizeof (DecRefPicMarking_t));
  467.         tmp_drpm->Next=NULL;
  468.         val = tmp_drpm->memory_management_control_operation = ue_v("SH: memory_management_control_operation", currStream);
  469.         if ((val==1)||(val==3))
  470.         {
  471.           tmp_drpm->difference_of_pic_nums_minus1 = ue_v("SH: difference_of_pic_nums_minus1", currStream);
  472.         }
  473.         if (val==2)
  474.         {
  475.           tmp_drpm->long_term_pic_num = ue_v("SH: long_term_pic_num", currStream);
  476.         }
  477.         if ((val==3)||(val==6))
  478.         {
  479.           tmp_drpm->long_term_frame_idx = ue_v("SH: long_term_frame_idx", currStream);
  480.         }
  481.         if (val==4)
  482.         {
  483.           tmp_drpm->max_long_term_frame_idx_plus1 = ue_v("SH: max_long_term_pic_idx_plus1", currStream);
  484.         }
  485.         // add command
  486.         if (img->dec_ref_pic_marking_buffer==NULL)
  487.         {
  488.           img->dec_ref_pic_marking_buffer=tmp_drpm;
  489.         }
  490.         else
  491.         {
  492.           tmp_drpm2=img->dec_ref_pic_marking_buffer;
  493.           while (tmp_drpm2->Next!=NULL) tmp_drpm2=tmp_drpm2->Next;
  494.           tmp_drpm2->Next=tmp_drpm;
  495.         }
  496.       }
  497.       while (val != 0);
  498.     }
  499.   }
  500. }
  501. /*!
  502.  ************************************************************************
  503.  * brief
  504.  *    To calculate the poc values
  505.  *        based upon JVT-F100d2
  506.  *  POC200301: Until Jan 2003, this function will calculate the correct POC
  507.  *    values, but the management of POCs in buffered pictures may need more work.
  508.  * return
  509.  *    none
  510.  ************************************************************************
  511.  */
  512. void decode_poc(ImageParameters *img)
  513. {
  514.   int i;
  515.   // for POC mode 0:
  516.   unsigned int MaxPicOrderCntLsb = (1<<(active_sps->log2_max_pic_order_cnt_lsb_minus4+4));
  517.   switch ( active_sps->pic_order_cnt_type )
  518.   {
  519.   case 0: // POC MODE 0
  520.     // 1st
  521.     if(img->idr_flag)
  522.     {
  523.       img->PrevPicOrderCntMsb = 0;
  524.       img->PrevPicOrderCntLsb = 0;
  525.     }
  526.     else
  527.     {
  528.       if (img->last_has_mmco_5)
  529.       {
  530.         if (img->last_pic_bottom_field)
  531.         {
  532.           img->PrevPicOrderCntMsb = 0;
  533.           img->PrevPicOrderCntLsb = 0;
  534.         }
  535.         else
  536.         {
  537.           img->PrevPicOrderCntMsb = 0;
  538.           img->PrevPicOrderCntLsb = img->toppoc;
  539.         }
  540.       }
  541.     }
  542.     // Calculate the MSBs of current picture
  543.     if( img->pic_order_cnt_lsb  <  img->PrevPicOrderCntLsb  &&
  544.       ( img->PrevPicOrderCntLsb - img->pic_order_cnt_lsb )  >=  ( MaxPicOrderCntLsb / 2 ) )
  545.       img->PicOrderCntMsb = img->PrevPicOrderCntMsb + MaxPicOrderCntLsb;
  546.     else if ( img->pic_order_cnt_lsb  >  img->PrevPicOrderCntLsb  &&
  547.       ( img->pic_order_cnt_lsb - img->PrevPicOrderCntLsb )  >  ( MaxPicOrderCntLsb / 2 ) )
  548.       img->PicOrderCntMsb = img->PrevPicOrderCntMsb - MaxPicOrderCntLsb;
  549.     else
  550.       img->PicOrderCntMsb = img->PrevPicOrderCntMsb;
  551.     // 2nd
  552.     if(img->field_pic_flag==0)
  553.     {           //frame pix
  554.       img->toppoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
  555.       img->bottompoc = img->toppoc + img->delta_pic_order_cnt_bottom;
  556.       img->ThisPOC = img->framepoc = (img->toppoc < img->bottompoc)? img->toppoc : img->bottompoc; // POC200301
  557.     }
  558.     else if (img->bottom_field_flag == FALSE)
  559.     {  //top field
  560.       img->ThisPOC= img->toppoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
  561.     }
  562.     else
  563.     {  //bottom field
  564.       img->ThisPOC= img->bottompoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
  565.     }
  566.     img->framepoc=img->ThisPOC;
  567.     if ( img->frame_num!=img->PreviousFrameNum)
  568.       img->PreviousFrameNum=img->frame_num;
  569.     if(img->nal_reference_idc)
  570.     {
  571.       img->PrevPicOrderCntLsb = img->pic_order_cnt_lsb;
  572.       img->PrevPicOrderCntMsb = img->PicOrderCntMsb;
  573.     }
  574.     break;
  575.   case 1: // POC MODE 1
  576.     // 1st
  577.     if(img->idr_flag)
  578.     {
  579.       img->FrameNumOffset=0;     //  first pix of IDRGOP,
  580.       img->delta_pic_order_cnt[0]=0;                        //ignore first delta
  581.       if(img->frame_num)
  582.         error("frame_num not equal to zero in IDR picture", -1020);
  583.     }
  584.     else
  585.     {
  586.       if (img->last_has_mmco_5)
  587.       {
  588.         img->PreviousFrameNumOffset = 0;
  589.         img->PreviousFrameNum = 0;
  590.       }
  591.       if (img->frame_num<img->PreviousFrameNum)
  592.       {             //not first pix of IDRGOP
  593.         img->FrameNumOffset = img->PreviousFrameNumOffset + img->MaxFrameNum;
  594.       }
  595.       else
  596.       {
  597.         img->FrameNumOffset = img->PreviousFrameNumOffset;
  598.       }
  599.     }
  600.     // 2nd
  601.     if(active_sps->num_ref_frames_in_pic_order_cnt_cycle)
  602.       img->AbsFrameNum = img->FrameNumOffset+img->frame_num;
  603.     else
  604.       img->AbsFrameNum=0;
  605.     if( (!img->nal_reference_idc) && img->AbsFrameNum > 0)
  606.       img->AbsFrameNum--;
  607.     // 3rd
  608.     img->ExpectedDeltaPerPicOrderCntCycle=0;
  609.     if(active_sps->num_ref_frames_in_pic_order_cnt_cycle)
  610.     for(i=0;i<(int) active_sps->num_ref_frames_in_pic_order_cnt_cycle;i++)
  611.       img->ExpectedDeltaPerPicOrderCntCycle += active_sps->offset_for_ref_frame[i];
  612.     if(img->AbsFrameNum)
  613.     {
  614.       img->PicOrderCntCycleCnt = (img->AbsFrameNum-1)/active_sps->num_ref_frames_in_pic_order_cnt_cycle;
  615.       img->FrameNumInPicOrderCntCycle = (img->AbsFrameNum-1)%active_sps->num_ref_frames_in_pic_order_cnt_cycle;
  616.       img->ExpectedPicOrderCnt = img->PicOrderCntCycleCnt*img->ExpectedDeltaPerPicOrderCntCycle;
  617.       for(i=0;i<=(int)img->FrameNumInPicOrderCntCycle;i++)
  618.         img->ExpectedPicOrderCnt += active_sps->offset_for_ref_frame[i];
  619.     }
  620.     else
  621.       img->ExpectedPicOrderCnt=0;
  622.     if(!img->nal_reference_idc)
  623.       img->ExpectedPicOrderCnt += active_sps->offset_for_non_ref_pic;
  624.     if(img->field_pic_flag==0)
  625.     {           //frame pix
  626.       img->toppoc = img->ExpectedPicOrderCnt + img->delta_pic_order_cnt[0];
  627.       img->bottompoc = img->toppoc + active_sps->offset_for_top_to_bottom_field + img->delta_pic_order_cnt[1];
  628.       img->ThisPOC = img->framepoc = (img->toppoc < img->bottompoc)? img->toppoc : img->bottompoc; // POC200301
  629.     }
  630.     else if (img->bottom_field_flag == FALSE)
  631.     {  //top field
  632.       img->ThisPOC = img->toppoc = img->ExpectedPicOrderCnt + img->delta_pic_order_cnt[0];
  633.     }
  634.     else
  635.     {  //bottom field
  636.       img->ThisPOC = img->bottompoc = img->ExpectedPicOrderCnt + active_sps->offset_for_top_to_bottom_field + img->delta_pic_order_cnt[0];
  637.     }
  638.     img->framepoc=img->ThisPOC;
  639.     img->PreviousFrameNum=img->frame_num;
  640.     img->PreviousFrameNumOffset=img->FrameNumOffset;
  641.     break;
  642.   case 2: // POC MODE 2
  643.     if(img->idr_flag) // IDR picture
  644.     {
  645.       img->FrameNumOffset=0;     //  first pix of IDRGOP,
  646.       img->ThisPOC = img->framepoc = img->toppoc = img->bottompoc = 0;
  647.       if(img->frame_num)
  648.         error("frame_num not equal to zero in IDR picture", -1020);
  649.     }
  650.     else
  651.     {
  652.       if (img->last_has_mmco_5)
  653.       {
  654.         img->PreviousFrameNum = 0;
  655.         img->PreviousFrameNumOffset = 0;
  656.       }
  657.       if (img->frame_num<img->PreviousFrameNum)
  658.         img->FrameNumOffset = img->PreviousFrameNumOffset + img->MaxFrameNum;
  659.       else
  660.         img->FrameNumOffset = img->PreviousFrameNumOffset;
  661.       img->AbsFrameNum = img->FrameNumOffset+img->frame_num;
  662.       if(!img->nal_reference_idc)
  663.         img->ThisPOC = (2*img->AbsFrameNum - 1);
  664.       else
  665.         img->ThisPOC = (2*img->AbsFrameNum);
  666.       if (img->field_pic_flag==0)
  667.         img->toppoc = img->bottompoc = img->framepoc = img->ThisPOC;
  668.       else if (img->bottom_field_flag == FALSE)
  669.          img->toppoc = img->framepoc = img->ThisPOC;
  670.       else img->bottompoc = img->framepoc = img->ThisPOC;
  671.     }
  672.     img->PreviousFrameNum=img->frame_num;
  673.     img->PreviousFrameNumOffset=img->FrameNumOffset;
  674.     break;
  675.   default:
  676.     //error must occurs
  677.     assert( 1==0 );
  678.     break;
  679.   }
  680. }
  681. /*!
  682.  ************************************************************************
  683.  * brief
  684.  *    A little helper for the debugging of POC code
  685.  * return
  686.  *    none
  687.  ************************************************************************
  688.  */
  689. int dumppoc(ImageParameters *img) 
  690. {
  691.     printf ("nPOC locals...n");
  692.     printf ("toppoc                                %dn", (int) img->toppoc);
  693.     printf ("bottompoc                             %dn", (int) img->bottompoc);
  694.     printf ("frame_num                             %dn", (int) img->frame_num);
  695.     printf ("field_pic_flag                        %dn", (int) img->field_pic_flag);
  696.     printf ("bottom_field_flag                     %dn", (int) img->bottom_field_flag);
  697.     printf ("POC SPSn");
  698.     printf ("log2_max_frame_num_minus4             %dn", (int) active_sps->log2_max_frame_num_minus4);         // POC200301
  699.     printf ("log2_max_pic_order_cnt_lsb_minus4     %dn", (int) active_sps->log2_max_pic_order_cnt_lsb_minus4);
  700.     printf ("pic_order_cnt_type                    %dn", (int) active_sps->pic_order_cnt_type);
  701.     printf ("num_ref_frames_in_pic_order_cnt_cycle %dn", (int) active_sps->num_ref_frames_in_pic_order_cnt_cycle);
  702.     printf ("delta_pic_order_always_zero_flag      %dn", (int) active_sps->delta_pic_order_always_zero_flag);
  703.     printf ("offset_for_non_ref_pic                %dn", (int) active_sps->offset_for_non_ref_pic);
  704.     printf ("offset_for_top_to_bottom_field        %dn", (int) active_sps->offset_for_top_to_bottom_field);
  705.     printf ("offset_for_ref_frame[0]               %dn", (int) active_sps->offset_for_ref_frame[0]);
  706.     printf ("offset_for_ref_frame[1]               %dn", (int) active_sps->offset_for_ref_frame[1]);
  707.     printf ("POC in SLice Headern");
  708.     printf ("pic_order_present_flag                %dn", (int) active_pps->pic_order_present_flag);
  709.     printf ("delta_pic_order_cnt[0]                %dn", (int) img->delta_pic_order_cnt[0]);
  710.     printf ("delta_pic_order_cnt[1]                %dn", (int) img->delta_pic_order_cnt[1]);
  711.     printf ("delta_pic_order_cnt[2]                %dn", (int) img->delta_pic_order_cnt[2]);
  712.     printf ("idr_flag                              %dn", (int) img->idr_flag);
  713.     printf ("MaxFrameNum                           %dn", (int) img->MaxFrameNum);
  714.     return 0;
  715. }
  716. /*!
  717.  ************************************************************************
  718.  * brief
  719.  *    return the poc of img as per (8-1) JVT-F100d2
  720.  *  POC200301
  721.  ************************************************************************
  722.  */
  723. int picture_order(ImageParameters *img)
  724. {
  725.   if (img->field_pic_flag==0) // is a frame
  726.     return img->framepoc;
  727.   else if (img->bottom_field_flag == FALSE) // top field
  728.     return img->toppoc;
  729.   else // bottom field
  730.     return img->bottompoc;
  731. }