📄 lencod.c
字号:
/*!
***********************************************************************
* \mainpage
* This is the H.264/AVC encoder reference software. For detailed documentation
* see the comments in each file.
*
* \author
* The main contributors are listed in contributors.h
*
* \version
* JM 9.0 (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-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>
* - Valeri George <george@hhi.de>
* - Karsten Suehring <suehring@hhi.de>
* - Alexis Michael Tourapis <alexis@mobilygen.com>
***********************************************************************
*/
#include "contributors.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include "global.h"
#include "configfile.h"
#include "leaky_bucket.h"
#include "memalloc.h"
#include "intrarefresh.h"
#include "fmo.h"
#include "sei.h"
#include "parset.h"
#include "image.h"
#include "output.h"
#include "fast_me.h"
#include "ratectl.h"
#include "explicit_gop.h"
#define JM "9 (FRExt)"
#define VERSION "9.0"
#define EXT_VERSION "(FRExt)"
InputParameters inputs, *input = &inputs;
ImageParameters images, *img = &images;
StatParameters statistics, *stats = &statistics;
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;
int frame_statistic_start;
extern ColocatedParams *Co_located;
void Init_Motion_Search_Module ();
void Clear_Motion_Search_Module ();
void report_frame_statistic();
int M,N,n,np,nb; //Rate control
//add by yjy
/*!
***********************************************************************
* \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)
{
int8_t *pYUVBuffer, *pEncodedData;
int nOutLen;
H264_Init_Encoder();
for (img->number=0; img->number < input->no_frames; img->number++)
{
#if(0)
img->nal_reference_idc = 1;
//much of this can go in init_frame() or init_field()?
//poc for this frame or field
img->toppoc = (input->intra_period && input->idr_enable ? IMG_NUMBER % input->intra_period : IMG_NUMBER) * (2*(input->jumpd+1));
if ((input->PicInterlace==FRAME_CODING)&&(input->MbInterlace==FRAME_CODING))
img->bottompoc = img->toppoc; //progressive
else
img->bottompoc = img->toppoc+1; //hard coded
img->framepoc = min (img->toppoc, img->bottompoc);
//frame_num for this frame
//if (input->StoredBPictures == 0 || input->successive_Bframe == 0 || img-> number < 2)
if ((input->StoredBPictures == 0 && input->PyramidCoding == 0) || input->successive_Bframe == 0 || img-> number < 2 || input->PyramidCoding == 0)
img->frame_num = (input->intra_period && input->idr_enable ? IMG_NUMBER % input->intra_period : IMG_NUMBER) % (1 << (log2_max_frame_num_minus4 + 4));
else
{
img->frame_num ++;
if (input->intra_period && input->idr_enable)
{
if (0== (img->number % input->intra_period))
{
img->frame_num=0;
}
}
img->frame_num %= (1 << (log2_max_frame_num_minus4 + 4));
}
//the following is sent in the slice header
img->delta_pic_order_cnt[0]=0;
if (input->StoredBPictures)
{
if (img->number)
{
img->delta_pic_order_cnt[0]=+2 * input->successive_Bframe;
}
}
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];
}
#endif
//Rate control
if (img->type == I_SLICE)
{
if(input->RCEnable)
{
if (input->intra_period == 0)
{
n = input->no_frames + (input->no_frames - 1) * input->successive_Bframe;
/* number of P frames */
np = input->no_frames-1;
/* number of B frames */
nb = (input->no_frames - 1) * input->successive_Bframe;
}else
{
N = input->intra_period*(input->successive_Bframe+1);
M = input->successive_Bframe+1;
n = (img->number==0) ? N - ( M - 1) : N;
/* last GOP may contain less frames */
if(img->number/input->intra_period >= input->no_frames / input->intra_period)
{
if (img->number != 0)
n = (input->no_frames - img->number) + (input->no_frames - img->number - 1) * input->successive_Bframe + input->successive_Bframe;
else
n = input->no_frames + (input->no_frames - 1) * input->successive_Bframe;
}
/* number of P frames */
if (img->number == 0)
np = (n + 2 * (M - 1)) / M - 1; /* first GOP */
else
np = (n + (M - 1)) / M - 1;
/* number of B frames */
nb = n - np - 1;
}
rc_init_GOP(np,nb);
}
}
// 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
if (input->ReportFrameStats)
report_frame_statistic();
img->nb_references += 1;
img->nb_references = min(img->nb_references, img->buf_cycle); // Tian Dong. PLUS1, +1, June 7, 2002
encode_enhancement_layer();
#endif
H264_Encode_IP(pYUVBuffer, pEncodedData, &nOutLen);
H264_Encode_B(pYUVBuffer, pEncodedData, &nOutLen);
process_2nd_IGOP();
}
H264_Release_Encoder();
return 0; //encode JM73_FME version
}
/*!
***********************************************************************
* \brief
* Terminates and reports statistics on error.
*
***********************************************************************
*/
void report_stats_on_error()
{
input->no_frames=img->number-1;
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 (input->PyramidCoding)
clear_gop_structure ();
// free structure for rd-opt. mode decision
clear_rdopt ();
#ifdef _LEAKYBUCKET_
calc_buffer();
#endif
if (input->ReportFrameStats)
report_frame_statistic();
// report everything
report();
free_picture (frame_pic);
if (top_pic)
free_picture (top_pic);
if (bottom_pic)
free_picture (bottom_pic);
free_dpb();
free_collocated(Co_located);
uninit_out_buffer();
free_global_buffers();
// free image mem
free_img ();
free_context_memory ();
FreeNalPayloadBuffer();
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=input->pic_order_cnt_type;
img->delta_pic_order_always_zero_flag=0;
img->num_ref_frames_in_pic_order_cnt_cycle= 1;
if (input->StoredBPictures)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -