📄 image.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 <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
#include <string.h>
#include <memory.h>
#include <assert.h>
#include "global.h"
#include "image.h"
#include "refbuf.h"
#include "mbuffer.h"
#include "header.h"
#include "memalloc.h"
#include "bitstream.h"
#include "ratectl.h"
#include "vlc.h"
#include "configfile.h"
#define TO_SAVE 4711
#define FROM_SAVE 4712
#define Clip(min,max,val) (((val)<(min))?(min):(((val)>(max))?(max):(val)))
static void code_a_picture(Picture *frame);
static void ReadOneFrame (int FrameNoInFile, int HeaderSize, int xs, int ys);
static void write_reconstructed_image();
static int writeout_picture();
static int writeout_slice();
static void find_snr();
static void frame_mode_buffer (int bit_frame, float snr_frame_y, float snr_frame_u, float snr_frame_v);
static void init_frame();
void init_field ();
void top_field(Picture *pic);
void bot_field(Picture *pic);
void combine_field();
int terminate_picture();
void put_buffer_frame();
void put_buffer_top();
void put_buffer_bot();
void interpolate_frame_to_fb();
static void CopyFrameToOldImgOrgVariables ();
static void UnifiedOneForthPix (pel_t ** imgY, pel_t ** imgU, pel_t ** imgV,
pel_t ** out4Y);
static void ReportFirstframe(int tmp_time);
static void ReportIntra(int tmp_time);
static void ReportP(int tmp_time);
static void ReportB(int tmp_time);
static int CalculateFrameNumber(); // Calculates the next frame number
static int FrameNumberInFile; // The current frame number in the input file
// !! weighting prediction
// The two fuctions below can be rewritten by other algorithms
// Determine the parameters of weighting prediction
void estimate_weighting_factor();
//cjw 20051219 weighting prediction for field
void estimate_weighting_factor_field();
#define IClip( Min, Max, Val) (((Val)<(Min))? (Min):(((Val)>(Max))? (Max):(Val)))
//Rate control
int QP;
extern SiceSetRegion *sliceregion; //X ZHENG, 2008.04
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void stuffing_byte(int n)
{
int i;
Bitstream *currStream;
currStream = currBitStream;
for(i=0; i<n; i++)
{
currStream->streamBuffer[currStream->byte_pos++] = 0x80;
currStream->bits_to_go = 8;
currStream->byte_buf = 0;
}
}
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
static void picture_header()
{
int len=0;
Bitstream *bitstream = currBitStream;
img->cod_counter = 0;
if(img->type==INTRA_IMG)
{
len =len+IPictureHeader(img->number);
if((input->InterlaceCodingOption == FRAME_CODING)||(input->InterlaceCodingOption == FIELD_CODING)) //add by wuzhongmou
img->count=img->count+2; //add by wuzhongmou
// the following two lines were deleted by qihuafei, 20070925
// if((input->InterlaceCodingOption == PAFF_CODING))
// img->count=img->count+1; //add by wuzhongmou
}
else
len = len+PBPictureHeader();
#ifdef REGION
if( input->RegionPresentFlag )
{
len += WriteRegionExtension();
}
#endif
// Rate control
img->NumberofHeaderBits +=len;
if(img->BasicUnit<img->Frame_Total_Number_MB)
img->NumberofBasicUnitHeaderBits +=len;
// Update statistics
stat->bit_slice += len;
stat->bit_use_header[img->type] += len;
img->ptype =img->type;
WriteFrameFieldMBInHeader = 0;
}
/*
*************************************************************************
* Function:Encodes one frame
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
int encode_one_frame ()
{
static int prev_frame_no = 0; // POC200301
static int consecutive_non_reference_pictures = 0; // POC200301
// int i;
time_t ltime1;
time_t ltime2;
#ifdef WIN32
struct _timeb tstruct1;
struct _timeb tstruct2;
#else
struct timeb tstruct1;
struct timeb tstruct2;
#endif
int tmp_time;
int bits_frm = 0, bits_fld = 0;
float dis_frm = 0, dis_frm_y = 0, dis_frm_u = 0, dis_frm_v = 0;
float dis_fld = 0, dis_fld_y = 0, dis_fld_u = 0, dis_fld_v = 0;
// int framesize;
//Rate control
int pic_type, bits = 0;
//cjw
second_IField=0;
#ifdef WIN32
_ftime (&tstruct1); // start time ms
#else
ftime (&tstruct1);
#endif
time (<ime1); // start time s
init_frame (); // initial frame variables
FrameNumberInFile = CalculateFrameNumber();
ReadOneFrame (FrameNumberInFile, input->infile_header, input->img_width, input->img_height); //modify by wuzhongmou 0610
CopyFrameToOldImgOrgVariables();
current_slice_bytepos=0; //qhg 20060327 for de-emulation
img->types = img->type;
//Rate control
img->FieldControl=0;
if(input->RCEnable)
{
/*update the number of MBs in the basic unit for MB adaptive
f/f coding*/
img->BasicUnit=input->basicunit;
rc_init_pict(1,0,1);
img->qp = updateQuantizationParameter(0);
pic_type = img->type;
QP =0;
}
#ifdef COREFRAME
//核心帧 用于核心帧之前的B帧编码skip和direct模式的块用
if(cur_core_frame_flag == 1)
{
img->preserve_imgtr_last_P_frm = img->imgtr_last_P_frm;
img->imgtr_last_P_frm = core_reference_frame_distance[core_frame_ref_num];
img->img_next_is_core_frame = 1;
img->img_next_core_frame_ref_distance = core_reference_frame_distance[core_frame_ref_num];
core_reference_frame_distance[core_frame_num] = picture_distance;
}
else if(img->type!=B_IMG)
{
img->img_next_is_core_frame = 0;
}
#endif
if (input->InterlaceCodingOption != FIELD_CODING) // !! frame coding
{
put_buffer_frame (); //initialize frame buffer
//frame picture
if(input->progressive_frame) //add by wuzhongmou 0610
{
img->progressive_frame = 1;
img->picture_structure = 1;
}else
{
img->progressive_frame = 0;
img->picture_structure = 1;
} //add by wuzhongmou 0610
if (img->type == B_IMG)
Bframe_ctr++; // Bframe_ctr only used for statistics, should go to stat->
singlefactor =(float) ((Bframe_ctr & 0x01) ? 2 : 0.5);
// !! [1/5/2004]
// !! calculate the weighting parameter
//cjw 20051219
img->LumVarFlag = 0 ; // !! default : no weighting prediction
img->mb_weighting_flag = 1 ;
//cjw 20051219
if(img->type != INTRA_IMG && input->slice_weighting_flag == 1){
estimate_weighting_factor();
}
// !! [1/5/2004]
if(input->slice_set_enable) //X ZHENG, 2008.04
init_slice_set(0);
code_a_picture (frame_pic);
if (img->type!=B_IMG)
Update_Picture_Buffers();
stat->bit_ctr_emulationprevention += stat->em_prev_bits_frm;
frame_mode_buffer (bits_frm, dis_frm_y, dis_frm_u, dis_frm_v);
#ifdef COREFRAME
//核心帧
if(cur_core_frame_flag&&(img->type!=B_IMG))
{
//
memcpy(&core_frame_mref_frm[core_frame_num][0][0], &mref[0][0][0], (img->height+2*IMG_PAD_SIZE)*4*(img->width+2*IMG_PAD_SIZE)*4);
memcpy(&core_reference_frame[core_frame_num][0][0][0], &ref_frm[0][0][0][0], img->height*img->width);
memcpy(&core_reference_frame[core_frame_num][1][0][0], &ref_frm[0][1][0][0], img->height*img->width/4);
memcpy(&core_reference_frame[core_frame_num][2][0][0], &ref_frm[0][2][0][0], img->height*img->width/4);
}
#endif
}
if (input->InterlaceCodingOption != FRAME_CODING) // !! field coding
{
int i;
//Rate control
int old_pic_type; // picture type of top field used for rate control
int TopFieldBits;
//Rate control
old_pic_type = img->type;
img->FieldControl=1;
// Commented by qihuafei, 20070926
// if(input->InterlaceCodingOption==FIELD_CODING) //this condition is always true without PAFF
// img->FieldFrame = 0;// [5/8/2007 Leeswan]
img->FieldFrame = 0;// [5/8/2007 Leeswan]
if(input->RCEnable)
{
img->BasicUnit=input->basicunit;
//Commented by qihuafei, 20070926
/* if(input->InterlaceCodingOption==1) //this condition is always true without PAFF
rc_init_pict(0,1,1);
else
rc_init_pict(0,1,0);
*/
rc_init_pict(0,1,1);
img->qp = updateQuantizationParameter(1);
}
img->TopFieldFlag=1;// [5/8/2007 Leeswan]
singlefactor = 1;
//non frame picture
if(input->progressive_frame) //add by wuzhongmou 0610
{
error ("invalid match of Progressive_frame and InterlaceCodingOption ",100);
}else
{
img->progressive_frame = 0;
img->picture_structure = 0;
}
//resize picture size, reference number for field
img->height >>= 1;
img->height_cr >>= 1;
img->nb_references = img->nb_references*2; // coded available reference number
//Commented by qihuafei, 20070926
/* if (input->InterlaceCodingOption == FIELD_CODING) //this condition is always true without PAFF
{
AllocateBitstream(); //start bit stream for field coding
if (img->type == B_IMG)
Bframe_ctr++; // Bframe_ctr only used for statistics, should go to stat->
}
else
{
// save coded bitstream from frame coding
pic_buf = (byte *) calloc(img->width*img->height*4,1);
memcpy(pic_buf,currBitStream->streamBuffer,currBitStream->byte_pos);
framesize = currBitStream->byte_pos;
memset(currBitStream,0,sizeof(currBitStream));
currBitStream->bits_to_go = 8;
}
*/
AllocateBitstream(); //start bit stream for field coding
if (img->type == B_IMG)
Bframe_ctr++; // Bframe_ctr only used for statistics, should go to stat->
//coded picture header
picture_header();
img->buf_cycle <<=1; // restrict maximum reference numuber
img->old_type = img->type;
//initialize buffer
put_buffer_top();
// !! start [12/28/2005] shenyanfei cjw
//cjw 20051219
img->LumVarFlag = 0 ; // !! default : no weighting prediction
img->mb_weighting_flag = 1 ;
//cjw 20051219
if(img->type != INTRA_IMG && input->slice_weighting_flag == 1){
estimate_weighting_factor_field();
}
// !! end [12/28/2005] shenyanfei cjw
init_field();
if(input->slice_set_enable) //X ZHENG, 2008.04
init_slice_set(1);
top_field(top_pic);
//Rate control
TopFieldBits=top_pic->bits_per_picture;
if (img->type != B_IMG)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -