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

📄 b_frame.c

📁 本源码是H.26L标准的Visual C++源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose.  In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/

/*!
 *************************************************************************************
 * \file b_frame.c
 *
 * \brief
 *    B picture decoding
 *
 * \author
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
 *    - Byeong-Moon Jeon                <jeonbm@lge.com>
 *    - Yoon-Seong Soh                  <yunsung@lge.com>
 *    - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
 *************************************************************************************
 */

#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "global.h"
#include "mbuffer.h"
#include "b_frame.h"
#include "elements.h"

#define POS 0

/*!
 ************************************************************************
 * \brief
 *    Write previous decoded P frame to output file
 ************************************************************************
 */
void write_prev_Pframe(struct img_par *img, FILE *p_out)
{
  int i,j;

  for(i=0;i<img->height;i++)
    for(j=0;j<img->width;j++)
      fputc(imgY_prev[i][j],p_out);

  for(i=0;i<img->height_cr;i++)
    for(j=0;j<img->width_cr;j++)
      fputc(imgUV_prev[0][i][j],p_out);

  for(i=0;i<img->height_cr;i++)
    for(j=0;j<img->width_cr;j++)
      fputc(imgUV_prev[1][i][j],p_out);
}



/*!
 ************************************************************************
 * \brief
 *    Copy decoded P frame to temporary image array
 ************************************************************************
 */
void copy_Pframe(struct img_par *img, int postfilter)
{
  int i,j;

  /*
   * the mmin, mmax macros are taken out, because it makes no sense due to limited range of data type
   */

  if(postfilter)
  {
    for(i=0;i<img->height;i++)
      for(j=0;j<img->width;j++)
      {
        imgY_prev[i][j] = imgY_pf[i][j];
      }
    for(i=0;i<img->height_cr;i++)
      for(j=0;j<img->width_cr;j++)
      {
        imgUV_prev[0][i][j] = imgUV_pf[0][i][j];
      }
    for(i=0;i<img->height_cr;i++)
      for(j=0;j<img->width_cr;j++)
      {
        imgUV_prev[1][i][j] = imgUV_pf[1][i][j];
      }
  }
  else
  {
    for(i=0;i<img->height;i++)
      for(j=0;j<img->width;j++)
      {
        imgY_prev[i][j] = imgY[i][j];
      }
    for(i=0;i<img->height_cr;i++)
      for(j=0;j<img->width_cr;j++)
      {
        imgUV_prev[0][i][j] = imgUV[0][i][j];
      }
    for(i=0;i<img->height_cr;i++)
      for(j=0;j<img->width_cr;j++)
      {
        imgUV_prev[1][i][j] = imgUV[1][i][j];
      }
  }
}




/*!
 ************************************************************************
 * \brief
 *    init macroblock B frames
 ************************************************************************
 */
void init_macroblock_Bframe(struct img_par *img)
{
  int i,j;
  int fw_predframe_no=0;
    Macroblock *currMB = &img->mb_data[img->current_mb_nr];

    currMB->ref_frame = 0;
    currMB->predframe_no = 0;


  // reset vectors and pred. modes
  for (i=0;i<BLOCK_SIZE;i++)
  {
    for(j=0;j<BLOCK_SIZE;j++)
    {
      img->fw_mv[img->block_x+i+4][img->block_y+j][0]=img->fw_mv[img->block_x+i+4][img->block_y+j][1]=0;
      img->bw_mv[img->block_x+i+4][img->block_y+j][0]=img->bw_mv[img->block_x+i+4][img->block_y+j][1]=0;
      img->dfMV[img->block_x+i+4][img->block_y+j][0]=img->dfMV[img->block_x+i+4][img->block_y+j][1]=0;
      img->dbMV[img->block_x+i+4][img->block_y+j][0]=img->dbMV[img->block_x+i+4][img->block_y+j][1]=0;
      img->ipredmode[img->block_x+i+1][img->block_y+j+1] = 0;
    }
  }

  // Set the reference frame information for motion vector prediction
  if (img->imod==INTRA_MB_OLD || img->imod==INTRA_MB_NEW)
  {
    for (j = 0;j < BLOCK_SIZE;j++)
    {
      for (i = 0;i < BLOCK_SIZE;i++)
      {
        img->fw_refFrArr[img->block_y+j][img->block_x+i] =
        img->bw_refFrArr[img->block_y+j][img->block_x+i] = -1;
      }
    }
  }
  else if(img->imod == B_Forward)
  {
    for (j = 0;j < BLOCK_SIZE;j++)
    {
      for (i = 0;i < BLOCK_SIZE;i++)
      {
        img->fw_refFrArr[img->block_y+j][img->block_x+i] = fw_predframe_no; // previous P
        img->bw_refFrArr[img->block_y+j][img->block_x+i] = -1;
      }
    }
  }
  else if(img->imod == B_Backward)
  {
    for (j = 0;j < BLOCK_SIZE;j++)
    {
      for (i = 0;i < BLOCK_SIZE;i++)
      {
        img->fw_refFrArr[img->block_y+j][img->block_x+i] = -1;
        img->bw_refFrArr[img->block_y+j][img->block_x+i] = 0; // next P
      }
    }
  }
  else if(img->imod == B_Bidirect)
  {
    for (j = 0;j < BLOCK_SIZE;j++)
    {
      for (i = 0;i < BLOCK_SIZE;i++)
      {
        img->fw_refFrArr[img->block_y+j][img->block_x+i] = fw_predframe_no; // previous P
        img->bw_refFrArr[img->block_y+j][img->block_x+i] = 0; // next P
      }
    }
  }
  else if(img->imod == B_Direct)
  {
    for (j = 0;j < BLOCK_SIZE;j++)
    {
      for (i = 0;i < BLOCK_SIZE;i++)
      {
        img->fw_refFrArr[img->block_y+j][img->block_x+i] = -1; // previous P
        img->bw_refFrArr[img->block_y+j][img->block_x+i] = -1; // next P
      }
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Get for a given MB of a B picture the reference frame
 *    parameter and the motion vectors from the NAL
 *
 ************************************************************************
 */
void readMotionInfoFromNAL_Bframe(struct img_par *img,struct inp_par *inp)
{
  int i,j,ii,jj,ie,j4,i4,k,pred_vec=0;
  int vec;
  int ref_frame;
  int fw_predframe_no=0, bw_predframe_no;     // frame number which current MB is predicted from
  int fw_blocktype=0, bw_blocktype=0, blocktype=0;
  int l,m;

  // keep track of neighbouring macroblocks available for prediction
  int mb_nr = img->current_mb_nr;
  int mb_width = img->width/16;
  int mb_available_up = (img->mb_y == 0) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width].slice_nr);
  int mb_available_left = (img->mb_x == 0) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-1].slice_nr);
  int mb_available_upleft  = (img->mb_x == 0 || img->mb_y == 0) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width-1].slice_nr);
  int mb_available_upright = (img->mb_x >= mb_width-1 || img->mb_y == 0) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width+1].slice_nr);

  // keep track of neighbouring blocks available for motion vector prediction
  int block_available_up, block_available_left, block_available_upright, block_available_upleft;
  int mv_a, mv_b, mv_c, mv_d;
  int mvPredType, rFrameL, rFrameU, rFrameUR;

  SyntaxElement THEcurrSE;
  SyntaxElement *currSE = &THEcurrSE;

  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  Slice *currSlice = img->currentSlice;
  DataPartition *dp;
  int *partMap = assignSE2partition[currSlice->dp_mode];

  bw_predframe_no=0;


  // /////////////////////////////////////////////////////////////
  // find fw_predfame_no
  // /////////////////////////////////////////////////////////////
  if(img->imod==B_Forward || img->imod==B_Bidirect)
  {
    if(img->type==B_IMG_MULT)
    {

#if TRACE
      snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_fw_Reference frame no  ");
#endif
      currSE->type = SE_REFFRAME;
      dp = &(currSlice->partArr[partMap[SE_BFRAME]]);

      if (inp->symbol_mode == UVLC)
        currSE->mapping = linfo;
      else
        currSE->reading = readRefFrameFromBuffer_CABAC;

      dp->readSyntaxElement(  currSE, img, inp, dp);
      fw_predframe_no = currSE->value1;


      if (fw_predframe_no > img->buf_cycle)
      {
        set_ec_flag(SE_REFFRAME);
        fw_predframe_no = 1;
      }

      ref_frame=fw_predframe_no;

 /*     if (ref_frame > img->number)
      {
        ref_frame = 0;
      }*/

      currMB->predframe_no = fw_predframe_no;
      currMB->ref_frame = img->ref_frame = ref_frame;

      // Update the reference frame information for motion vector prediction
      for (j = 0;j < BLOCK_SIZE;j++)
      {
        for (i = 0;i < BLOCK_SIZE;i++)
        {
          img->fw_refFrArr[img->block_y+j][img->block_x+i] = fw_predframe_no;
        }
      }
    } 
    else
      fw_predframe_no = currMB->predframe_no;
  }

  // /////////////////////////////////////////////////////////////
  // find blk_size
  // /////////////////////////////////////////////////////////////
  if(img->imod==B_Bidirect)
  {

#if TRACE
    snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_bidiret_fw_blk");
#endif

    currSE->type = SE_BFRAME;

    dp = &(currSlice->partArr[partMap[SE_BFRAME]]);

    if (inp->symbol_mode == UVLC)
      currSE->mapping = linfo;
    else
      currSE->reading = readBiDirBlkSize2Buffer_CABAC;

    dp->readSyntaxElement(  currSE, img, inp, dp);

    fw_blocktype = currSE->value1+1;


#if TRACE
    snprintf(currSE->tracestring, TRACESTRING_SIZE, "B_bidiret_bw_blk");
#endif

    currSE->type = SE_BFRAME;

    dp = &(currSlice->partArr[partMap[SE_BFRAME]]);

    if (inp->symbol_mode == UVLC)
      currSE->mapping = linfo;
    else
      currSE->reading = readBiDirBlkSize2Buffer_CABAC;


    dp->readSyntaxElement(  currSE, img, inp, dp);

    bw_blocktype = currSE->value1+1;

  }

  // /////////////////////////////////////////////////////////////
  // find MVDFW
  // /////////////////////////////////////////////////////////////
  if(img->imod==B_Forward || img->imod==B_Bidirect)
  {
    // forward : note realtion between blocktype and img->mb_mode
    // bidirect : after reading fw_blk_size, fw_pmv is obtained
    if(img->mb_mode==1)
      blocktype=1;
    else if(img->mb_mode>3)
      blocktype=img->mb_mode/2;
    else if(img->mb_mode==3)
      blocktype=fw_blocktype;

    img->fw_blc_size_h = BLOCK_STEP[blocktype][0]*4;
    img->fw_blc_size_v = BLOCK_STEP[blocktype][1]*4;

    ie=4-BLOCK_STEP[blocktype][0];
    for(j=0;j<4;j=j+BLOCK_STEP[blocktype][1])   // Y position in 4-pel resolution

⌨️ 快捷键说明

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