📄 slice.c
字号:
/*!
**************************************************************************************
* \file
* slice.c
* \brief
* generate the slice header, setup the bit buffer for slices,
* and generates the slice NALU(s)
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Thomas Stockhammer <stockhammer@ei.tum.de>
* - Detlev Marpe <marpe@hhi.de>
* - Stephan Wenger <stewe@cs.tu-berlin.de>
* - Alexis Michael Tourapis <alexismt@ieee.org>
***************************************************************************************
*/
#include "contributors.h"
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include "global.h"
#include "header.h"
#include "rtp.h"
#include "fmo.h"
#include "vlc.h"
#include "image.h"
#include "cabac.h"
#include "elements.h"
#include "epzs.h"
#ifdef ADAPTIVE_FILTER
#include "adaptive_filter.h"
#endif
#ifdef ADAPTIVE_FD_SD_CODING
#include "rdopt_coding_state.h"
CSptr cs_slice_coding = NULL;
CSptr cs_slice_coding1 = NULL;
#endif
#ifdef MV_COMPETITION
#include "mv_competition.h"
#endif
#ifdef ADAPTIVE_QUANTIZATION
#include "adaptive_quantization.h"
void InitAQMSStatParamForSlice();
void encode_one_macroblock_for_iaqms(int CurrentMbAddr, double *min_rdcost, int *best_iaqms_idx, int *saved_best_mode, int *best_QP, int *best_deltaQP, int *best_athr, int *best_cbp, int masterQP, int deltaQP);
int internal_param[10];
void init_internal_parameter(int CurrentMbAddr, int *best_iaqms_idx, int *saved_best_mode, int *best_QP, int *best_deltaQP, int *best_athr, int *best_cbp);
void set_internal_parameter();
void decide_last_coeff(int CurrentMbAddr, double *min_rdcost, int *best_iaqms_idx, int *saved_best_mode, int *best_QP, int *best_deltaQP, int *best_athr, int *best_cbp, int masterQP, int deltaQP);
#endif
#if (defined(ADAPTIVE_QUANTIZATION) || defined(RDO_Q))
void encode_one_macroblock_each_quant(int CurrentMbAddr);
#endif
#ifdef USE_INTRA_MDDT
#include <memory.h>
static void InitScanOrderForSlice();
extern void precompute_all_inner_product8x8();
extern void precompute_all_inner_product16x16();
#endif
#ifdef SWITCHED_FILTERS
#include "switched_filters.h"
#endif // SWITCHED_FILTERS
// Local declarations
static Slice *malloc_slice();
static void free_slice(Slice *slice);
static void init_slice(int start_mb_addr);
static void set_ref_pic_num();
extern ColocatedParams *Co_located;
extern StorablePicture **listX[6];
void poc_ref_pic_reorder(StorablePicture **list, unsigned num_ref_idx_lX_active, int *reordering_of_pic_nums_idc, int *abs_diff_pic_num_minus1, int *long_term_pic_idx, int weighted_prediction, int list_no);
void SetLagrangianMultipliers();
#ifdef RDO_Q
extern short best_mode;
#endif
/*!
************************************************************************
* \brief
* init_ref_pic_list_reordering initializations should go here
************************************************************************
*/
void init_ref_pic_list_reordering()
{
Slice* currSlice = img->currentSlice;
currSlice->ref_pic_list_reordering_flag_l0 = 0;
currSlice->ref_pic_list_reordering_flag_l1 = 0;
}
/*!
************************************************************************
* \brief
* This function generates the slice (and partition) header(s)
*
* \return number of bits used for the slice (and partition) header(s)
*
* \par Side effects:
* Adds slice/partition header symbols to the symbol buffer
* increments Picture->no_slices, allocates memory for the
* slice, sets img->currSlice
************************************************************************
*/
int start_slice()
{
EncodingEnvironmentPtr eep;
Slice *currSlice = img->currentSlice;
Bitstream *currStream;
int header_len = 0;
int i;
int NumberOfPartitions = (input->partition_mode == PAR_DP_1?1:3);
//one partition for IDR img
if(img->currentPicture->idr_flag)
{
NumberOfPartitions = 1;
}
RTPUpdateTimestamp (img->tr); // this has no side effects, just leave it for all NALs
for (i=0; i<NumberOfPartitions; i++)
{
currStream = (currSlice->partArr[i]).bitstream;
currStream->write_flag = 0;
if (i==0) // First partition
header_len += SliceHeader (0);
else // Second/Third partition
header_len += Partition_BC_Header(i);
//! Initialize CABAC
if (input->symbol_mode == CABAC)
{
eep = &((currSlice->partArr[i]).ee_cabac);
if (currStream->bits_to_go != 8)
header_len+=currStream->bits_to_go;
writeVlcByteAlign(currStream);
arienco_start_encoding(eep, currStream->streamBuffer, &(currStream->byte_pos));
cabac_new_slice();
}
else
{
// Initialize CA-VLC
CAVLC_init();
}
}
if(input->symbol_mode == CABAC)
{
init_contexts();
}
return header_len;
}
/*!
************************************************************************
* \brief
* This function terminates a slice (but doesn't write it out),
* the old terminate_slice (0)
* \return
* 0 if OK, \n
* 1 in case of error
*
************************************************************************
*/
int terminate_slice(int lastslice)
{
static int MbWidthC [4]= { 0, 8, 8, 16};
static int MbHeightC [4]= { 0, 8, 16, 16};
int bytes_written;
Bitstream *currStream;
Slice *currSlice = img->currentSlice;
EncodingEnvironmentPtr eep;
int i;
int byte_pos_before_startcode_emu_prevention;
int min_num_bytes=0;
int stuffing_bytes=0;
int RawMbBits;
if (input->symbol_mode == CABAC)
write_terminating_bit (1); // only once, not for all partitions
for (i=0; i<currSlice->max_part_nr; i++)
{
currStream = (currSlice->partArr[i]).bitstream;
if (input->symbol_mode == UVLC)
{
SODBtoRBSP(currStream);
byte_pos_before_startcode_emu_prevention = currStream->byte_pos;
currStream->byte_pos = RBSPtoEBSP(currStream->streamBuffer, 0 , currStream->byte_pos, 0);
*(stats->em_prev_bits) += (currStream->byte_pos - byte_pos_before_startcode_emu_prevention) * 8;
}
else // CABAC
{
eep = &((currSlice->partArr[i]).ee_cabac);
// terminate the arithmetic code
arienco_done_encoding(eep);
currStream->bits_to_go = eep->Ebits_to_go;
currStream->byte_buf = 0;
bytes_written = currStream->byte_pos;
img->bytes_in_picture += currStream->byte_pos;
byte_pos_before_startcode_emu_prevention= currStream->byte_pos;
if (lastslice && i==((currSlice->max_part_nr-1)))
{
RawMbBits = 256 * img->bitdepth_luma + 2 * MbWidthC[active_sps->chroma_format_idc] * MbHeightC[active_sps->chroma_format_idc] * img->bitdepth_chroma;
min_num_bytes = ((96 * get_pic_bin_count()) - (RawMbBits * (int)img->PicSizeInMbs *3) + 1023) / 1024;
if (min_num_bytes>img->bytes_in_picture)
{
stuffing_bytes = min_num_bytes - img->bytes_in_picture;
printf ("CABAC stuffing words = %6d\n", stuffing_bytes/3);
}
}
// printf ("bytepos: %d\n", currStream->byte_pos);
currStream->byte_pos = RBSPtoEBSP(currStream->streamBuffer, 0, currStream->byte_pos, currStream->byte_pos + stuffing_bytes);
*(stats->em_prev_bits) += (currStream->byte_pos - byte_pos_before_startcode_emu_prevention) * 8;
} // CABAC
} // partition loop
if( input->symbol_mode == CABAC )
{
store_contexts();
}
if (img->type != I_SLICE || img->type != SI_SLICE)
free_ref_pic_list_reordering_buffer (currSlice);
return 0;
}
/*!
************************************************************************
* \brief
* Encodes one slice
* \par
* returns the number of coded MBs in the SLice
************************************************************************
*/
int encode_one_slice (int SliceGroupId, Picture *pic, int TotalCodedMBs)
{
Boolean end_of_slice = FALSE;
Boolean recode_macroblock;
int len;
int NumberOfCodedMBs = 0;
int CurrentMbAddr;
double FrameRDCost = DBL_MAX, FieldRDCost = DBL_MAX;
#ifdef RDO_Q
int masterQP=0, deltaQP, bestQP;
double min_rdcost=1e30;
int qp_left, qp_up;
int best_deltaQP=0;
#endif
#ifdef ADAPTIVE_FD_SD_CODING
int i,j;
int slice_bits;
int rate_only_FD;
int rate_FD_and_SD;
int SSD_only_FD;
int SSD_FD_and_SD;
double rd_cost_only_FD;
double rd_cost_FD_and_SD;
Macroblock* currMB;
#endif
#ifdef ADAPTIVE_QUANTIZATION
int best_iaqms_idx = 0;
int best_cbp = 47;
int best_athr = 0;
#endif
#ifdef USE_NEW_OFFSET
if(input->UseNewOffset)
ResetFrameOffset();
#endif
#ifdef ADAPTIVE_FILTER
if(img->AdaptiveFilterFlag == 0)
ResetAdaptiveFilter();
else if (img->AdaptiveFilterFlag == 1)
{
if (input->UseAdaptiveFilter == 1)
UnifiedOneForthPixWithNewFilter();
else if (input->UseAdaptiveFilter == 2)
UnifiedOneForthPixWithNewFilterVer(); // separable aif
#ifdef DIRECTIONAL_FILTER
else if (input->UseAdaptiveFilter == 3)
{
if (input->ImpType == IMP_FLOAT32)
UnifiedOneForthPixWith_1DAIF_float();
else if (input->ImpType == IMP_INT16)
UnifiedOneForthPixWith_1DAIF_int16();
}
#ifdef E_DAIF
else if (input->UseAdaptiveFilter == FILTER_TYPE_EDAIF)
{
UnifiedOneForthPixWithNewFilter();
}
#endif // E_DAIF
#endif // DIRECTIONAL_FILTER
SwapUpsampledFrames();
}
#endif
#ifdef ADAPTIVE_QUANTIZATION
InitAQMSStatParamForSlice();
#endif
#ifdef ADAPTIVE_FD_SD_CODING
rdopt_FDSD=&rddata_FDSD_coding;
rdopt_FDSD_interlace=&rddata_FDSD_coding_interlace;
#endif
img->cod_counter = 0;
#ifdef USE_INTRA_MDDT
if(input->UseIntraMDDT)
{
InitScanOrderForSlice();
precompute_all_inner_product8x8();
precompute_all_inner_product16x16();
}
#endif
CurrentMbAddr = FmoGetFirstMacroblockInSlice (SliceGroupId);
// printf ("\n\nEncode_one_slice: PictureID %d SliceGroupId %d SliceID %d FirstMB %d \n", img->tr, SliceGroupId, img->current_slice_nr, CurrentMbInScanOrder);
init_slice (CurrentMbAddr);
// <FTRD : Compatibility with hierarchical B slices
#ifdef MV_COMPETITION
if(input->mv_competition > 0)
{
enc_picture->slice_type = img->type;
if(img->type == B_SLICE /*&& input->HierarchicalCoding!=0*/)
init_mv_scale_hb();
}
#endif
// FTRD>
Bytes_After_Header = img->currentSlice->partArr[0].bitstream->byte_pos;
SetLagrangianMultipliers();
#ifdef SWITCHED_FILTERS
if((img->type != I_SLICE) && (input->UseHPFilter == HPF_SIFO))
{
UnifiedOneForthPixFiltSel(img->filterParam);
SwapFilteredFrames();
}
#endif // SWITCHED_FILTERS
if (input->symbol_mode==CABAC)
{
SetCtxModelNumber ();
}
img->checkref = (input->rdopt && input->RestrictRef && (img->type==P_SLICE || img->type==SP_SLICE));
/*
// Tian Dong: June 7, 2002 JVT-B042
// When the pictures are put into different layers and subseq, not all the reference frames
// in multi-frame buffer are valid for prediction. The acutual number of the valid reference
// frames, fb->num_short_used, will be given by start_slice(sym).
// Save the fb->short_used.
if (input->NumFramesInELSubSeq)
{
short_used = fb->short_used;
}
*/
len = start_slice ();
// Rate control
img->NumberofHeaderBits +=len;
// basic unit layer rate control
if(img->BasicUnit<img->Frame_Total_Number_MB)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -