?? 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 RM "5"
#define VERSION "5.2j"
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
p_dec = p_dec_u = p_dec_v = p_stat = p_log = p_datpart = p_trace = NULL;
Configure (argc, argv);
init_img();
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)
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; // Add by cjw, 20070327
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;
}
// 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);
}
}
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
if ((input->successive_Bframe != 0) && (IMG_NUMBER > 0)) // B-frame(s) to encode
{
img->type = B_IMG; // set image type to B-frame
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;
img->height_cr= img->height/2; //add by wuzhongmou 0610
img->framerate = (int)FrameRate[input->frame_rate_code-1];
//img->framerate=INIT_FRAME_RATE; // The basic frame rate (of the original sequence)
if(input->InterlaceCodingOption != FRAME_CODING)
img->buf_cycle *= 2;
get_mem_mv (&(img->mv));
get_mem_mv (&(img->p_fwMV));
get_mem_mv (&(img->p_bwMV));
get_mem_mv (&(img->all_mv));
get_mem_mv (&(img->all_bmv));
get_mem_ACcoeff (&(img->cofAC));
get_mem_DCcoeff (&(img->cofDC));
get_mem_mv (&(img->omv));
get_mem_mv (&(img->all_omv));
get_mem_mv (&(img->omv_fld));
get_mem_mv (&(img->all_omv_fld));
/*Lou Start*/
get_mem4Dint(&(img->chromacofAC),2,4,2,17);
/*Lou End*/
/*lgp*/
if(input->InterlaceCodingOption != FRAME_CODING)
img->buf_cycle /= 2;
if ((img->MB_SyntaxElements = (SyntaxElement*)calloc (((img->height*img->width/256)+1200), sizeof(SyntaxElement))) == NULL)
no_mem_exit ("init_img: MB_SyntaxElements");
if ((img->quad = (int*)calloc (511, sizeof(int))) == NULL)
no_mem_exit ("init_img: img->quad");
img->quad+=255;
for (i=0; i < 256; ++i) // fix from TML1 / TML2 sw, truncation removed
{
img->quad[i]=img->quad[-i]=i*i;
}
if(((img->mb_data) = (Macroblock *) calloc((img->width/MB_BLOCK_SIZE) * (img->height/MB_BLOCK_SIZE),sizeof(Macroblock))) == NULL)
no_mem_exit("init_img: img->mb_data");
for (i=0; i < (img->width/MB_BLOCK_SIZE) * (img->height/MB_BLOCK_SIZE); i++)
{
get_mem4Dint (&(img->mb_data[i].cofAC),6,4,2,65);/*lgp*dct*modify*/
get_mem4Dint(&(img->mb_data[i].chromacofAC),2,4,2,17);
}
for (i=0; i < (img->width/MB_BLOCK_SIZE) * (img->height/MB_BLOCK_SIZE); i++)
{
img->mb_data[i].slice_nr = 0;
}
// allocate memory for intra pred mode buffer for each block: img->ipredmode
get_mem2Dint(&(img->ipredmode), img->width/B8_SIZE+100, img->height/B8_SIZE+100); //need two extra rows at right and bottom
// Prediction mode is set to -1 outside the frame, indicating that no prediction can be made from this part
for (i=0; i < img->width/(B8_SIZE)+100; i++)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -