📄 image.c
字号:
/*!
*************************************************************************************
* \file image.c
*
* \brief
* Code one image/slice
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Inge Lille-Langoy <inge.lille-langoy@telenor.com>
* - Rickard Sjoberg <rickard.sjoberg@era.ericsson.se>
* - Jani Lainema <jani.lainema@nokia.com>
* - Sebastian Purreiter <sebastian.purreiter@mch.siemens.de>
* - 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>
* - Thomas Wedi <wedi@tnt.uni-hannover.de>
* - Ragip Kurceren <ragip.kurceren@nokia.com>
* - Antti Hallapuro <antti.hallapuro@nokia.com>
* - Alexis Michael Tourapis <alexismt@ieee.org>
*************************************************************************************
*/
#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>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include "global.h"
#include "refbuf.h"
#include "mbuffer.h"
#include "intrarefresh.h"
#include "fmo.h"
#include "sei.h"
#include "memalloc.h"
#include "nalu.h"
#include "ratectl.h"
#include "mb_access.h"
#include "output.h"
#include "cabac.h"
#include "image.h"
#ifdef ADAPTIVE_QUANTIZATION
#include "adaptive_quantization.h"
#endif
#ifdef ADAPTIVE_FILTER
#include "adaptive_filter.h"
#endif
#ifdef USE_POST_FILTER
#include "postprocessing.h"
#include "output.h"
#include <fcntl.h>
#endif
#ifdef MV_COMPETITION
#include "mv_competition.h"
// <FTRD Compatibility with APEC
extern MV_Competition mv_comp;
extern int ******send_index_mv_prediction;
extern int *****predModeMV;
// FTRD>
#endif
#ifdef SWITCHED_FILTERS
#include "switched_filters.h"
#endif // SWITCHED_FILTERS
extern pic_parameter_set_rbsp_t *PicParSet[MAXPPS];
void code_a_picture(Picture *pic);
void frame_picture (Picture *frame, int method);
void field_picture(Picture *top, Picture *bottom);
static int writeout_picture(Picture *pic);
static int picture_structure_decision(Picture *frame, Picture *top, Picture *bot);
static void distortion_fld (float *dis_fld_y, float *dis_fld_u, float *dis_fld_v);
static void find_snr(void);
#ifdef ADAPTIVE_QUANTIZATION
void find_distortion(void);
#else
static void find_distortion(void);
#endif
static void field_mode_buffer(int bit_field, float snr_field_y, float snr_field_u, float snr_field_v);
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);
static void init_field(void);
static void put_buffer_frame(void);
static void put_buffer_top(void);
static void put_buffer_bot(void);
static void copy_motion_vectors_MB(void);
#ifdef ADAPTIVE_FD_SD_CODING
static void store_motion_vectors_FDSD_coding (void);
static void restore_motion_vectors_FDSD_coding (void);
#endif
static void PaddAutoCropBorders (int org_size_x, int org_size_y, int img_size_x, int img_size_y,
int org_size_x_cr, int org_size_y_cr, int img_size_x_cr, int img_size_y_cr);
static void ReadOneFrame (int FrameNoInFile, int HeaderSize, int xs, int ys, int xs_cr, int ys_cr);
static void writeUnit(Bitstream* currStream ,int partition);
static void rdPictureCoding(void);
#ifdef _ADAPT_LAST_GROUP_
int *last_P_no;
int *last_P_no_frm;
int *last_P_no_fld;
#endif
static void ReportFirstframe(int tmp_time, int me_time);
static void ReportIntra(int tmp_time, int me_time);
static void ReportSP(int tmp_time, int me_time);
static void ReportP(int tmp_time, int me_time);
static void ReportB(int tmp_time, int me_time);
static void ReportNALNonVLCBits(int tmp_time, int me_time);
static int CalculateFrameNumber(void); // Calculates the next frame number
StorablePicture *enc_picture;
StorablePicture *enc_frame_picture;
StorablePicture *enc_frame_picture2;
StorablePicture *enc_frame_picture3;
StorablePicture *enc_top_picture;
StorablePicture *enc_bottom_picture;
#ifdef ADAPTIVE_FILTER
StorablePicture *enc_frame_picture_aif;
#endif
//Rate control
int QP;
const int ONE_FOURTH_TAP[3][2] =
{
{20,20},
{-5,-4},
{ 1, 0},
};
/*#ifdef USE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64.0 , 0.0 , 0.0 , 0.0
}, // d_pos
{ 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 , 0.0 ,
1.0/64.0 , -5.0/64.0 , 40.0/64.0 , 20.0/64.0 , -5.0/64.0 , 1.0/64.0 ,
0.0 , 0.0 , 20.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 , 0.0
}, // e_pos
{ 1.0/2048.0, -5.0/2048.0, 20.0/2048.0, 20.0/2048.0, -5.0/2048.0, 1.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -100.0/2048.0, -100.0/2048.0, 25.0/2048.0, -5.0/2048.0,
52.0/2048.0, -260.0/2048.0, 1040.0/2048.0, 1040.0/2048.0, -260.0/2048.0, 52.0/2048.0,
20.0/2048.0, -100.0/2048.0, 400.0/2048.0, 400.0/2048.0, -100.0/2048.0, 20.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -100.0/2048.0, -100.0/2048.0, 25.0/2048.0, -5.0/2048.0,
1.0/2048.0, -5.0/2048.0, 20.0/2048.0, 20.0/2048.0, -5.0/2048.0, 1.0/2048.0
}, // f_pos
{ 0.0 , 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 ,
1.0/64.0 , -5.0/64.0 , 20.0/64.0 , 40.0/64.0 , -5.0/64.0 , 1.0/64.0 ,
0.0 , 0.0 , 0.0 , 20.0/64.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0
}, // g_pos
{ 0.0 , 0.0 , 1.0/32.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/32.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 20.0/32.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 20.0/32.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/32.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 1.0/32.0 , 0.0 , 0.0 , 0.0
}, // h_pos
{ 1.0/2048.0, -5.0/2048.0, 52.0/2048.0, 20.0/2048.0, -5.0/2048.0, 1.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -260.0/2048.0, -100.0/2048.0, 25.0/2048.0, -5.0/2048.0,
20.0/2048.0, -100.0/2048.0, 1040.0/2048.0, 400.0/2048.0, -100.0/2048.0, 20.0/2048.0,
20.0/2048.0, -100.0/2048.0, 1040.0/2048.0, 400.0/2048.0, -100.0/2048.0, 20.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -260.0/2048.0, -100.0/2048.0, 25.0/2048.0, -5.0/2048.0,
1.0/2048.0, -5.0/2048.0, 52.0/2048.0, 20.0/2048.0, -5.0/2048.0, 1.0/2048.0
}, // i_pos
{ 1.0/1024.0, -5.0/1024.0, 20.0/1024.0, 20.0/1024.0, -5.0/1024.0, 1.0/1024.0,
-5.0/1024.0, 25.0/1024.0, -100.0/1024.0, -100.0/1024.0, 25.0/1024.0, -5.0/1024.0,
20.0/1024.0, -100.0/1024.0, 400.0/1024.0, 400.0/1024.0, -100.0/1024.0, 20.0/1024.0,
20.0/1024.0, -100.0/1024.0, 400.0/1024.0, 400.0/1024.0, -100.0/1024.0, 20.0/1024.0,
-5.0/1024.0, 25.0/1024.0, -100.0/1024.0, -100.0/1024.0, 25.0/1024.0, -5.0/1024.0,
1.0/1024.0, -5.0/1024.0, 20.0/1024.0, 20.0/1024.0, -5.0/1024.0, 1.0/1024.0
}, // j_pos
{ 1.0/2048.0, -5.0/2048.0, 20.0/2048.0, 52.0/2048.0, -5.0/2048.0, 1.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -100.0/2048.0, -260.0/2048.0, 25.0/2048.0, -5.0/2048.0,
20.0/2048.0, -100.0/2048.0, 400.0/2048.0, 1040.0/2048.0, -100.0/2048.0, 20.0/2048.0,
20.0/2048.0, -100.0/2048.0, 400.0/2048.0, 1040.0/2048.0, -100.0/2048.0, 20.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -100.0/2048.0, -260.0/2048.0, 25.0/2048.0, -5.0/2048.0,
1.0/2048.0, -5.0/2048.0, 20.0/2048.0, 52.0/2048.0, -5.0/2048.0, 1.0/2048.0
}, // k_pos
{ 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 20.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 52.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 , 0.0
}, // l_pos
{ 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 20.0/64.0 , 0.0 , 0.0 , 0.0 ,
1.0/64.0 , -5.0/64.0 , 40.0/64.0 , 20.0/64.0 , -5.0/64.0 , 1.0/64.0 ,
0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 , 0.0
}, // m_pos
{ 1.0/2048.0, -5.0/2048.0, 20.0/2048.0, 20.0/2048.0, -5.0/2048.0, 1.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -100.0/2048.0, -100.0/2048.0, 25.0/2048.0, -5.0/2048.0,
20.0/2048.0, -100.0/2048.0, 400.0/2048.0, 400.0/2048.0, -100.0/2048.0, 20.0/2048.0,
52.0/2048.0, -260.0/2048.0, 1040.0/2048.0, 1040.0/2048.0, -260.0/2048.0, 52.0/2048.0,
-5.0/2048.0, 25.0/2048.0, -100.0/2048.0, -100.0/2048.0, 25.0/2048.0, -5.0/2048.0,
1.0/2048.0, -5.0/2048.0, 20.0/2048.0, 20.0/2048.0, -5.0/2048.0, 1.0/2048.0
}, // n_pos
{ 0.0 , 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 0.0 , 20.0/64.0 , 0.0 , 0.0 ,
1.0/64.0 , -5.0/64.0 , 20.0/64.0, 40.0/64.0 , -5.0/64.0 , 1.0/64.0 ,
0.0 , 0.0 , 0.0 , -5.0/64.0 , 0.0 , 0.0 ,
0.0 , 0.0 , 0.0 , 1.0/64.0 , 0.0 , 0.0
} // o_pos
};
#endif*/
void MbAffPostProc(void)
{
imgpel temp[32][16];
imgpel ** imgY = enc_picture->imgY;
imgpel ***imgUV = enc_picture->imgUV;
int i, y, x0, y0, uv;
if (img->yuv_format != YUV400)
{
for (i=0; i<(int)img->PicSizeInMbs; i+=2)
{
if (enc_picture->mb_field[i])
{
get_mb_pos(i, &x0, &y0);
for (y=0; y<(2*MB_BLOCK_SIZE);y++)
memcpy(&temp[y],&imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
for (y=0; y<MB_BLOCK_SIZE;y++)
{
memcpy(&imgY[y0+(2*y)][x0],temp[y], MB_BLOCK_SIZE * sizeof(imgpel));
memcpy(&imgY[y0+(2*y + 1)][x0],temp[y+ MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
}
x0 = x0 / (16/img->mb_cr_size_x);
y0 = y0 / (16/img->mb_cr_size_y);
for (uv=0; uv<2; uv++)
{
for (y=0; y<(2*img->mb_cr_size_y);y++)
memcpy(&temp[y],&imgUV[uv][y0+y][x0], img->mb_cr_size_x * sizeof(imgpel));
for (y=0; y<img->mb_cr_size_y;y++)
{
memcpy(&imgUV[uv][y0+(2*y)][x0],temp[y], img->mb_cr_size_x * sizeof(imgpel));
memcpy(&imgUV[uv][y0+(2*y + 1)][x0],temp[y+ img->mb_cr_size_y], img->mb_cr_size_x * sizeof(imgpel));
}
}
}
}
}
else
{
for (i=0; i<(int)img->PicSizeInMbs; i+=2)
{
if (enc_picture->mb_field[i])
{
get_mb_pos(i, &x0, &y0);
for (y=0; y<(2*MB_BLOCK_SIZE);y++)
memcpy(&temp[y],&imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
for (y=0; y<MB_BLOCK_SIZE;y++)
{
memcpy(&imgY[y0+(2*y)][x0],temp[y], MB_BLOCK_SIZE * sizeof(imgpel));
memcpy(&imgY[y0+(2*y + 1)][x0],temp[y+ MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
}
}
}
}
}
/*!
************************************************************************
* \brief
* Encodes a picture
*
* This is the main picture coding loop.. It is called by all this
* frame and field coding stuff after the img-> elements have been
* set up. Not sure whether it is useful for MB-adaptive frame/field
* coding
************************************************************************
*/
void code_a_picture(Picture *pic)
{
unsigned int NumberOfCodedMBs = 0;
int SliceGroup = 0;
int j;
img->currentPicture = pic;
img->currentPicture->idr_flag = ((!IMG_NUMBER) && (!(img->structure==BOTTOM_FIELD))) || (input->idr_enable && (img->type == I_SLICE || img->type==SI_SLICE)&& (!(img->structure==BOTTOM_FIELD)));
pic->no_slices = 0;
pic->distortion_u = pic->distortion_v = pic->distortion_y = 0.0;
RandomIntraNewPicture (); //! Allocates forced INTRA MBs (even for fields!)
// The slice_group_change_cycle can be changed here.
// FmoInit() is called before coding each picture, frame or field
img->slice_group_change_cycle=1;
FmoInit(img, active_pps, active_sps);
FmoStartPicture (); //! picture level initialization of FMO
CalculateQuantParam();
CalculateOffsetParam();
if(input->Transform8x8Mode)
{
CalculateQuant8Param();
CalculateOffset8Param();
}
reset_pic_bin_count();
img->bytes_in_picture = 0;
while (NumberOfCodedMBs < img->PicSizeInMbs) // loop over slices
{
// Encode one SLice Group
while (!FmoSliceGroupCompletelyCoded (SliceGroup))
{
// Encode the current slice
NumberOfCodedMBs += encode_one_slice (SliceGroup, pic, NumberOfCodedMBs);
FmoSetLastMacroblockInSlice (img->current_mb_nr);
// Proceed to next slice
img->current_slice_nr++;
stats->bit_slice = 0;
}
// Proceed to next SliceGroup
SliceGroup++;
}
FmoEndPicture ();
// Modified for Fast Mode Decision. Inchoon Choi, SungKyunKwan Univ.
if (input->rdopt == 3 && (img->type != B_SLICE))
for (j = 0; j < input->NoOfDecoders; j++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -