⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slice.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
    img->NumberofBasicUnitHeaderBits +=len;

  //  printf("short size, used, num-used: (%d,%d,%d)\n", fb->short_size, fb->short_used, fb->num_short_used);

  /*
  // Tian Dong: June 7, 2002 JVT-B042
  if (input->NumFramesInELSubSeq)
  {
  fb->short_used = fb->num_short_used;
  }
  */
  // Update statistics
  stats->bit_slice += len;
  stats->bit_use_header[img->type] += len;
  // printf ("\n\n");

#ifdef USE_INTRA_MDDT
  if(input->UseIntraMDDT)
  {
    memset(img->quant_stat,    0, sizeof(long)*16);
    memset(img->quant_stat8x8, 0, sizeof(long)*64);
    memset(img->quant_stat16x16, 0, sizeof(long)*256);
  }
#endif 

  while (end_of_slice == FALSE) // loop over macroblocks
  {
    if (img->AdaptiveRounding && input->AdaptRndPeriod && (img->current_mb_nr % input->AdaptRndPeriod == 0))
    {
      CalculateOffsetParam();

      if(input->Transform8x8Mode)
      {
        CalculateOffset8Param();
      }
    }

    //sw paff
    if (!img->MbaffFrameFlag)
    {
#ifdef RDO_Q
      static int deltaQPTabB[] = {0, 1, -1, 2,  3};
      static int deltaQPTabP[] = {0, 1, -1, 2, -2};
      static int deltaQPTabSizeB = 5;
      static int deltaQPTabSizeP = 5; 
      int        deltaQPCnt; 
      int master_cr_qp=0, master_cr_cbp=0, cr_qp;

#ifdef ADAPTIVE_QUANTIZATION
      init_internal_parameter(CurrentMbAddr, &best_iaqms_idx, &saved_best_mode, &bestQP, &best_deltaQP, &best_athr, &best_cbp);
#endif
#endif
      recode_macroblock = FALSE;
      rdopt = &rddata_top_frame_mb;   // store data in top frame MB 

#ifndef RDO_Q
      start_macroblock (CurrentMbAddr, FALSE);
#else
#ifdef ADAPTIVE_QUANTIZATION
      if(input->UseRDO_Q || img->slice_fractional_quant_flag)
#else
      if(input->UseRDO_Q)
#endif
      {
        masterQP=img->masterQP=img->qp;

        Motion_Selected = 0;
        final_mb_encoding = 0;

        if(img->type == B_SLICE)
        {
          int qp_anchor; 
          Macroblock *currMB;
          start_macroblock (CurrentMbAddr, FALSE);

          currMB = &img->mb_data[img->current_mb_nr];
          qp_left = (currMB->mb_available_left) ? currMB->mb_available_left->qp:img->masterQP;
          qp_up = (currMB->mb_available_up) ? currMB->mb_available_up->qp:img->masterQP;
          qp_anchor = (qp_left+qp_up+1)>>1;
          min_rdcost = 1e30; 
          for (deltaQPCnt=0; deltaQPCnt<deltaQPTabSizeB; deltaQPCnt++)
          {
            deltaQP = deltaQPTabB[deltaQPCnt];
            img->qp=Clip3(-img->bitdepth_luma_qp_scale, 51, (masterQP+deltaQP));

            // skip this QP if deltaQP is too large based on local stats 
            if(!(img->qp-qp_anchor >= -1 && img->qp-qp_anchor <= 2) && currMB->mb_available_left && currMB->mb_available_up)
              continue;

#ifdef ADAPTIVE_QUANTIZATION
            if(!input->UseRDO_Q && deltaQP!=0) continue;
            if(img->slice_fractional_quant_flag)
            {
              encode_one_macroblock_for_iaqms(CurrentMbAddr, &min_rdcost, &best_iaqms_idx, &saved_best_mode, &bestQP, &best_deltaQP, &best_athr, &best_cbp, masterQP, deltaQP);
            }
            else
#endif
              encode_one_macroblock_each_quant(CurrentMbAddr);

            Motion_Selected = 1;
            if (rdopt->min_rdcost<min_rdcost)
            {
              min_rdcost=rdopt->min_rdcost;
              bestQP=img->qp;
              best_deltaQP = deltaQP;
              saved_best_mode = img->mb_data[img->current_mb_nr].mb_type;//best_mode;
            }
          }
        }
        else
        {   // I and P slices
          int qp_anchor; 
          Macroblock *currMB; 

          start_macroblock (CurrentMbAddr, FALSE);

          currMB = &img->mb_data[img->current_mb_nr];
          qp_left = (currMB->mb_available_left) ? currMB->mb_available_left->qp:img->masterQP;
          qp_up = (currMB->mb_available_up) ? currMB->mb_available_up->qp:img->masterQP;
          qp_anchor = (qp_left+qp_up+1)>>1;

          min_rdcost = 1e30;
          for (deltaQPCnt=0; deltaQPCnt<deltaQPTabSizeP; deltaQPCnt++)
          {
            deltaQP = deltaQPTabP[deltaQPCnt];
            img->qp=Clip3(-img->bitdepth_luma_qp_scale, 51, (masterQP+deltaQP));

            if(deltaQP != 0 && !(img->qp-qp_anchor >= -2 && img->qp-qp_anchor <= 1) && currMB->mb_available_left && currMB->mb_available_up && img->type == P_SLICE)
              continue; 

#ifdef ADAPTIVE_QUANTIZATION
            if(!input->UseRDO_Q && deltaQP!=0) continue;
            if(img->slice_fractional_quant_flag)
            {
              encode_one_macroblock_for_iaqms(CurrentMbAddr, &min_rdcost, &best_iaqms_idx, &saved_best_mode, &bestQP, &best_deltaQP, &best_athr, &best_cbp, masterQP, deltaQP);
            }
            else
#endif
              encode_one_macroblock_each_quant(CurrentMbAddr);

            Motion_Selected = 1;
#ifdef ADAPTIVE_QUANTIZATION
            if(input->successive_Bframe==0 && input->UseRDO_Q)
#else
            if(input->successive_Bframe==0)
#endif
            {
              static byte QP_SCALE_CR[52]=
              {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,
                12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
                28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,
                37,38,38,38,39,39,39,39
              };

              cr_qp = Clip3 ( -img->bitdepth_chroma_qp_scale, 51, img->qp + img->chroma_qp_offset[0] );
              cr_qp = (cr_qp < 0 ? cr_qp : QP_SCALE_CR[cr_qp]) + img->bitdepth_chroma_qp_scale;

              if(deltaQP == 0) // deltaQP =0 has to be test first!
              {
                master_cr_cbp = img->mb_data[img->current_mb_nr].cbp/16;
                master_cr_qp = cr_qp;
              }
              else
              { 
                if(deltaQP>0 && cr_qp > master_cr_qp && master_cr_cbp!=0 )
                  continue;
              }
            }

            if (rdopt->min_rdcost<min_rdcost)
            {
              min_rdcost=rdopt->min_rdcost;
              bestQP=img->qp;
              best_deltaQP = deltaQP;
              saved_best_mode = img->mb_data[img->current_mb_nr].mb_type;//best_mode;
            }
          }
        }

        final_mb_encoding = 1;
        img->qp=Clip3(-img->bitdepth_luma_qp_scale, 51, (masterQP + best_deltaQP));
#ifdef ADAPTIVE_QUANTIZATION
        img->mb_iaqms_idx = best_iaqms_idx;
        input->disthres  = best_athr;
#endif
        encode_one_macroblock_each_quant(CurrentMbAddr);
      }
      else
        start_macroblock (CurrentMbAddr, FALSE);
#endif

#ifdef ADAPTIVE_FD_SD_CODING
      currMB    = &img->mb_data[img->current_mb_nr];

      if (img->APEC_in_FD_and_SD==0)
      {
        currMB->SD_Coding_on_off=0;
#ifdef RDO_Q
#ifdef ADAPTIVE_QUANTIZATION
        if(!input->UseRDO_Q && !(img->slice_fractional_quant_flag) )
#else
        if(!input->UseRDO_Q)
#endif
          encode_one_macroblock ();
#else
        encode_one_macroblock ();
#endif

#ifdef USE_NEW_OFFSET
        if(input->UseNewOffset)
        {
          if(img->frameOffsetAvail == 0)
            SetFrameOffset(); 
        }
#endif

#ifdef ADAPTIVE_FILTER
        if(UseAdaptiveFilterForCurrentFrame() && img->AdaptiveFilterFlag == 0)
        {
          if ((input->UseAdaptiveFilter == 1) || (input->UseAdaptiveFilter == 3) || (input->UseAdaptiveFilter == FILTER_TYPE_EDAIF))
            SetAdaptiveFilter();
          else if (input->UseAdaptiveFilter == 2)
            SetAdaptiveFilterHor(); // separable aif
        }
#endif
        write_one_macroblock (1);

#ifdef RDO_Q
        if(input->UseRDO_Q)
          img->qp=masterQP;
#endif
      }
      else
      {
        store_coding_state(cs_slice_coding1);
        slice_bits=stats->bit_slice;
        currMB->SD_Coding_on_off=1;
#ifdef ADAPTIVE_QUANTIZATION
        if(img->slice_fractional_quant_flag)
          encode_one_macroblock_each_quant(CurrentMbAddr);
        else
#endif
          encode_one_macroblock ();

        store_rdopt_data_for_FDSD_coding ();//copy all relevant data to rdopt_FDSD buffer

        write_one_macroblock (1);
        rate_only_FD=stats->bit_slice-slice_bits;
        SSD_only_FD=0;
        for (i=0;i<16;i++)
        {
          for (j=0;j<16;j++)
          {
#ifdef  INTERNAL_BIT_DEPTH_INCREASE
            SSD_only_FD+=SQR_DEPTH(enc_picture->imgY[img->pix_y+j][img->pix_x+i], imgY_org[img->opix_y+j][img->opix_x+i], input->BitDepthLuma, img->BitDepthIncrease);
#else
            SSD_only_FD+=img->quad[enc_picture->imgY[img->pix_y+j][img->pix_x+i]-imgY_org[img->opix_y+j][img->opix_x+i]];
#endif
          }
        }
        slice_bits=stats->bit_slice;
        currMB->SD_Coding_on_off=0;//Turn SD Coding off
        reset_coding_state(cs_slice_coding1);
#ifdef ADAPTIVE_QUANTIZATION
        if(img->slice_fractional_quant_flag)
          encode_one_macroblock_each_quant(CurrentMbAddr);
        else
#endif
          encode_one_macroblock ();

        write_one_macroblock (1);
        rate_FD_and_SD=stats->bit_slice-slice_bits;
        SSD_FD_and_SD=0;
        for (i=0;i<16;i++)
        {
          for (j=0;j<16;j++)
          {
#ifdef  INTERNAL_BIT_DEPTH_INCREASE
            SSD_FD_and_SD+=SQR_DEPTH(enc_picture->imgY[img->pix_y+j][img->pix_x+i], imgY_org[img->opix_y+j][img->opix_x+i], input->BitDepthLuma, img->BitDepthIncrease);
#else
            SSD_FD_and_SD+=img->quad[enc_picture->imgY[img->pix_y+j][img->pix_x+i]-imgY_org[img->opix_y+j][img->opix_x+i]];
#endif
          }
        }

        rd_cost_only_FD  =(double)SSD_only_FD  +img->lambda_md[img->type][img->qp] * (double)rate_only_FD;
        rd_cost_FD_and_SD=(double)SSD_FD_and_SD+img->lambda_md[img->type][img->qp] * (double)rate_FD_and_SD;

        if (rd_cost_only_FD<rd_cost_FD_and_SD)
        {
          reset_coding_state(cs_slice_coding1);
          slice_bits=stats->bit_slice;
          currMB->SD_Coding_on_off=1;

          restore_rdopt_data_for_FDSD_coding ();

          write_one_macroblock (1);
        }
#ifdef ADAPTIVE_FILTER
        if(UseAdaptiveFilterForCurrentFrame() && img->AdaptiveFilterFlag == 0)
        {
          if ((input->UseAdaptiveFilter == 1) || ((input->UseAdaptiveFilter == 3) || (input->UseAdaptiveFilter == FILTER_TYPE_EDAIF)))
            SetAdaptiveFilter();
          else
            SetAdaptiveFilterHor(); // separable aif
        }
#endif
      }
      if (currMB->written_SD_Coding_on_off==0)
      {
        currMB->SD_Coding_on_off=0;
      }
#else//ADAPTIVE_FD_SD_CODING
#ifdef RDO_Q
#ifdef ADAPTIVE_QUANTIZATION
      if(!input->UseRDO_Q && !(img->slice_fractional_quant_flag) )
#else
      if(!input->UseRDO_Q)
#endif
        encode_one_macroblock ();
#else
      encode_one_macroblock ();
#endif
#ifdef ADAPTIVE_FILTER
      if(UseAdaptiveFilterForCurrentFrame() && img->AdaptiveFilterFlag == 0)
      {
        if ((input->UseAdaptiveFilter == 1) || ((input->UseAdaptiveFilter == 3) || (input->UseAdaptiveFilter == FILTER_TYPE_EDAIF)))
          SetAdaptiveFilter();
        else
          SetAdaptiveFilterHor(); // separable aif
      }
#endif
      write_one_macroblock (1);

#ifdef RDO_Q
      if(input->UseRDO_Q)
        img->qp=masterQP;
#endif

#endif //ADAPTIVE_FD_SD_CODING
#ifdef ADAPTIVE_QUANTIZATION
      set_internal_parameter();
#endif

      terminate_macroblock (&end_of_slice, &recode_macroblock);

      //       printf ("encode_one_slice: mb %d,  slice %d,   bitbuf bytepos %d EOS %d\n", 
      //       img->current_mb_nr, img->current_slice_nr, 
      //       img->currentSlice->partArr[0].bitstream->byte_pos, end_of_slice);

      if (recode_macroblock == FALSE)       // The final processing of the macroblock has been done
      {
        CurrentMbAddr = FmoGetNextMBNr (CurrentMbAddr);
        if (CurrentMbAddr == -1)   // end of slice
        {
          //          printf ("FMO End of Slice Group detected, current MBs %d, force end of slice\n", NumberOfCodedMBs+1);
          end_of_slice = TRUE;
#ifdef USE_NEW_OFFSET
          if(input->UseNewOffset)
          {
            if(img->frameOffsetAvail == 0)
            {
              CalcFrameOffset();
              img->frameOffsetAvail = 1; 
            }
          }
#endif
#ifdef ADAPTIVE_FILTER
          if(UseAdaptiveFilterForCurrentFrame() && img->AdaptiveFilterFlag == 0)
          {
            if((input->UseAdaptiveFilter == 1) || (input->UseAdaptiveFilter == 3) || (input->UseAdaptiveFilter == FILTER_TYPE_EDAIF))
              FindFilterCoef();
            else
              FindFilterCoefHor(); // separable aif
          }           
#endif    
        }
        NumberOfCodedMBs++;       // only here we are sure that the coded MB is actually included in the slice
        proceed2nextMacroblock ();
      }
      else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -