lencod.c

来自「the newest JM software by h.264 JVT offi」· C语言 代码 · 共 2,070 行 · 第 1/5 页

C
2,070
字号
/*!
 ***********************************************************************
 *  \mainpage
 *     This is the H.264/AVC encoder reference software. For detailed documentation
 *     see the comments in each file.
 *
 *     The JM software web site is located at:
 *     http://iphome.hhi.de/suehring/tml
 *
 *     For bug reporting and known issues see:
 *     https://ipbt.hhi.de
 *
 *  \author
 *     The main contributors are listed in contributors.h
 *
 *  \version
 *     JM 15.1 (FRExt)
 *
 *  \note
 *     tags are used for document system "doxygen"
 *     available at http://www.doxygen.org
 */
/*!
 *  \file
 *     lencod.c
 *  \brief
 *     H.264/AVC reference encoder project main()
 *  \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>
 *   - Stephan Wenger                  <stewe@cs.tu-berlin.de>
 *   - Jani Lainema                    <jani.lainema@nokia.com>
 *   - Byeong-Moon Jeon                <jeonbm@lge.com>
 *   - Yoon-Seong Soh                  <yunsung@lge.com>
 *   - Thomas Stockhammer              <stockhammer@ei.tum.de>
 *   - Detlev Marpe                    <marpe@hhi.de>
 *   - Guido Heising
 *   - Valeri George                   <george@hhi.de>
 *   - Karsten Suehring                <suehring@hhi.de>
 *   - Alexis Michael Tourapis         <alexismt@ieee.org>
 ***********************************************************************
 */

#include "contributors.h"

#include <time.h>
#include <math.h>
#include <sys/timeb.h>
#include "global.h"

#include "cconv_yuv2rgb.h"
#include "configfile.h"
#include "context_ini.h"
#include "explicit_gop.h"
#include "explicit_seq.h"
#include "filehandle.h"
#include "image.h"
#include "input.h"
#include "img_io.h"
#include "slice.h"
#include "intrarefresh.h"
#include "leaky_bucket.h"
#include "memalloc.h"
#include "me_epzs.h"
#include "me_umhex.h"
#include "me_umhexsmp.h"
#include "output.h"
#include "parset.h"
#include "q_matrix.h"
#include "q_offsets.h"
#include "ratectl.h"
#include "report.h"
#include "rdoq.h"
#include "errdo.h"
#include "rdopt.h"
#include "wp_mcprec.h"
#include "mv-search.h"

InputParameters  *params = NULL;
ImageParameters  *img = NULL;


int initial_Bframes;

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

extern int  allocate_block_mem(void);
extern void free_block_mem(void);
extern int allocate_mb_mem(void);
extern void free_mb_mem(void);

static void SetLevelIndices(void);
static void chroma_mc_setup(void);
static int  get_mem_mv  (short*******);
static int  get_mem_bipred_mv (short********);
static void free_mem_bipred_mv (short*******);
static void free_mem_mv (short******);
static void init_img( ImageParameters *img, InputParameters *params);
static void init_poc(void);

static void init_encoder(ImageParameters *img);
static void encode_sequence(ImageParameters *img);


void init_stats (StatParameters *stats)
{
  memset(stats, 0, sizeof(StatParameters));
  stats->NumberBFrames = params->NumberBFrames;
}

void init_dstats (DistortionParams *dist)
{
  dist->frame_ctr = 0;
  memset(dist->metric, 0, TOTAL_DIST_TYPES * sizeof(DistMetric));
}

/*!
 ***********************************************************************
 * \brief
 *    Initialize encoding parameters.
 ***********************************************************************
 */
static void init_frame_params(ImageParameters *p_img)
{
  int base_mul = 0;

  if (params->idr_period)
  {
    if (!params->adaptive_idr_period && ( p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period == 0 )
      p_img->nal_reference_idc = NALU_PRIORITY_HIGHEST;

    if (params->adaptive_idr_period == 1 && ( p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0 )
      p_img->nal_reference_idc = NALU_PRIORITY_HIGHEST;
    else
      p_img->nal_reference_idc = (params->DisposableP) ? (p_img->frm_number + 1)% 2 : NALU_PRIORITY_LOW;
  }
  else
    p_img->nal_reference_idc = (p_img->frm_number && params->DisposableP) ? (p_img->frm_number + 1)% 2 : NALU_PRIORITY_LOW;

  //much of this can go in init_frame() or init_field()?
  //poc for this frame or field
  if (params->idr_period)
  {
    if (!params->adaptive_idr_period)
      base_mul = ( p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period;
    else if (params->adaptive_idr_period == 1)
      base_mul = (( p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0) ? 0 : ( p_img->frm_number - p_img->lastIDRnumber );
  }
  else 
    base_mul = ( p_img->frm_number - p_img->lastIDRnumber );

  if ((p_img->frm_number - p_img->lastIDRnumber) <= params->intra_delay)
  {    
    base_mul = -base_mul;
  }
  else
  {
    base_mul -= ( base_mul ? params->intra_delay :  0);    
  }

  p_img->toppoc = base_mul * (2 * p_img->base_dist);

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

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

  //the following is sent in the slice header
  p_img->delta_pic_order_cnt[0] = 0;

  if ((params->BRefPictures == 1) && (p_img->frm_number))
  {
    p_img->delta_pic_order_cnt[0] = 2 * params->NumberBFrames;
  }  

  if (params->NumberBFrames && params->last_frame && ((p_img->gop_number) + 1) == params->no_frm_base)
  {
    int bi = (int)((float)p_img->base_dist / (initial_Bframes + 1.0) + 0.499999);
    int new_bframes = ((params->last_frame - (p_img->frm_number - 1) * p_img->base_dist) / bi) - 1;

    //about to code the last ref frame, adjust delta poc
    p_img->delta_pic_order_cnt[0]= -2*(initial_Bframes - new_bframes);
    p_img->toppoc    += p_img->delta_pic_order_cnt[0];
    p_img->bottompoc += p_img->delta_pic_order_cnt[0];
    p_img->framepoc   = imin (p_img->toppoc, p_img->bottompoc);
  }

  //frame_num for this frame
  if (params->idr_period && ((!params->adaptive_idr_period && ( p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period == 0)
    || (params->adaptive_idr_period == 1 && ( p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0)) )
  {
    p_img->frame_num = 0;
  }
}

/*!
 ***********************************************************************
 * \brief
 *    Allocate the Image structure
 * \par  Output:
 *    Image Parameters ImageParameters *img
 ***********************************************************************
 */
static void alloc_img( ImageParameters **p_img)
{
  if ((*p_img = (ImageParameters *) calloc(1, sizeof(ImageParameters)))==NULL) 
    no_mem_exit("alloc_img: p_img");

  get_mem3Dint(&((*p_img)->mb_rres),   MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  get_mem3Dint(&((*p_img)->mb_ores),   MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  get_mem4Dpel(&((*p_img)->mpr_4x4),   MAX_PLANE, 9, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  get_mem4Dpel(&((*p_img)->mpr_8x8),   MAX_PLANE, 9, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  get_mem4Dpel(&((*p_img)->mpr_16x16), MAX_PLANE, 5, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  get_mem3Dpel(&((*p_img)->mb_pred),   MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
}


/*!
 ***********************************************************************
 * \brief
 *    Main function for encoder.
 * \param argc
 *    number of command line arguments
 * \param argv
 *    command line arguments
 * \return
 *    exit code
 ***********************************************************************
 */
int main(int argc, char **argv)
{ 
  p_dec = -1;

  alloc_img( &img);
  allocate_block_mem();
  allocate_mb_mem();  

  params = (InputParameters *) malloc(sizeof(InputParameters));

  Configure (argc, argv);

  // init encoder
  init_encoder(img);

  // encode sequence
  encode_sequence(img);

  // terminate sequence
  free_mb_mem();
  free_block_mem();
  free_encoder_memory(img);

  if ( params != NULL )
    free( params );

  return 0;
}


/*!
 ***********************************************************************
 * \brief
 *    Initialize encoder
 ***********************************************************************
 */

static void init_encoder(ImageParameters *img)
{
  giRDOpt_B8OnlyFlag = 0;

  p_log = NULL;

  cabac_encoding = 0;

  frame_statistic_start = 1;

  // Open Files
  OpenFiles(&params->input_file1);

  Init_QMatrix();
  Init_QOffsetMatrix();

  init_poc();
  GenerateParameterSets();
  SetLevelIndices();

  init_img(img, params);
  init_rdopt(params);


  if (params->rdopt == 3)
  {
    init_error_conceal(params->ErrorConcealment); 
  }

#ifdef _LEAKYBUCKET_
  initial_Bframes = 0;
  Bit_Buffer = (long *)malloc((params->no_frames + 1) * sizeof(long));
  total_frame_buffer = 0;
#endif

  // Prepare hierarchical coding structures. 
  // Code could be extended in the future to allow structure adaptation.
  if (params->HierarchicalCoding)
  {
    init_gop_structure(params);
    if (params->NumberBFrames && params->HierarchicalCoding == 3)
      interpret_gop_structure();
    else
      create_hierarchy(params);
  }

  dpb.init_done = 0;
  init_dpb();
  init_out_buffer();
  init_stats (stats);
  init_dstats(dist);

  enc_picture = NULL;

  init_global_buffers();

  if ( params->WPMCPrecision )
  {
    wpxInitWPXPasses(params);
  }

  create_context_memory ();
  Init_Motion_Search_Module ();
  information_init(img, params, stats);

  if(params->DistortionYUVtoRGB)
    init_YUVtoRGB();

  //Rate control
  if (params->RCEnable)
    rc_init_sequence();

  img->last_valid_reference = 0;
  tot_time = 0;                 // time for total encoding session

  initial_Bframes = params->NumberBFrames;

  PatchInputNoFrames();

  img->type = I_SLICE;
  // Write sequence header (with parameter sets)
  stats->bit_ctr_parametersets = 0;
  stats->bit_slice = start_sequence();

  if (params->UseRDOQuant)
    precalculate_unary_exp_golomb_level();

   if (params->ExplicitSeqCoding)
     OpenExplicitSeqFile(params);
}

/*!
 ***********************************************************************
 * \brief
 *    Determine coding level a frame belongs to
 ***********************************************************************
 */
static int determine_coding_level(ImageParameters *p_img, InputParameters *pparams, int curr_frame)
{
  int coding_level = 0;  

  if (curr_frame - p_img->last_idr_number == 0)
    return coding_level;
  else
    coding_level  = (curr_frame - p_img->last_idr_number - 1) % (1 + pparams->NumberBFrames);
  
  return coding_level;
}

/*!
 ************************************************************************
 * \brief
 *    Set the image type for I,P and SP pictures (not B!)
 ************************************************************************
 */
static void SetImgType(ImageParameters *img, int gop_frame_num)
{
  if (gop_frame_num == 0)
  {
    int intra_refresh = (params->intra_period == 0) ? (img->gop_number == 0) : (( ( img->frm_number - img->lastIntraNumber) % params->intra_period ) == 0);
    int idr_refresh;

    if ( params->idr_period && !params->adaptive_idr_period )
      idr_refresh = (( ( img->frm_number - img->lastIDRnumber  ) % params->idr_period   ) == 0);
    else if ( params->idr_period && params->adaptive_idr_period == 1 )
      idr_refresh = (( ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber)  ) % params->idr_period   ) == 0);
    else
      idr_refresh = (img->gop_number == 0);

    if (intra_refresh || idr_refresh)
    {
      set_slice_type( img, I_SLICE );        // set image type for first image to I-frame
    }
    else
    {
      set_slice_type(img, (params->sp_periodicity && ((img->gop_number % params->sp_periodicity) == 0))
        ? SP_SLICE  : ((params->BRefPictures == 2) ? B_SLICE : P_SLICE) );
    }
  }
  else
  {

⌨️ 快捷键说明

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