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

📄 macroblock.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 5 页
字号:

/*!
 *************************************************************************************
 * \file macroblock.c
 *
 * \brief
 *    Process one macroblock
 *
 * \author
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
 *    - Inge Lille-Langoy               <inge.lille-langoy@telenor.com>
 *    - Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
 *    - Jani Lainema                    <jani.lainema@nokia.com>
 *    - Sebastian Purreiter             <sebastian.purreiter@mch.siemens.de>
 *    - Detlev Marpe                    <marpe@hhi.de>
 *    - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
 *    - Ragip Kurceren                  <ragip.kurceren@nokia.com>
 *    - Alexis Michael Tourapis         <alexismt@ieee.org>
 *************************************************************************************
 */

#include "contributors.h"

#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <memory.h>
#include <math.h>

#include "global.h"

#include "elements.h"
#include "macroblock.h"
#include "refbuf.h"
#include "fmo.h"
#include "vlc.h"
#include "image.h"
#include "mb_access.h"
#include "ratectl.h"              // header file for rate control
#include "rc_quadratic.h"
#include "cabac.h"
#include "transform8x8.h"
#include "transform.h"
#include "me_fullsearch.h"
#include "symbol.h"

#if TRACE
#define TRACE_SE(trace,str)  snprintf(trace,TRACESTRING_SIZE,str)
#else
#define TRACE_SE(trace,str)
#endif

extern const byte QP_SCALE_CR[52] ;

//Rate control
extern int delta_qp_mbaff[2][2],delta_qp_mbaff[2][2];
extern int qp_mbaff[2][2],qp_mbaff[2][2];

// function pointer for different ways of obtaining chroma interpolation
static void (*OneComponentChromaPrediction4x4) (imgpel* , int , int , short****** , int , short , int , int );
static void OneComponentChromaPrediction4x4_regenerate (imgpel* , int , int , short****** , int , short , int , int );
static void OneComponentChromaPrediction4x4_retrieve (imgpel* , int , int , short****** , int , short , int , int );

static int  slice_too_big(int rlc_bits);

static int  writeChromaIntraPredMode (Macroblock* currMB);
static int  writeMotionInfo2NAL      (Macroblock* currMB);
static int  writeChromaCoeff         (Macroblock* currMB);
static int  writeCBPandDquant        (Macroblock* currMB);

extern int *mvbits;

extern int QP2QUANT[40];
extern int ver_offset[4][8][4];
extern int hor_offset[4][8][4];

static int diff  [16];
static int diff64[64];
static imgpel l0_pred[MB_PIXELS];
static imgpel l1_pred[MB_PIXELS];
static const unsigned char subblk_offset_x[3][8][4] =
{
  { {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}, },

  { {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}, },

  { {0, 4, 0, 4},
    {8,12, 8,12},
    {0, 4, 0, 4},
    {8,12, 8,12},
    {0, 4, 0, 4},
    {8,12, 8,12},
    {0, 4, 0, 4},
    {8,12, 8,12}  }
};

static const unsigned char subblk_offset_y[3][8][4] =
{
  { {0, 0, 4, 4},
    {0, 0, 4, 4},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}, },

  { {0, 0, 4, 4},
    {8, 8,12,12},
    {0, 0, 4, 4},
    {8, 8,12,12},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0} },

 { {0, 0, 4, 4},
   {0, 0, 4, 4},
   {8, 8,12,12},
   {8, 8,12,12},
   {0, 0, 4, 4},
   {0, 0, 4, 4},
   {8, 8,12,12},
   {8, 8,12,12} }
};

extern ColocatedParams *Co_located;
extern ColocatedParams *Co_located_JV[MAX_PLANE];  //!< Co_located to be used during 4:4:4 independent mode encoding

 /*!
 ************************************************************************
 * \brief
 *    updates the coordinates for the next macroblock to be processed
 *
 * \param mb_addr
 *    macroblock address in scan order
 ************************************************************************
 */
void set_MB_parameters (int mb_addr)
{
  img->current_mb_nr = mb_addr;

  get_mb_block_pos(mb_addr, &img->mb_x, &img->mb_y);

  img->block_x = img->mb_x << 2;
  img->block_y = img->mb_y << 2;
  img->pix_x   = img->block_x << 2;
  img->pix_y   = img->block_y << 2;
  img->opix_x  = img->pix_x;

  if (img->MbaffFrameFlag)
  {
    if (img->mb_data[mb_addr].mb_field)
    {
      pCurImg    = (mb_addr & 0x01) ? imgY_org_bot  : imgY_org_top;
      pImgOrg[0] = pCurImg;
      if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(input))
      {
        imgUV_org  = (mb_addr & 0x01) ? imgUV_org_bot : imgUV_org_top;
        pImgOrg[1] = imgUV_org[0];
        pImgOrg[2] = imgUV_org[1];
      }

      img->opix_y   = (img->mb_y >> 1 ) << 4;
      img->mb_data[mb_addr].list_offset = (mb_addr % 2) ? 4 : 2;
    }
    else
    {
      pCurImg    = imgY_org_frm;      
      pImgOrg[0] = imgY_org_frm;
      if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(input))
      {
        imgUV_org  = imgUV_org_frm;
        pImgOrg[1] = imgUV_org_frm[0];
        pImgOrg[2] = imgUV_org_frm[1];
      }
      img->opix_y   = img->block_y << 2;
      img->mb_data[mb_addr].list_offset = 0;
    }
  }
  else
  {
    img->opix_y   = img->block_y << 2;
    img->mb_data[mb_addr].list_offset = 0;
  }

  if (img->yuv_format != YUV400)
  {
    img->pix_c_x = (img->mb_cr_size_x * img->pix_x) >> 4;
    img->pix_c_y = (img->mb_cr_size_y * img->pix_y) >> 4;

    img->opix_c_x = (img->mb_cr_size_x * img->opix_x) >> 4;
    img->opix_c_y = (img->mb_cr_size_y * img->opix_y) >> 4;
  }
  //  printf ("set_MB_parameters: mb %d,  mb_x %d,  mb_y %d\n", mb_addr, img->mb_x, img->mb_y);
}


/*!
 ************************************************************************
 * \brief
 *    updates the coordinates and statistics parameter for the
 *    next macroblock
 ************************************************************************
 */
void proceed2nextMacroblock(Macroblock *currMB)
{
#if TRACE
  int use_bitstream_backing = (input->slice_mode == FIXED_RATE || input->slice_mode == CALL_BACK);
#endif
  int*        bitCount = currMB->bitcounter;
  int i;

  if (bitCount[BITS_TOTAL_MB] > img->max_bitCount)
    printf("Warning!!! Number of bits (%d) of macroblock_layer() data seems to exceed defined limit (%d).\n", bitCount[BITS_TOTAL_MB],img->max_bitCount);

  // Update the statistics
  stats->bit_use_mb_type[img->type]     += bitCount[BITS_MB_MODE       ];
  stats->tmp_bit_use_cbp[img->type]     += bitCount[BITS_CBP_MB        ];
  stats->bit_use_coeffC[img->type]      += bitCount[BITS_COEFF_UV_MB   ];
  stats->bit_use_coeff[0][img->type]    += bitCount[BITS_COEFF_Y_MB    ];
  stats->bit_use_coeff[1][img->type]    += bitCount[BITS_COEFF_CB_MB   ]; 
  stats->bit_use_coeff[2][img->type]    += bitCount[BITS_COEFF_CR_MB   ]; 
  stats->bit_use_delta_quant[img->type] += bitCount[BITS_DELTA_QUANT_MB];
  stats->bit_use_stuffingBits[img->type] += bitCount[BITS_STUFFING];

  if (IS_INTRA(currMB))
  {
    ++stats->intra_chroma_mode[currMB->c_ipred_mode];

    if ((currMB->cbp&15) != 0)
    {
      ++stats->mode_use_transform[currMB->luma_transform_size_8x8_flag][img->type][currMB->mb_type];
    }
  }

  ++stats->mode_use[img->type][currMB->mb_type];
  stats->bit_use_mode[img->type][currMB->mb_type]+= bitCount[BITS_INTER_MB];

  if (img->type != I_SLICE)
  {
    if (currMB->mb_type == P8x8)
    {
      for(i=0;i<4;i++)
      {
        if (currMB->b8mode[i] > 0)
          ++stats->mode_use[img->type][currMB->b8mode[i]];
        else
          ++stats->b8_mode_0_use[img->type][currMB->luma_transform_size_8x8_flag];

        if (currMB->b8mode[i]==4)
        {
          if ((currMB->luma_transform_size_8x8_flag && (currMB->cbp&15) != 0) || input->Transform8x8Mode == 2)
            ++stats->mode_use_transform[1][img->type][4];
          else
            ++stats->mode_use_transform[0][img->type][4];
        }
      }
    }
    else if (currMB->mb_type >= 0 && currMB->mb_type <=3 && ((currMB->cbp&15) != 0))
    {
      ++stats->mode_use_transform[currMB->luma_transform_size_8x8_flag][img->type][currMB->mb_type];
    }
  }

  // Statistics
  if ((img->type == P_SLICE)||(img->type==SP_SLICE) )
  {
    ++stats->quant0;
    stats->quant1 += currMB->qp;      // to find average quant for inter frames
  }
}

/*!
************************************************************************
* \brief
*    updates chroma QP according to luma QP and bit depth
************************************************************************
*/
void set_chroma_qp(Macroblock *currMB)
{
  int i;
  for (i=0; i<2; i++)
  {
    currMB->qpc[i] = iClip3 ( -img->bitdepth_chroma_qp_scale, 51, currMB->qp + img->chroma_qp_offset[i] );
    currMB->qpc[i] = currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]];
  }
}

/*!
 ************************************************************************
 * \brief
 *    initializes the current macroblock
 *
 * \param mb_addr
 *    macroblock address in scan order
 * \param mb_field
 *    true for field macroblock coding
 ************************************************************************
 */
void start_macroblock(Macroblock **currMB, int mb_addr, int mb_field)
{
  int i,j,l;
  int use_bitstream_backing = (input->slice_mode == FIXED_RATE || input->slice_mode == CALL_BACK);
  Slice *curr_slice = img->currentSlice;
  DataPartition *dataPart;
  Bitstream *currStream;
  int prev_mb;

  *currMB = &img->mb_data[mb_addr];
  (*currMB)->mbAddrX = mb_addr;


  (*currMB)->mb_field = mb_field;

  enc_picture->mb_field[mb_addr] = mb_field;
  (*currMB)->is_field_mode = (img->field_picture || ( img->MbaffFrameFlag && (*currMB)->mb_field));

  set_MB_parameters (mb_addr);

  prev_mb = FmoGetPreviousMBNr(img->current_mb_nr);

  if ( input->ChromaMCBuffer )
    OneComponentChromaPrediction4x4 = OneComponentChromaPrediction4x4_retrieve;
  else
    OneComponentChromaPrediction4x4 = OneComponentChromaPrediction4x4_regenerate;

  if(use_bitstream_backing)
  {
    if ((!input->MbInterlace)||((mb_addr & 0x01)==0)) // KS: MB AFF -> store stream positions for 1st MB only
    {
      // Keep the current state of the bitstreams
      if(!img->cod_counter)
      {
        for (i=0; i<curr_slice->max_part_nr; i++)
        {
          dataPart = &(curr_slice->partArr[i]);
          currStream = dataPart->bitstream;
          currStream->stored_bits_to_go = currStream->bits_to_go;
          currStream->stored_byte_pos   = currStream->byte_pos;
          currStream->stored_byte_buf   = currStream->byte_buf;
          stats->stored_bit_slice       = stats->bit_slice;

          if (input->symbol_mode ==CABAC)

⌨️ 快捷键说明

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