📄 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
int8_t *yuv_buffer=NULL, *encoded_data=NULL;
int * out_len;
extern int CalculateFrameNumber();
/*!
***********************************************************************
* \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)
{
USERDATA UserData;
int nFrameSize;
int FrameNumberInFile;
int nOutLen;
int8_t * pYUVBuffer = NULL, *pEncodedData = NULL;
FILE * fpOutFile = NULL;
int nCounter=0, nBits=0;
pEncodedData = (int8_t *) malloc(352*288*2);
if(pEncodedData==NULL)
{
printf("memory allocation failed\n");
//goto termination;
}
fpOutFile = fopen("test.264", "wb");
if(fpOutFile==NULL)
{
printf("open file allocation failed\n");
//goto termination;
}
H264_Init_Encoder(NULL, &UserData, pEncodedData, &nOutLen);
nFrameSize = img->width*img->height*3/2;
fwrite(pEncodedData, 1, nOutLen, fpOutFile);
pYUVBuffer = (int8_t *) malloc(img->width*img->height*3/2);
if(pYUVBuffer==NULL)
{
printf("memory allocation failed\n");
//goto termination;
}
for (img->number=0; img->number < input->no_frames; img->number++)
{
FrameNumberInFile = CalculateFrameNumber();
// seek to current frame
if (lseek (p_in, (nFrameSize)* FrameNumberInFile, SEEK_SET) == -1)
{
//snprintf(errortext, ET_SIZE, "ReadOneFrame: cannot advance file pointer in p_in beyond frame %d\n", input->start_frame +FrameNumberInFile);
//error (errortext,-1);
}
if (read(p_in, pYUVBuffer, nFrameSize) != nFrameSize)
{
printf("read file failed\n");
}
H264_Encode_IP(pYUVBuffer, pEncodedData, &nOutLen);
//printf("my stat: QP %d, PSNR %.3f, buffer:%.3f, bitrate: %.3f\n", img->qp, snr->snr_y, CurrentBufferFullness, bit_rate);
fwrite(pEncodedData, 1, nOutLen, fpOutFile);
nBits += nOutLen;
if((++nCounter)%25==0)
{
printf("bit rate is: %d\n", nBits*8);
nBits = 0;
}
// printf("b_interval:%f\n",img->b_interval);
for(img->b_frame_to_code=1; img->b_frame_to_code<=input->successive_Bframe; img->b_frame_to_code++)
{
FrameNumberInFile = CalculateFrameNumber();
// seek to current frame
if (lseek (p_in, nFrameSize* FrameNumberInFile, SEEK_SET) == -1)
{
//snprintf(errortext, ET_SIZE, "ReadOneFrame: cannot advance file pointer in p_in beyond frame %d\n", input->start_frame +FrameNumberInFile);
//error (errortext,-1);
}
if (read(p_in, pYUVBuffer, nFrameSize) != nFrameSize)
{
printf("read file failed\n");
}
H264_Encode_B(pYUVBuffer, pEncodedData, &nOutLen);
if(nOutLen)
{
fwrite(pEncodedData, 1, nOutLen, fpOutFile);
//printf("my stat: QP %d, PSNR %.3f, buffer:%.3f, bitrate: %.3f\n", img->qp, snr->snr_y, CurrentBufferFullness, bit_rate);
nBits += nOutLen;
if((++nCounter)%25==0)
{
printf("bit rate is: %d\n", nBits*8);
nBits = 0;
}
}
}
img->b_frame_to_code=0;
process_2nd_IGOP();
}
termination:
if(pYUVBuffer)
free(pYUVBuffer);
if(pEncodedData)
free(pEncodedData);
if(fpOutFile)
fclose(fpOutFile);
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)
{
img->offset_for_non_ref_pic = 0;
img->offset_for_ref_frame[0] = 2;
}
else
{
img->offset_for_non_ref_pic = -2*(input->successive_Bframe);
img->offset_for_ref_frame[0] = 2*(input->successive_Bframe+1);
}
if ((input->PicInterlace==FRAME_CODING)&&(input->MbInterlace==FRAME_CODING))
img->offset_for_top_to_bottom_field=0;
else
img->offset_for_top_to_bottom_field=1;
if ((input->PicInterlace==FRAME_CODING)&&(input->MbInterlace==FRAME_CODING))
{
img->pic_order_present_flag=0;
img->delta_pic_order_cnt_bottom = 0;
}
else
{
img->pic_order_present_flag=1;
img->delta_pic_order_cnt_bottom = 1;
}
}
/*!
***********************************************************************
* \brief
* Initializes the img->nz_coeff
* \par Input:
* none
* \par Output:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -