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

📄 lencod.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
***********************************************************************
* 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.
************************************************************************
*/

/*!
 ***********************************************************************
 *  \mainpage
 *     This is the H.26L encoder reference software. For detailed documentation
 *     see the comments in each file.
 *
 *  \author
 *     The main contributors are listed in contributors.h
 *
 *  \version
 *     JM 6.1e
 *
 *  \note
 *     tags are used for document system "doxygen"
 *     available at http://www.doxygen.org
 */
/*!
 *  \file
 *     lencod.c
 *  \brief
 *     TML encoder project main
 *  \author
 *   Main contributors (see contributors.h for copyright, address and affiliation details)
 *   - Inge Lille-Lang鴜               <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                   <heising@hhi.de>
 *
 ***********************************************************************
 */

#include "contributors.h"

#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
#include <stdlib.h>
#if defined WIN32
  #include <conio.h>
#endif
#include <assert.h>

#include "global.h"
#include "configfile.h"
#include "leaky_bucket.h"
#include "memalloc.h"
#include "mbuffer.h"
#include "intrarefresh.h"
#include "fmo.h"
#include "encodeiff.h"
#include "sei.h"
#include "parset.h"
#include "image.h"

#define JM      "6"
#define VERSION "6.1e"

InputParameters inputs, *input = &inputs;
ImageParameters images, *img   = &images;
StatParameters  stats,  *stat  = &stats;
SNRParameters   snrs,   *snr   = &snrs;
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;


/*!
 ***********************************************************************
 * \brief
 *    Main function for encoder.
 * \param argc
 *    number of command line arguments
 * \param argv
 *    command line arguments
 * \return
 *    exit code
 ***********************************************************************
 */
void Init_Motion_Search_Module ();
void Clear_Motion_Search_Module ();

int main(int argc,char **argv)
{
  int image_type;
  int num_ref_pic_bak;
  
  p_dec = p_dec_u = p_dec_v = p_stat = p_log = p_datpart = p_trace = NULL;

  
//  isBigEndian = testEndian();

  Configure (argc, argv);
  init_img();
  AllocNalPayloadBuffer();

  frame_pic = malloc_picture();
  if (input->InterlaceCodingOption != FRAME_CODING)
  {
    top_pic = malloc_picture();
    bottom_pic = malloc_picture();
  }
  init_rdopt ();

  // allocate memory for frame buffers
  init_frame_buffers(input,img);
  init_global_buffers();
  create_context_memory ();

  Init_Motion_Search_Module ();

  information_init();
  searchwindow=calloc((2*input->search_range+1)*(2*input->search_range+1),1);
  // B pictures
  Bframe_ctr=0;
  img->refPicID=-1;  //WYK: Oct. 16, 2001
  img->refPicID_frm = -1;
  img->refPicID_fld = -1;
  tot_time=0;                 // time for total encoding session

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

  PatchInputNoFrames();
  img->pn = -1;    // Tian
  init_poc();

  // Write sequence header (with parameter sets)
  stat->bit_slice = start_sequence();
  start_frame_no_in_this_IGOP = 0;
  for (img->number=0; img->number < input->no_frames; img->number++)
  {
    img->disposable_flag = FALSE;
    img->pn = (img->pn+1) % (img->buf_cycle+1); 

    //much of this can go in init_frame() or init_field()?
    //poc for this frame or field
    img->toppoc = IMG_NUMBER*img->offset_for_ref_frame[0];
    if (input->InterlaceCodingOption==FRAME_CODING)
      img->bottompoc = img->toppoc;     //progressive
    else 
      img->bottompoc = img->toppoc+1;   //hard coded
    push_poc(img->toppoc, img->bottompoc, REFFRAME);               //push poc values into array
    
    //frame_num for this frame
    img->frame_num = IMG_NUMBER % (1 << (LOG2_MAX_FRAME_NUM_MINUS4 + 4));
    img->idr_flag = !IMG_NUMBER;            //for 2nd field, idr is reset in SliceHeader() //5.0f
    
    //the following is sent in the slice header
    img->delta_pic_order_cnt[0]=0;
    
    SetImgType();

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

      //about to code the last ref frame, adjust deltapoc         
      img->delta_pic_order_cnt[0]= -2*(initial_Bframes - input->successive_Bframe);
      img->toppoc += img->delta_pic_order_cnt[0];
      img->bottompoc += img->delta_pic_order_cnt[0];
      toprefpoc[0] = img->toppoc;                             //fix array values too
      bottomrefpoc[0] = img->bottompoc;
      
    }
#endif

    image_type = img->type;
    
    img->num_ref_pic_active_fwd_minus1 = max(0,img->nb_references - 1);
    num_ref_pic_bak          = img->num_ref_pic_active_fwd_minus1;
    if (input->StoredBPictures > 0)
    {
      if (input->InterlaceCodingOption>=MB_CODING)
      {
        img->num_ref_pic_active_bwd_minus1 = 1;
      }
      else
      {
        img->num_ref_pic_active_bwd_minus1 = 1;
      }
    }
    else
    {
      img->num_ref_pic_active_bwd_minus1 = 0;
    }
  
    // which layer the image belonged to?
    if ( IMG_NUMBER % (input->NumFramesInELSubSeq+1) == 0 )
      img->layer = 0;
    else
      img->layer = 1;



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


    img->nb_references += 1;
    img->nb_references = min(img->nb_references, img->buf_cycle +1); // Tian Dong. PLUS1, +1, June 7, 2002
    img->nb_references = min(img->nb_references, fb->short_used+fb->long_used);// Tian Dong. June 7, 2002

    if ((input->successive_Bframe != 0) && (IMG_NUMBER > 0)) // B-frame(s) to encode
    {
      img->type = B_IMG;            // set image type to B-frame
      img->num_ref_pic_active_bwd_minus1 = 0;

      img->types= INTER_IMG;

      if (input->NumFramesInELSubSeq == 0) 
        img->layer = 0;
      else 
        img->layer = 1;

      img->frame_num++;                 //increment frame_num once for B-frames
      img->frame_num %= (1 << (LOG2_MAX_FRAME_NUM_MINUS4 + 4));
      img->idr_flag=0;                                        //B-frames cannot be idr  //5.0f
      img->disposable_flag = TRUE;      // These B frames are disposable (are the non-disposable B frames implemented???)

      for(img->b_frame_to_code=1; img->b_frame_to_code<=input->successive_Bframe; img->b_frame_to_code++)
      {
        img->num_ref_pic_active_fwd_minus1 = num_ref_pic_bak;       // coding field pictures would have changed this, so reset

        //! somewhere here the disposable flag was set -- B frames are always disposable in this encoder.
        //! This happens now in slice.c, terminate_slice, where the nal_reference_idc is set up
        //poc for this B frame
        img->toppoc = (IMG_NUMBER-1)*img->offset_for_ref_frame[0] + 2* img->b_frame_to_code;
        if (input->InterlaceCodingOption==FRAME_CODING)
          img->bottompoc = img->toppoc;     //progressive
        else 
          img->bottompoc = img->toppoc+1;
        
        push_poc(img->toppoc,img->bottompoc, NONREFFRAME);      //push pocs for B frame
        
        //the following is sent in the slice header
        img->delta_pic_order_cnt[0]= 2*(img->b_frame_to_code-1);
        img->delta_pic_order_cnt[1]= 0;   // POC200301
        
        encode_one_frame();  // encode one B-frame
      }
    }

    process_2nd_IGOP();
  }
  // terminate sequence
  terminate_sequence();

  fclose(p_in);
  if (p_dec)
    fclose(p_dec);
  if (p_trace)
    fclose(p_trace);

  Clear_Motion_Search_Module ();

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

#ifdef _LEAKYBUCKET_
  calc_buffer();
#endif

  // report everything
  report();



  free_picture (frame_pic);
  if (top_pic)
    free_picture (top_pic);
  if (bottom_pic)
    free_picture (bottom_pic);

  // free allocated memory for frame buffers
  free_frame_buffers(input,img);
  free_global_buffers();

  // free image mem
  free_img ();
  free_context_memory ();
  FreeNalPayloadBuffer();
  free(  searchwindow);

  return 0;
}


/*!
 ***********************************************************************
 * \brief
 *    Initializes the POC structure with appropriate parameters.
 * 
 ***********************************************************************

⌨️ 快捷键说明

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