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

📄 lencod.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 4 页
字号:
/*!
 ***********************************************************************
 *  \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 14.2 (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 "filehandle.h"
#include "image.h"
#include "input.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"

InputParameters  inputs, *params = &inputs;
ImageParameters  images, *img    = &images;
Decoders       decoders, *decs   = &decoders;

#ifdef _ADAPT_LAST_GROUP_
int initial_Bframes = 0;
#endif

Boolean In2ndIGOP = FALSE;
int    start_frame_no_in_this_IGOP = 0;
int    start_tr_in_this_IGOP = 0;
int    FirstFrameIn2ndIGOP=0;
int    cabac_encoding = 0;
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 FrameNumberInFile;

extern void Init_Motion_Search_Module (void);
extern void Clear_Motion_Search_Module (void);

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

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

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

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

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

    if (params->adaptive_idr_period == 1 && ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period == 0 )
      img->nal_reference_idc = NALU_PRIORITY_HIGHEST;
    else
      img->nal_reference_idc = (params->DisposableP) ? (img->frm_number + 1)% 2 : NALU_PRIORITY_LOW;
  }
  else
    img->nal_reference_idc = (img->frm_number && params->DisposableP) ? (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 = ( img->frm_number - img->lastIDRnumber ) % params->idr_period;
    else if (params->adaptive_idr_period == 1)
      base_mul = (( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period == 0) ? 0 : ( img->frm_number - img->lastIDRnumber );
  }
  else 
    base_mul = ( img->frm_number - img->lastIDRnumber );

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

  img->toppoc = base_mul * (2 * (params->jumpd + 1));

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

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

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

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

/*!
 ***********************************************************************
 * \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)
{
  int primary_disp = 0;
  giRDOpt_B8OnlyFlag = 0;

  p_dec = p_in = -1;

  p_log = p_trace = NULL;

  frame_statistic_start = 1;

  Configure (argc, argv);

  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_
  Bit_Buffer = malloc((params->no_frames * (params->successive_Bframe + 1) + 1) * sizeof(long));
#endif

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

  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

#ifdef _ADAPT_LAST_GROUP_
  if (params->last_frame > 0)
    params->no_frames = 1 + (params->last_frame + params->jumpd) / (params->jumpd + 1);
  initial_Bframes = params->successive_Bframe;
#endif

  PatchInputNoFrames();

  // Write sequence header (with parameter sets)
  stats->bit_ctr_parametersets = 0;
  stats->bit_slice = start_sequence();
  stats->bit_ctr_parametersets += stats->bit_ctr_parametersets_n;
  start_frame_no_in_this_IGOP = 0;

  if (params->UseRDOQuant)
    precalculate_unary_exp_golomb_level();

  for (img->number=0; img->number < params->no_frames; img->number++)
  {
    img->frm_number = img->number;    
    FrameNumberInFile = CalculateFrameNumber();
    SetImgType();
    init_frame_params();

    if (img->last_ref_idc)
    {
      img->frame_num++;                 //increment frame_num once for B-frames
      img->frame_num %= max_frame_num;
    }

    //frame_num for this frame
    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->frame_num = 0;
      primary_disp   = 0;   
    }

    if (params->ResendSPS == 1 && img->type == I_SLICE && img->frm_number != 0)
    {
      stats->bit_slice = rewrite_paramsets();
      stats->bit_ctr_parametersets += stats->bit_ctr_parametersets_n;
    }

#ifdef _ADAPT_LAST_GROUP_
    if (params->successive_Bframe && params->last_frame && (IMG_NUMBER + 1) == params->no_frames)
    {
      int bi = (int)((float)(params->jumpd + 1)/(params->successive_Bframe + 1.0) + 0.499999);

      params->successive_Bframe = ((params->last_frame - (img->frm_number - 1)*(params->jumpd + 1)) / bi) - 1;

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

    //Rate control
    if (params->RCEnable && img->type == I_SLICE)
      rc_init_gop_params();

    // which layer does the image belong to?
    img->layer = (IMG_NUMBER % (params->NumFramesInELSubSeq + 1)) ? 0 : 1;

    // redundant frame initialization and allocation
    if (params->redundant_pic_flag)
    {
      Init_redundant_frame();
      Set_redundant_frame();
    }

    encode_one_frame(); // encode one I- or P-frame

    img->last_ref_idc = img->nal_reference_idc ? 1 : 0;

    // if key frame is encoded, encode one redundant frame
    if (params->redundant_pic_flag && key_frame)
    {
      encode_one_redundant_frame();
    }

    if (img->type == I_SLICE && params->EnableOpenGOP)
      img->last_valid_reference = img->ThisPOC;

    if (params->ReportFrameStats)
      report_frame_statistic();

    if (img->nal_reference_idc == 0)
    {
      primary_disp ++;
    }

    if (img->currentPicture->idr_flag)
    {
      img->idr_gop_number = 0;
    }
    else
      img->idr_gop_number ++;

    encode_enhancement_layer();    
  }
  // terminate sequence
  free_encoder_memory(img);

  return 0;
}


void free_encoder_memory(ImageParameters *img)
{
  int nplane;
  terminate_sequence();

  flush_dpb();

  close(p_in);
  if (-1 != p_dec)
    close(p_dec);
  if (p_trace)
    fclose(p_trace);

  Clear_Motion_Search_Module ();

  RandomIntraUninit();
  FmoUninit();

  if (params->HierarchicalCoding)
    clear_gop_structure ();

  // free structure for rd-opt. mode decision
  clear_rdopt (params);

#ifdef _LEAKYBUCKET_
  calc_buffer();
#endif

  // report everything
  report(img, params, stats);

#ifdef _LEAKYBUCKET_
  if (Bit_Buffer)
    free(Bit_Buffer);
#endif

  free_dpb();

  if( IS_INDEPENDENT(params) )
  {
    for( nplane=0; nplane<MAX_PLANE; nplane++ )
    {
      free_colocated(Co_located_JV[nplane]);
    }
  }
  else
  {
    free_colocated(Co_located);
  }

  uninit_out_buffer();

  free_global_buffers();

  // free image mem
  free_img ();
  free_context_memory ();
  FreeParameterSets();
}

/*!
 ***********************************************************************
 * \brief
 *    Initializes the POC structure with appropriate parameters.
 *
 ***********************************************************************
 */
void init_poc()
{
  //the following should probably go in sequence parameters
  // frame poc's increase by 2, field poc's by 1

  img->pic_order_cnt_type=params->pic_order_cnt_type;

  img->delta_pic_order_always_zero_flag = FALSE;
  img->num_ref_frames_in_pic_order_cnt_cycle= 1;

  if (params->BRefPictures == 1)
  {
    img->offset_for_non_ref_pic  =  0;
    img->offset_for_ref_frame[0] =  2;
  }
  else
  {
    img->offset_for_non_ref_pic  = -2*(params->successive_Bframe);
    img->offset_for_ref_frame[0] =  2*(params->successive_Bframe + 1);
  }

  if ((params->PicInterlace==FRAME_CODING) && (params->MbInterlace==FRAME_CODING))

⌨️ 快捷键说明

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