Example #1
0
/*!
************************************************************************
* \brief
*    Encode Enhancement Layer.
************************************************************************
*/
void encode_enhancement_layer()
{
  int previous_ref_idc = 1;

  if ((input->successive_Bframe != 0) && (IMG_NUMBER > 0)) // B-frame(s) to encode
  {
    if (input->PReplaceBSlice)
      img->type = P_SLICE;            // set image type to P-frame
    else
      img->type = B_SLICE;            // set image type to B-frame

    if (input->NumFramesInELSubSeq == 0)
      img->layer = 0;
    else
      img->layer = 1;

    if (input->BRefPictures != 1 && input->HierarchicalCoding==0)
    {
      img->frame_num++; //increment frame_num once for B-frames
      img->frame_num %= (1 << (log2_max_frame_num_minus4 + 4));
    }
    img->nal_reference_idc = 0;

    //if (input->HierarchicalCoding == 3 || input->HierarchicalCoding == 1)
    if (input->HierarchicalCoding)
    {
      for(img->b_frame_to_code=1; img->b_frame_to_code<=input->successive_Bframe; img->b_frame_to_code++)
      {

        img->nal_reference_idc = 0;

        img->type = gop_structure[img->b_frame_to_code - 1].slice_type;

        if (previous_ref_idc == 1)
        {
          img->frame_num++;                 //increment frame_num for each stored B slice
          img->frame_num %= (1 << (log2_max_frame_num_minus4 + 4));
        }

        if (gop_structure[img->b_frame_to_code - 1].reference_idc== NALU_PRIORITY_HIGH )
        {
          img->nal_reference_idc = 1;
          previous_ref_idc = 1;
        }
        else
          previous_ref_idc = 0;

        img->b_interval =
          ((double) (input->jumpd + 1) / (input->successive_Bframe + 1.0) );

        if (input->HierarchicalCoding == 3)
          img->b_interval = 1.0;

        if(input->intra_period && input->idr_enable)
          img->toppoc = 2*(((IMG_NUMBER%input->intra_period)-1)*(input->jumpd+1) + (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code - 1].display_no)));
        else
          img->toppoc = 2*((IMG_NUMBER-1)*(input->jumpd + 1) + (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code -1].display_no)));

        if (img->b_frame_to_code == 1)
          img->delta_pic_order_cnt[0] = img->toppoc - 2*(start_tr_in_this_IGOP  + (IMG_NUMBER)*((input->jumpd+1)));
        else
          img->delta_pic_order_cnt[0] = img->toppoc - 2*(start_tr_in_this_IGOP  + (IMG_NUMBER-1)*((input->jumpd+1)) + (int) (2.0 *img->b_interval * (double) (1+ gop_structure[img->b_frame_to_code - 2].display_no)));

        if ((input->PicInterlace==FRAME_CODING)&&(input->MbInterlace==FRAME_CODING))
          img->bottompoc = img->toppoc;     //progressive
        else
          img->bottompoc = img->toppoc+1;

        img->framepoc = imin (img->toppoc, img->bottompoc);

        img->delta_pic_order_cnt[1]= 0;   // POC200301

        encode_one_frame();  // encode one B-frame
        if (input->ReportFrameStats)
          report_frame_statistic();

        if (gop_structure[img->b_frame_to_code - 1].reference_idc== NALU_PRIORITY_HIGH && img->b_frame_to_code==input->successive_Bframe)
        {
          img->frame_num++;                 //increment frame_num for each stored B slice
          img->frame_num %= (1 << (log2_max_frame_num_minus4 + 4));
        }
      }
      img->b_frame_to_code = 0;
    }
    else
    {
      for(img->b_frame_to_code=1; img->b_frame_to_code<=input->successive_Bframe; img->b_frame_to_code++)
      {

        img->nal_reference_idc = 0;
        if (input->BRefPictures == 1 )
        {
          img->nal_reference_idc = 1;
          img->frame_num++;                 //increment frame_num once for B-frames
          img->frame_num %= (1 << (log2_max_frame_num_minus4 + 4));
        }

        img->b_interval =
          ((double) (input->jumpd + 1) / (input->successive_Bframe + 1.0) );

        if (input->HierarchicalCoding == 3)
          img->b_interval = 1.0;

        if(input->intra_period && input->idr_enable)
          img->toppoc = 2*(((IMG_NUMBER% input->intra_period)-1)*(input->jumpd+1) + (int) (img->b_interval * (double)img->b_frame_to_code));
        else
          img->toppoc = 2*((IMG_NUMBER-1)*(input->jumpd+1) + (int) (img->b_interval * (double)img->b_frame_to_code));

        if ((input->PicInterlace==FRAME_CODING)&&(input->MbInterlace==FRAME_CODING))
          img->bottompoc = img->toppoc;     //progressive
        else
          img->bottompoc = img->toppoc+1;

        img->framepoc = imin (img->toppoc, img->bottompoc);

        //the following is sent in the slice header
        if (input->BRefPictures != 1)
        {
          img->delta_pic_order_cnt[0]= 2*(img->b_frame_to_code-1);
        }
        else
        {
          img->delta_pic_order_cnt[0]= -2;
        }

        img->delta_pic_order_cnt[1]= 0;   // POC200301

        encode_one_frame();  // encode one B-frame

        if (input->BRefPictures == 1 && img->b_frame_to_code==input->successive_Bframe)
        {
          img->frame_num++;                 //increment frame_num for each stored B slice
          img->frame_num %= (1 << (log2_max_frame_num_minus4 + 4));
        }

        if (input->ReportFrameStats)
          report_frame_statistic();
      }
    }
  }
  img->b_frame_to_code = 0;
}
/*!
************************************************************************
* \brief
*    Encode Enhancement Layer.
************************************************************************
*/
void encode_enhancement_layer()
{
  //int previous_ref_idc = 1;

  if ((params->successive_Bframe != 0) && (IMG_NUMBER > 0) && (params->EnableIDRGOP == 0 || img->idr_gop_number)) // B-frame(s) to encode
  {
    set_slice_type( ( params->PReplaceBSlice ) ? P_SLICE : B_SLICE); // set slice type
    img->layer = (params->NumFramesInELSubSeq == 0) ? 0 : 1;
    img->nal_reference_idc = NALU_PRIORITY_DISPOSABLE;

    if (params->HierarchicalCoding)
    {
      for(img->b_frame_to_code = 1; img->b_frame_to_code <= params->successive_Bframe; img->b_frame_to_code++)
      {
        img->nal_reference_idc = NALU_PRIORITY_DISPOSABLE;
        set_slice_type( gop_structure[img->b_frame_to_code - 1].slice_type);

        if (img->last_ref_idc == 1)
        {
          img->frame_num++;                 //increment frame_num for each stored B slice
          img->frame_num %= max_frame_num;
        }
        
        img->nal_reference_idc = gop_structure[img->b_frame_to_code - 1].reference_idc;
        img->b_interval = ((double) (params->jumpd + 1) / (params->successive_Bframe + 1.0) );

        if (params->HierarchicalCoding == 3)
          img->b_interval = 1.0;

        if (IMG_NUMBER && (IMG_NUMBER <= params->intra_delay))
        {
          if(params->idr_period && ((!params->adaptive_idr_period && ( img->frm_number - img->lastIDRnumber ) % params->idr_period == 0)
            || (params->adaptive_idr_period == 1 && ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period == 0)) )
            img->toppoc = (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code - 1].display_no));
          else
            img->toppoc = 2*((-img->frm_number + img->lastIDRnumber)*(params->jumpd + 1) 
            + (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code -1].display_no)));

          if (img->b_frame_to_code == 1)
            img->delta_pic_order_cnt[0] = img->toppoc - 2*(start_tr_in_this_IGOP  + (params->intra_delay - IMG_NUMBER)*((params->jumpd + 1)));
          else
            img->delta_pic_order_cnt[0] = img->toppoc - 2*(start_tr_in_this_IGOP  + (params->intra_delay - IMG_NUMBER)*((params->jumpd + 1)) 
            + (int) (2.0 *img->b_interval * (double) (1 + gop_structure[img->b_frame_to_code - 2].display_no)));
        }
        else
        {
          if(params->idr_period && !params->adaptive_idr_period)
            img->toppoc = 2*((( ( img->frm_number - img->lastIDRnumber - params->intra_delay) % params->idr_period ) - 1)*(params->jumpd + 1) 
            + (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code - 1].display_no)));
          else if(params->idr_period && params->adaptive_idr_period == 1)
            img->toppoc = 2*((( img->frm_number - img->lastIDRnumber - params->intra_delay ) - 1)*(params->jumpd + 1) 
            + (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code - 1].display_no)));
          else
            img->toppoc = 2*((img->frm_number - img->lastIDRnumber - 1 - params->intra_delay)*(params->jumpd + 1) 
            + (int)(img->b_interval * (double)(1 + gop_structure[img->b_frame_to_code - 1].display_no)));

          if (img->b_frame_to_code == 1)
            img->delta_pic_order_cnt[0] = img->toppoc - 2*(start_tr_in_this_IGOP  + (img->frm_number - img->lastIDRnumber)*((params->jumpd + 1)));
          else
            img->delta_pic_order_cnt[0] = img->toppoc - 2*(start_tr_in_this_IGOP  + (img->frm_number - img->lastIDRnumber - 1)*((params->jumpd + 1)) 
            + (int) (2.0 *img->b_interval * (double) (1+ gop_structure[img->b_frame_to_code - 2].display_no)));
        }

        img->bottompoc = ((params->PicInterlace==FRAME_CODING)&&(params->MbInterlace==FRAME_CODING)) ? img->toppoc : img->toppoc + 1;
        img->framepoc = imin (img->toppoc, img->bottompoc);

        img->delta_pic_order_cnt[1]= 0;   
        FrameNumberInFile = CalculateFrameNumber();

        encode_one_frame();  // encode one frame
        
        img->last_ref_idc = img->nal_reference_idc ? 1 : 0;

        if (params->ReportFrameStats)
          report_frame_statistic();
      }
      img->b_frame_to_code = 0;
    }
    else
    {
      for(img->b_frame_to_code = 1; img->b_frame_to_code <= params->successive_Bframe; img->b_frame_to_code++)
      {
        img->nal_reference_idc = (params->BRefPictures == 1 ) ? NALU_PRIORITY_LOW : NALU_PRIORITY_DISPOSABLE;
        
        if (img->last_ref_idc)
        {
          img->frame_num++;                 //increment frame_num once for B-frames
          img->frame_num %= max_frame_num;
        }

        img->b_interval =
          ((double) (params->jumpd + 1) / (params->successive_Bframe + 1.0) );

        if (params->HierarchicalCoding == 3)
          img->b_interval = 1.0;

        if (IMG_NUMBER && (IMG_NUMBER <= params->intra_delay))
        { 
          if(params->idr_period && ((!params->adaptive_idr_period && ( img->frm_number - img->lastIDRnumber ) % params->idr_period == 0)
            || (params->adaptive_idr_period == 1 && ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period == 0)) )
            img->toppoc = (int) (img->b_interval * (double)img->b_frame_to_code);
          else
            img->toppoc = 2*((-img->frm_number + img->lastIDRnumber)*(params->jumpd + 1) 
            + (int) (img->b_interval * (double)img->b_frame_to_code));
        }
        else
        {
          if(params->idr_period && !params->adaptive_idr_period)
            img->toppoc = 2*((( ( img->frm_number - img->lastIDRnumber - params->intra_delay) % params->idr_period )-1)*(params->jumpd+1) + (int) (img->b_interval * (double)img->b_frame_to_code));
          else if(params->idr_period && params->adaptive_idr_period == 1)
            img->toppoc = 2*((( img->frm_number - img->lastIDRnumber - params->intra_delay )-1)*(params->jumpd+1) + (int) (img->b_interval * (double)img->b_frame_to_code));
          else
            img->toppoc = 2*((img->frm_number - img->lastIDRnumber - 1 - params->intra_delay)*(params->jumpd+1) + (int) (img->b_interval * (double)img->b_frame_to_code));
        }

        if ((params->PicInterlace==FRAME_CODING)&&(params->MbInterlace==FRAME_CODING))
          img->bottompoc = img->toppoc;     //progressive
        else
          img->bottompoc = img->toppoc+1;

        img->framepoc = imin (img->toppoc, img->bottompoc);

        //the following is sent in the slice header
        if (params->BRefPictures != 1)
        {
          img->delta_pic_order_cnt[0]= 2 * (img->b_frame_to_code - 1);
        }
        else
        {
          img->delta_pic_order_cnt[0]= -2;
        }

        img->delta_pic_order_cnt[1]= 0;   // POC200301
        FrameNumberInFile = CalculateFrameNumber();

        encode_one_frame();  // encode one B-frame
        
        img->last_ref_idc = img->nal_reference_idc ? 1 : 0;

        if (params->ReportFrameStats)
          report_frame_statistic();
      }
    }
  }
  img->b_frame_to_code = 0;
}