📄 lencod.c
字号:
/*
*****************************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2003, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
* The AVS Working Group doesn't represent or warrant that the programs
* furnished here under are free of infringement of any third-party patents.
* Commercial implementations of AVS, including shareware, may be
* subject to royalty fees to patent holders. Information regarding
* the AVS patent policy for standardization procedure is available at
* AVS Web site http://www.avs.org.cn. Patent Licensing is outside
* of AVS Working Group.
*
* The Original Code is Reference Software for China National Standard
* GB/T 20090.2-2006 (short for AVS-P2 or AVS Video) at version RM52J.
*
* The Initial Developer of the Original Code is Video subgroup of AVS
* Workinggroup (Audio and Video coding Standard Working Group of China).
* Contributors: Guoping Li, Siwei Ma, Jian Lou, Qiang Wang ,
* Jianwen Chen,Haiwu Zhao, Xiaozhen Zheng, Junhao Zheng, Zhiming Wang
*
******************************************************************************
*/
/*
*************************************************************************************
* File name:
* Function:
*
*************************************************************************************
*/
#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 "memalloc.h"
#include "mbuffer.h"
#include "image.h"
#include "header.h"
#include "ratectl.h"
#include "vlc.h" // Added by cjw, 20070327
#include "bitstream.h"
#ifdef FastME
#include "fast_me.h"
#endif
#define SM ""
#define VERSION "0.3"
InputParameters inputs, *input = &inputs;
ImageParameters images, *img = &images;
SNRParameters snrs, *snr = &snrs;
StatParameters stats,*stat=&stats;
int start_tr_in_this_IGOP = 0;
/*
*************************************************************************
* Function:Main function for encoder.
* Input:argc
number of command line arguments
argv
command line arguments
* Output:
* Return: exit code
* Attention:
*************************************************************************
*/
void Init_Motion_Search_Module ();
void Clear_Motion_Search_Module ();
int main(int argc,char **argv)
{
int image_type;
int M, N, np, nb, n;
int len=0; // Added by cjw, 20070327
Bitstream *bitstream = currBitStream; // Added by cjw, 20070327
int i;
p_dec = p_dec_u = p_dec_v = p_stat = p_log = p_datpart = p_trace = NULL;
Configure (argc, argv);
init_img();
#ifdef COREFRAME
//核心帧变量初始化
core_frame_enable_flag = input->coreframeflag;
log2_core_frame_buffer_size = input->log2coreframebufsize;
core_frame_buffer_size = (int) pow(2, log2_core_frame_buffer_size);
core_frame_period = input->coreframeperiod;
losspack_flag = input->losspack;
core_frame_num = 0;
core_frame_ref_num = 0;
img->prev_is_core_frame = 0;
for(i=0; i<16; i++)
{
core_reference_frame_flag[i] = 0;
}
#endif
frame_pic = malloc_picture();
if (input->InterlaceCodingOption != FRAME_CODING)
{
top_pic = malloc_picture();
bot_pic = malloc_picture();
}
init_rdopt ();
init_frame_buffers(input,img);
init_global_buffers();
Init_Motion_Search_Module ();
information_init();
//Rate control
if(input->RCEnable)
{
if (input->frame_rate_code==15)
{
printf("Rate control is not supported when frame rate is not limited!\n"); //added by qhf
exit(0);
}
rc_init_seq();
}
//FAST MOTION ESTIMATION. ZHIBO CHEN 2003.3
#ifdef FastME
DefineThreshold();
#endif
// B pictures
Bframe_ctr=0;
tot_time=0; // time for total encoding session
img->pn = -1;
input->skip_mode_flag = 1;
// Write sequence header
stat->bit_slice = start_sequence();
printf("Sequence Header \n");
img->Seqheader_flag=1; // Add by cjw, 20070327
// img->count_PAFF=0; // Commented by qihuafei, 20070925
img->curr_picture_distance = img->last_picture_distance = 0; // Added by Xiaozhen Zheng, 20070405
tmp_buf_cycle = img->buf_cycle;
for (img->number=0; img->number < input->no_frames; img->number++)
{
// Added by cjw, 20070327, Begin
if(img->number!=0 && input->vec_period!=0 && img->number%(input->vec_period*input->seqheader_period*input->intra_period)==0 ){
len= WriteVideoEditCode(); //added by cjw 20070419
stat->bit_slice += len;
}
if(img->number!=0 && input->seqheader_period!=0 && img->number%(input->seqheader_period*input->intra_period)==0){
stat->bit_slice += start_sequence();
printf("Sequence Header \n");
img->Seqheader_flag=1;
// img->count_PAFF=0; //Commented by qihuafei, 20070925
#ifdef COREFRAME
//核心帧初始化
//核心帧变量初始化
core_frame_enable_flag = input->coreframeflag;
log2_core_frame_buffer_size = input->log2coreframebufsize;
core_frame_buffer_size = (int) pow(2, log2_core_frame_buffer_size);
core_frame_period = input->coreframeperiod;
losspack_flag = input->losspack;
core_frame_num = 0;
core_frame_ref_num = 0;
img->prev_is_core_frame = 0;
I_P_num = 0;
for(i=0; i<16; i++)
{
core_reference_frame_flag[i] = 0;
}
#endif
}
// Added by cjw, 20070327, End
img->pn = (img->pn+1) % (img->buf_cycle+1);
img->buf_cycle = tmp_buf_cycle;
//frame_num for this frame
img->frame_num = IMG_NUMBER % (1 << (LOG2_MAX_FRAME_NUM_MINUS4 + 4));
SetImgType();
stat->bit_use_header[img->type] += len; // Added by cjw, 20070327
// if(img->number==0) //modified by cjw AVS 20070204 Spec. 7.2.3.1 //commented Hisilicon XiaoZhen Zheng 20070327
// picture_distance = 0;
// else
// {
// if(img->type==B_IMG)
// picture_distance = ((IMG_NUMBER - 1) * (input->jumpd + 1) + img->b_interval * img->b_frame_to_code)%256; // Tsinghua 200701
// else
// picture_distance = (IMG_NUMBER * (input->jumpd + 1))%256; // Tsinghua 200701
// }
image_type = img->type;
if(image_type == INTRA_IMG) // jlzheng 7.21
//img->buf_cycle /= 2;
img->buf_cycle = 1; // cjw 20060321
// for 1 reference is used field coding (I bottom only 1 reference is used)
//Rate control
if (img->type == INTRA_IMG)
{
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);
}
}
#ifdef COREFRAME
//核心帧
if(core_frame_enable_flag)
{
//I帧编码核心帧
if(I_P_num%core_frame_period ==0)
{
if(img->type == INTRA_IMG)
{
cur_core_frame_flag = 1;
core_frame_ref_num = 0;
img->nb_references = 1;
}
else
{
core_frame_ref_num = (core_frame_num+core_frame_buffer_size-1)%core_frame_buffer_size;
// 首先判断前一个核心帧可用否
//没有可用参考帧就不编码核心帧
if(!core_reference_frame_flag[core_frame_ref_num])
{
for(i=core_frame_num-1; i>=0; i--)
{
if((core_reference_frame_flag[i])&&(i!=core_frame_num)&&(core_reference_frame_distance[i]!=img->frame_num))
{
core_frame_ref_num = i;
cur_core_frame_flag =1;
img->nb_references = 1;
break;
}
}
if(!cur_core_frame_flag)
{
for(i=core_frame_buffer_size-1; i>(int)core_frame_num; i--)
{
if((core_reference_frame_flag[i])&&(i!=core_frame_num)&&(core_reference_frame_distance[i]!=img->frame_num))
{
core_frame_ref_num = i;
cur_core_frame_flag =1;
img->nb_references = 1;
break;
}
}
}
}
else
{
cur_core_frame_flag =1;
img->nb_references = 1;
}
}
}
}
else
{
cur_core_frame_flag = 0;
}
if(core_frame_enable_flag&&img->prev_is_core_frame)
{
img->nb_references = 1;
}
cur_core_frame_ok_flag = 1;
#endif
encode_one_frame(); // encode one I- or P-frame
img->nb_references += 1;
img->nb_references = min(img->nb_references, 2);// Tian Dong. June 7, 2002
#ifdef COREFRAME
//核心帧
if(core_frame_enable_flag)
{
if(cur_core_frame_flag)
{
core_reference_frame_flag[core_frame_num] = cur_core_frame_ok_flag;
core_frame_num++;
core_frame_num = core_frame_num%core_frame_buffer_size;
cur_core_frame_flag = 0;
img->prev_is_core_frame = 1;
}
else
{
img->prev_is_core_frame = 0;
}
}
I_P_num++;
#endif
if ((input->successive_Bframe != 0) && (IMG_NUMBER > 0)) // B-frame(s) to encode
{
img->type = B_IMG; // set image type to B-frame
#ifdef BACKGROUND
img->typeb = 0;
#endif
picture_coding_type = 1;
img->types = INTER_IMG;
//cjw for weighted prediction
img->buf_cycle = tmp_buf_cycle;
img->frame_num++; //increment frame_num once for B-frames
img->frame_num %= (1 << (LOG2_MAX_FRAME_NUM_MINUS4 + 4));
for(img->b_frame_to_code=1; img->b_frame_to_code<=input->successive_Bframe; img->b_frame_to_code++)
{
// picture_distance = (IMG_NUMBER - 1) * (input->successive_Bframe + 1) + img->b_frame_to_code; // Tsinghua 200701 //commented Hisilicon XiaoZhen Zheng 20070327
encode_one_frame(); // encode one B-frame
}
}
}
// terminate sequence
terminate_sequence();
fclose(p_in);
if (p_dec)
fclose(p_dec);
if (p_trace)
fclose(p_trace);
Clear_Motion_Search_Module ();
// free structure for rd-opt. mode decision
clear_rdopt ();
// report everything
report();
free_picture (frame_pic);
free_global_buffers();
// free image mem
free_img ();
return 0;
}
/*
*************************************************************************
* Function:Initializes the Image structure with appropriate parameters.
* Input:Input Parameters struct inp_par *inp
* Output:Image Parameters struct img_par *img
* Return:
* Attention:
*************************************************************************
*/
void init_img()
{
int i,j;
float FrameRate[8] = {{24000/1001}, {24}, {25}, {30000/1001}, {30}, {50}, {60000/1001}, {60}};
img->no_multpred=input->no_multpred;
img->buf_cycle = input->no_multpred;
img->lindex=0;
img->max_lindex=0;
img->width = (input->img_width+img->auto_crop_right); //add by wuzhongmou 0610
img->height = (input->img_height+img->auto_crop_bottom); //add by wuzhongmou 0610
img->width_cr =img->width/2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -